链表实现多项式加法,减法,乘法
#include <iostream>
#include <process.h>
using namespace std;
struct list{
double coe;
double exp;
list * next;
};
int inputChioce();
void showInfo(list * p);
list * createList();
list * merge(list * head);
list * addList(list * p1,list * p2);
list * subtract(list * p1,list * p2);
list * multiply(list * p1,list * p2);
list * addList(list * p1,list * p2){
list * p = new list;
list * head = p;
list * tail = p;
p1 = p1->next;
p2 = p2->next;
while(p1 != NULL || p2!= NULL){
if(p1 != NULL && p2 != NULL){
p = new list;
if(p1->exp == p2->exp){
p->coe = (p1->coe) + (p2->coe);
p->exp = p1->exp;
p1 = p1->next;
p2 = p2->next;
}else if(p1->exp < p2->exp){
p->coe = p1->coe;
p->exp = p1->exp;
p1 = p1->next;
}else{
p->exp = p2->exp;
p->coe = p2->coe;
p2 = p2->next;
}
p->next = NULL;
tail->next = p;
tail = tail->next;
}else{
if(p1 == NULL){
tail->next = p2;
break;
}else{
tail->next = p1;
break;
}
}
}
cout << "相加的结果:";
showInfo(head);
return head;
}
list * subtract(list * p1,list * p2){
list * p = new list;
list * head = p;
list * tail = p;
p1 = p1->next;
p2 = p2->next;
while(p1 !=NULL || p2 != NULL){
if(p1 != NULL && p2 != NULL){
if(p1->exp == p2->exp){
if(p1->coe == p2->coe){
p1 = p1->next;
p2 = p2->next;
}else{
p = new list;
p->exp = p1->exp;
p->coe = (p1->coe)-(p2->coe);
p1 = p1->next;
p2 = p2->next;
}
}else if(p1->exp < p2->exp){
p = new list;
p->exp = p1->exp;
p->coe = p1->coe;
p1 = p1->next;
}else{
p = new list;
p->exp = p2->exp;
p->coe = -(p2->coe);
}
p->next = NULL;
tail->next = p;
tail = tail->next;
}else{
if(p2 == NULL){
tail->next = p1;
}else if(p1 == NULL){
while(p2!= NULL){
p = new list;
p->exp = p2->exp;
p->coe = -(p2->coe);
tail->next = p;
tail = tail->next;
p2 = p2->next;
}
tail->next = NULL;
}
}
}
cout << "相减的结果:";
showInfo(head);
return head;
}
list * multiply(list * p1,list * p2){
p1 = p1->next;
p2 = p2->next;
list * pa = p1;
list * pb = p2;
list * s = new list;
list * head = s;
list * tail = s;
while(pa != NULL){
while(pb != NULL){
s = s->next;
s = new list;
s->coe = pa->coe * pb->coe;
s->exp = pa->exp + pb->exp;
tail->next = s;
tail = tail->next;
pb = pb->next;
}
pa = pa->next;
pb = p2;
}
s->next = NULL;
head = merge(head);
cout << "相加的结果:";
showInfo(head);
return head;
}
list * merge(list * head){
list * p1 = head->next;
list * p2Prev = p1;
list * p2 = p1->next;
list * t;
while(p1->next != NULL){
while(p2 != NULL){
if(p1->exp == p2->exp){
p1->coe += p2->coe;
t = p2;
p2Prev->next = p2->next;
p2 = p2->next;
delete (t);
}else{
p2Prev = p2Prev->next;
p2 = p2->next;
}
}
p1 = p1->next;
p2Prev = p1;
p2 = p1->next;
}
return head;
}
list * creatList()
{
list * p = new list;
p->next = NULL;
list * head = p;
list * tail = p;
double coe,exp;
int count = 0;
while(true)
{
count++;
cout << "请输入第" << count <<"项的系数(系数和指数都为-1 为结束多项式输入):";
cin >>coe;
cout << "请输入第" << count <<"项的指数(系数和指数都为-1 为结束多项式输入):";
cin >> exp;
if(coe == -1 && exp == -1){
system("cls");
break;
}else{
p = new list;
p->coe = coe;
p->exp = exp;
p->next = NULL;
tail->next = p;
tail = tail->next;
system("cls");
}
}
showInfo(head);
return head;
}
void showInfo(list * p)
{
p = p->next;
while(p != NULL){
cout << " "<< p->coe << "Xexp(" << p->exp <<") ";
if(p->next != NULL && p->next->coe > 0)
cout << "+";
p = p->next;
}
cout << endl;
}
int inputChoice(){
cout << endl;
cout << endl;
cout << endl;
cout << "1.将两个多项式相加" << endl;
cout << "2.将两个多项式相减" << endl;
cout << "3.将两个多项式相乘" << endl;
cout << "4.退出" <<endl;
int choice;
cout << "请输入你的选择:";
cin >> choice;
while(choice < 1 || choice > 4){
cout << "你输入有误,请重新输入:";
cin >> choice;
}
return choice;
}
int main()
{
cout << "创建两个多项式,请输入第一个多项式" << endl;;
list * F1 = creatList();
cout << "请输入下一个多项式"<< endl;
list * F2 = creatList();
int choice = inputChoice();
while(choice != 4){
switch(choice){
case 1:
addList(F1,F2);
break;
case 2:
subtract(F1,F2);
break;
case 3:
multiply(F1,F2);
break;
case 4:
break;
}
choice = inputChoice();
}
system("pause");
return 0;
}