----------------------------------------------------------------------
#include<iostream>
#include"Polynomial.h"
using namespace std;
//读多项式A和B的函数
void readAB(Polynomial&A,Polynomial&B){
//读控制台多项式A和B
int len_A; //多项式A的项数
int exp_A[10]; //多项式A的指数列表
float coef_A[10]; //多项式A的系数列表
int len_B; //多项式B的项数
int exp_B[10]; //多项式B的指数列表
float coef_B[10]; //多项式B的系数列表
cout<<"请输入多项式A的项数"<<endl;
cin>>len_A;
cout<<"请输入多项式A,例如12x0+3x2+8x7:"<<endl;
for(int i=0;i<len_A-1;i++){
cin>>coef_A[i];
cin.get();
cin>>exp_A[i];
cin.get();
}
cin>>coef_A[len_A-1];
cin.get();
cin>>exp_A[len_A-1];
cout<<"请输入多项式B的项数"<<endl;
cin>>len_B;
cout<<"请输入多项式B,例如12x0+3x2+8x7:"<<endl;
for(int i=0;i<len_B-1;i++){
cin>>coef_B[i];
cin.get();
cin>>exp_B[i];
cin.get();
}
cin>>coef_B[len_B-1];
cin.get();
cin>>exp_B[len_B-1];
//生成多项式对象
A.head = new PolyNode();
A.head->exp = exp_A[0];
A.head->coef = coef_A[0];
A.head->next = NULL;
PolyNode*rear_A = A.head;
for(int i=1;i<len_A;i++){
PolyNode *_pn = new PolyNode();
_pn->exp = exp_A[i];
_pn->coef = coef_A[i];
_pn->next = NULL;
rear_A->next = _pn;
}
B.head = new PolyNode();
B.head->exp = exp_B[0];
B.head->coef = coef_B[0];
B.head->next = NULL;
PolyNode*rear_B = B.head;
for(int i=1;i<len_B;i++){
PolyNode *_pn = new PolyNode();
_pn->exp = exp_B[i];
_pn->coef = coef_B[i];
_pn->next = NULL;
rear_B->next = _pn;
}
}
//主函数
int main(){
Polynomial *A = new Polynomial();
Polynomial *B = new Polynomial();
Polynomial *Sum = new Polynomial();
readAB(*A,*B);
A->display();
B->display();
Sum->Add(*A,*B);
Sum->display();
return 1;
}
网友评论