链表实现一元多项式的加法和乘法
#include<bits/stdc++.h>
using namespace std;
typedef struct Node{
int ceof;
int exp;
struct Node* next;
}Node;
Node *read(string s)
{
Node *head,*middle,*near;
head=new Node;
near=new Node;
head->next=NULL;
near=head;
int sign=1,i=0,a=0,b=0;
while(s[i])
{
middle=new Node;
a=0,b=0,sign=1;
if(s[i]=='+'){
i++;
}
else if(s[i]=='-'){
sign=-1;
i++;
}
if(s[i]=='x'){
a=1;
i++;
if(s[i]=='^'){
i++;
while(s[i]>='0'&&s[i]<='9'&&i<s.size()){
b=b*10+s[i]-'0';
i++;
}
}
else if(s[i]!='^'||i>=s.size()){
b=1;
}
}
else if(s[i]>='0'&&s[i]<='9'){
while(s[i]>='0'&&s[i]<='9'&&i<s.size()){
a=a*10+s[i]-'0';
i++;
}
if(s[i]=='x'&&i<s.size()){
i++;
if(s[i]=='^'&&i<s.size()){
i++;
while(s[i]>='0'&&s[i]<='9'){
b=b*10+s[i]-'0';
i++;
}
}
else if(s[i]!='^'){
b=1;
}
}
else if(i>=s.size()){
b=0;
}
}
middle->ceof=a*sign;
middle->exp=b;
near->next=middle;
middle->next=NULL;
near=middle;
}
near->next=NULL;
return head;
}
Node *add_link(Node*p1,Node *p2)
{
Node *pr1=new Node;
Node *pr2=new Node;
Node *head=new Node;
Node *middle;
Node *near=new Node;
head->next=NULL;near=head;
pr1=p1->next;pr2=p2->next;
while((pr1)&&(pr2)){
middle=new Node;
if(pr1->exp==pr2->exp){
middle->exp=pr1->exp;
middle->ceof=pr1->ceof+pr2->ceof;
pr1=pr1->next;
pr2=pr2->next;
}
else if(pr1->exp>pr2->exp){
middle->exp=pr1->exp;
middle->ceof=pr1->ceof;
pr1=pr1->next;
}
else {
middle->exp=pr2->exp;
middle->ceof=pr2->ceof;
pr2=pr2->next;
}
near->next=middle;
middle->next=NULL;
near=middle;
}
if(!pr2&&pr1){
while(pr1){
middle=new Node;
middle->exp=pr1->exp;
middle->ceof=pr1->ceof;
near->next=middle;
middle->next=NULL;
near=middle;
pr1=pr1->next;
}
}
else if(pr2&&!pr1){
while(pr2){
middle=new Node;
middle->exp=pr2->exp;
middle->ceof=pr2->ceof;
near->next=middle;
middle->next=NULL;
near=middle;
pr2=pr2->next;
}
}
near->next=NULL;
return head;
}
Node *mul(Node *p1,Node *p2)
{
Node *pr1=new Node;
Node *pr2=new Node;
Node *head=new Node;
Node *near=new Node;
head->next=NULL;
near=head;
Node* middle;
pr1=p1->next,pr2=p2->next;
while(pr2){
middle=new Node;
middle->ceof=pr1->ceof*pr2->ceof;
middle->exp=pr1->exp+pr2->exp;
near->next=middle;
middle->next=NULL;
near=middle;
pr2=pr2->next;
}
Node *r=new Node;
r=head;
near->next=NULL;
pr2=p2;
while(pr1=pr1->next){
pr2=p2;
while(pr2=pr2->next){
Node* root=new Node;
middle=new Node;
middle->exp=pr1->exp+pr2->exp;
middle->ceof=(pr1->ceof)*(pr2->ceof);
root=head;
while(root=root->next){
if(root->next!=NULL){
if(root->exp>middle->exp&&root->next->exp<middle->exp){
middle->next=root->next;
root->next=middle;
break;
}
else if(root->exp==middle->exp){
root->ceof=middle->ceof+root->ceof;
break;
}
}
else if(root->next==NULL){
if(root->exp>middle->exp){
middle->next=NULL;
root->next=middle;
break;
}
else if(root->exp== middle->exp){
root->ceof=middle->ceof+root->ceof;
break;
}
}
}
}
}
return head;
}
void printt(Node *pr)
{
Node *pd=new Node;
pd=pr;
while(pd=pd->next){
if(pd->ceof!=0){
if(abs(pd->ceof)!=1){
if(pd->ceof>0) cout<<"+";
cout<<pd->ceof;
}
else{
if(pd->ceof==1){
cout<<"+";
if(pd->next==NULL&&pd->exp==0) cout<<pd->ceof;
}
else if(pd->ceof==-1){
if(pd->next==NULL&&pd->exp==0) cout<<pd->ceof;
else cout<<"-";
}
}
if(pd->exp!=0){
if(pd->exp==1) cout<<"x";
else cout<<"x"<<"^"<<pd->exp;
}
}
}
cout<<endl;
}
int main()
{
string s1,s2;
cin>>s1>>s2;cin.get();
Node *h1=read(s1);
Node *h2=read(s2);
Node *hh=add_link(h1,h2);
Node *hhh=mul(h1,h2);
printt(hhh);
}