#include <iostream>
using namespace std;
//poly資料型別
typedef struct DataType{
int coef;
int expn;
struct DateType *next;
}data;
//初始化運算式
data *Initlist(void)
{
data *head;
if ((head = (data *)malloc(sizeof(data))) == NULL)
exit(-1);
head->next = NULL;
return head;
}
//釋放鏈表
/*void deleteList(data *head)
{
data *temp1, *temp2;
temp1 = head->next;
while(temp1 != NULL)
{
temp2 = temp1->next;
delete temp1;
temp1 = temp2;
}
head->next = NULL;
delete head;
} */
//銷毀鏈表
/*void DesList(data *head)
{
FreeList(pHead);
free(pHead);
} */
//輸入運算式
void Inputlist(data *head)
{
int coef,expn;
data *tail,*xin;
tail = head;
while(1)
{
cout<<"請輸入(系數,指數),需指數遞增輸入,系數為0時結束輸入:";
cin>>coef>>expn;
if (coef != 0)
{
if ((xin = (data *)malloc(sizeof(data))) == NULL)
{
exit(-1);
}
xin->coef = coef;
xin->expn = expn;
tail->next = xin;
tail = xin;
}
else{
break;
}
}
tail->next = NULL;
}
//輸出運算式
void Outputlist(data *head)
{
data *temp;
temp = head->next;
if (temp == NULL){
cout<<endl;
return;
}
cout<<temp->coef<<" "<< temp->expn;
temp = temp->next;
while(temp != NULL){
cout<< temp->coef<<" "<<temp->expn;
temp = temp->next;
}
cout<<endl;
}
//運算式相加
data *Addlist(data *head1, data *head2)
{
int temp;
data *head3, *xin, *tail; //結果鏈表相關變數
data *temp1, *temp2; //運算鏈表相關變數
temp1 = head1->next;
temp2 = head2->next;
head3 = Initlist();
tail = head3;
//將兩個多項式掃描運算后放入結果鏈表中
while(temp1 != NULL && temp2 != NULL){
//如果指數相等
if (temp1->expn == temp2->expn){
if ((temp = temp1->coef + temp2->coef) == 0)
{
temp1 = temp1->next;
temp2 =temp2->next;
continue;
}
if ((xin = (data *)malloc(sizeof(data))) == NULL)
exit(-1);
xin->coef = temp;
xin->expn = temp1->expn;
tail->next = xin;
xin->next = NULL;
tail = xin;
temp1 =temp1->next;
temp2 = temp2->next;
}//如果運算式1指數小于2
else if(temp1->expn < temp2->expn)
{
if ((xin= (data *)malloc(sizeof(data))) == NULL)
exit(-1);
*xin= *temp1;
xin->next = NULL;
tail->next = xin;
tail = xin;
temp1 = temp1->next;
}//如果運算式1指數大于2
else{
if ((xin = (data *)malloc(sizeof(data))) == NULL)
exit(-1);
*xin = *temp2;
xin->next = NULL;
tail->next = xin;
tail = xin;
temp2 = temp2->next;
}
}
//將剩余的未掃描項放入結果鏈表中
while(temp1 != NULL){
if ((xin = (data *)malloc(sizeof(data))) == NULL)
exit(-1);
*xin = *temp1;
xin->next = NULL;
tail->next = xin;
tail = xin;
temp1 = temp1->next;
}
while(temp2 != NULL){
if ((xin = (data *)malloc(sizeof(data))) == NULL)
exit(-1);
*xin = *temp2;
xin->next = NULL;
tail->next = xin;
tail = xin;
temp2 = temp2->next;
}
return head3;
}
//兩個多項式減法
data *Sublist(data *head1, data *head2)
{
data *head3, *temp;
temp = head2->next;
//被減運算式每一項系數乘以-1
while(temp != NULL){
temp->coef = -1 * temp->coef;
temp = temp->next;
}
head3 = Addlist(head1, head2);
//將被減運算式值復原
temp = head2->next;
while(temp != NULL){
temp->coef = -1 * temp->coef;
temp = temp->next;
}
return head3;
}
int main()
{
data *head1, *head2, *result;
head1 = Initlist();
Inputlist(head1);
head2 = Initlist();
Inputlist(head2);
cout<<"運算式1:";
Outputlist(head1);
cout<<"運算式2:";
Outputlist(head2);
result = Addlist(head1, head2);
cout<<"運算式1 + 運算式2 = ";
Outputlist(result);
DeleteList(result);
result = Sublist(head1, head2);
cout<<"運算式1 - 運算式2 = ";
Outputlist(result);
DeleteList(result);
DeleteList(head1);
DeleteList(head2);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/54893.html
標籤:基礎類
上一篇:codeblocks怎么調編譯器
下一篇:c++的野指標
