C++编写二元多项式,并实现基本的加减运算

选择合适的存储结构表示二元多项式,并实现基本的加减运算

要求: 

1)二元多项式的输入采用如下方式进行键盘输入

5y^2+7x^4 + (3y^4+2y+9)x^2 + (2y^2+y)x + (y+9)

2)按照键盘输入的方式进行结果输出

3要求输出结果的升幂和降幂两种排列情况


/*本程序由孙旭编写,有些不足,敬请赐教
   仅供参考,就没写备注啦
   */ 





#include<iostream>
#include<string>
#include<math.h>
#include<strstream>
#include<sstream>
using namespace std;



void Get_Y(string s,string str1[100],char f[100],char k) {//获得多项式的系数 
	int i=0;
	while(i<s.size()) {
		string temp="";
		if(s[i]=='(') {
			i++;
			do {
				temp+=s[i];
				i++;
			} while(s[i]!=')');
			i++;
			if(i<s.size()) {
				if(s[i]=='x') {
					i++;
					if(s[i]=='^') {
						i++;
						str1[s[i]-'0']=temp;
						f[s[i]-'0']=k;
						i++;
						k=s[i];
						i++;
					} else {
						str1[1]=temp;
						f[1]=k;
						k=s[i];
						i++;
					}
				} else {
					str1[0]=temp;
					f[0]=k;
					i++;
				}
			} else {
				str1[0]=temp;
					f[0]=k;
					i++;
			}
		} else {
			if(s[i]>='0'&&s[i]<='9') {
				do {
					temp+=s[i];
					i++;
				} while(s[i]>='0'&&s[i]<='9'&&(i<s.size()));
				if(i<s.size()) {
				if(s[i]=='x') {
					i++;
					if(s[i]=='^') {
						i++;
						str1[s[i]-'0']=temp;
						f[s[i]-'0']=k;
						i++;
						k=s[i];
						i++;
					} else {
						str1[1]=temp;
						f[1]=k;
						k=s[i];
						i++;
					}
				} else {
					str1[0]=temp;
					f[0]=k;
					i++;
				}
			} else {
				str1[0]=temp;
					f[0]=k;
					i++;
			}
				
			} else {
				if(s[i]=='x') {
					i++;
					if(s[i]=='^') {
						i++;
						str1[s[i]-'0']="1";
						f[s[i]-'0']=k;
						i++;
						k=s[i];
						i++;
					} else {
						str1[1]="1";
						f[1]=k;
						k=s[i];
						i++;
					}
				} else {
					str1[0]="1";
					f[0]=k;
					i++;
				}
			}
		}
	}
}

void Change_Y(string str1[100],char f[100]) {//根据符号来改变关于y多项式的结构 
	int i,j;
	for(i=0;i<100;i++) {
		if(f[i]!=0&&f[i]=='-') {
			if(str1[i][0]=='-') {
				for(j=0;j<str1[i].size();j++) {
					if(str1[i][j]=='-') {
						str1[i][j]='+';
					} else {
						if(str1[i][j]=='+') {
						str1[i][j]='-';
					}
					}
				}
			} else {
				string temp="-";
				for(j=0;j<str1[i].size();j++) {
					if(str1[i][j]=='-') {
						str1[i][j]='+';
					} else {
						if(str1[i][j]=='+') {
						str1[i][j]='-';
					}
					}
				}
				str1[i]=temp+str1[i];
			}
		}
	} 
}

int Get_Num(string temp) {
	int i;
	int len=temp.size();
	int sum=0;
	for(i=0;i<len;i++) {
		sum+=(temp[i]-'0')*pow(10,len-i-1);
	}
	return sum;
}

string Get_String(int num) {
	strstream ss;
    string s;
    ss << num;
    ss >> s;
	return s;
}

void Get_M(string str1[100],int M[100][100]) {
	int i,j;
	char h;
	for(i=0;i<100;i++) {
		if(str1[i]!="") {
			j=0;
			while(j<str1[i].size()) {
				string temp="";
				if(str1[i][j]=='+'||str1[i][j]=='-') {
					h=str1[i][j];
					j++;
					if(str1[i][j]=='y') {
						temp="1";
					} else {
						do {
						temp+=str1[i][j];
						j++;
					} while((str1[i][j]>='0')&&(str1[i][j]<='9')&&(j<str1[i].size()));
				}
				
				if(str1[i][j]=='y') {
					j++;
					if(j<str1[i].size()) {
						if(str1[i][j]=='^') {
							j++;
							M[i][str1[i][j]-'0']=Get_Num(temp)*(h=='+'?1:(-1));
							j++;
						} else {
							M[i][1]=Get_Num(temp)*(h=='+'?1:(-1));
						}
					} else {
						M[i][1]=Get_Num(temp)*(h=='+'?1:(-1));
					}
				} else {
					M[i][0]=Get_Num(temp)*(h=='+'?1:(-1));
							j++;
				}
				
					
					
				} else {
					h='+';
					if(str1[i][j]=='y') {
						temp="1";
					} else {
						do {
						temp+=str1[i][j];
						j++;
					} while((str1[i][j]>='0')&&(str1[i][j]<='9')&&(j<str1[i].size()));
				}
				
				if(str1[i][j]=='y') {
					j++;
					if(j<str1[i].size()) {
						if(str1[i][j]=='^') {
							j++;
							M[i][str1[i][j]-'0']=Get_Num(temp)*(h=='+'?1:(-1));
							j++;
						} else {
							M[i][1]=Get_Num(temp)*(h=='+'?1:(-1));
						}
					} else {
						M[i][1]=Get_Num(temp)*(h=='+'?1:(-1));
					}
				} else {
					M[i][0]=Get_Num(temp)*(h=='+'?1:(-1));
							j++;
				}
				
				}
			}
		}
	} 
}

void Print(string str1[100]) {
	int i;
	for(i=0;i<100;i++) {
		if(str1[i]!="") {
			cout<<str1[i]<<endl;
		}
	}
}
void Print1(char f[100]) {
	int i;
	for(i=0;i<100;i++) {
		if(f[i]!=0) {
			cout<<i<<" "<<f[i]<<endl;
		}
	}
}

void Print2(int M[100][100]) {
	int i,j;
	for(i=0;i<10;i++) {
		for(j=0;j<10;j++) {
			if(M[i][j]!=0) {
					cout<<M[i][j]<<" ";
			}
		}
		cout<<endl;
	}
}


void Get_Result(int M[100][100],int M1[100][100]) {//获得多项式系数 
	int i,j;
	for(i=0;i<100;i++) {
		for(j=0;j<100;j++) {
			M[i][j]+=M1[i][j];
		}
	}
}


bool isNum(string str)  {
    stringstream sin(str);  
    double d;  
    char c;  
    if(!(sin >> d))  
    {
        return false;
    }
    if (sin >> c) 
    {
        return false;
    }  
    return true;  
}

string Print_S(int M[100][100]) {
	int i,j;
	string sum="";
	i=75;
	while(i>=0) {
		string temp="";
		j=99;
		while(j>=0) {
			if(M[i][j]!=0) {
				if(M[i][j]!=1&&M[i][j]!=-1) {
					temp=temp+Get_String(M[i][j]);
				} else {
					if(M[i][j]==-1&&j!=0) {
						temp=temp+'-';
					} else {
						if(M[i][j]==-1&&j==0) {
							temp=temp+"-1";
						} else {
							if(M[i][j]==1&&j==0) {
						temp=temp+'1';
					} 
						}
						
					}
				}
				if(j!=0&&j!=1) {
					temp=temp+'y'+'^'+Get_String(j);
					j--;
					while(M[i][j]==0&&j>=0) {
						j--;
					}
				
					if(M[i][j]>0) {
						temp=temp+'+';
					} 
				} else {
					if(j==1) {
						temp=temp+'y';
					j--;
					while(M[i][j]==0&&j>=0) {
						j--;
					}
				
					if(M[i][j]>0) {
						temp=temp+'+';
					} 
					} else {
						if(j==0) {
							j--;
						}
					}
				}
			} else {
				j--;
			}
		}
		if(temp.size()!=1&&temp.size()!=0&&isNum(temp)==false) {
			string tem="(";
			tem+=temp;
			temp=tem+")";
		} 
		
		if(temp!="") {
		if(i!=0&&i!=1) {
			sum=sum+temp+'x'+'^'+Get_String(i)+'+';
			i--;
		} else {
			if(i==1) {
				sum=sum+temp+'x'+'+';
				i--;
			} else {
				if(i==0) {
					sum=sum+temp;
					i--;
				}
			}
		}	
		} else {
			i--;
		}
	
	}
	return sum;
}


string Print_J(int M[100][100]) {
	int i,j;
	string sum="";
	i=0;
	while(i<70) {
		string temp="";
		j=0;
		while(j<100) {
			if(M[i][j]!=0) {
				if(M[i][j]!=1&&M[i][j]!=-1) {
					temp=temp+Get_String(M[i][j]);
				} else {
					if(M[i][j]==-1&&j!=0) {
						temp=temp+'-';
					} else {
						if(M[i][j]==-1&&j==0) {
							temp=temp+"-1";
						} else {
							if(M[i][j]==1&&j==0) {
						temp=temp+'1';
					} 
						}
						
					}
				}
				if(j!=0&&j!=1) {
					temp=temp+'y'+'^'+Get_String(j);
					j++;
					while(M[i][j]==0&&j<100) {
						j++;
					}
				
					if(M[i][j]>0) {
						temp=temp+'+';
					} 
				} else {
					if(j==1) {
						temp=temp+'y';
					j++;
					while(M[i][j]==0&&j<100) {
						j++;
					}
				
					if(M[i][j]>0) {
						temp=temp+'+';
					} 
					} else {
						if(j==0) {
							j++;
					while(M[i][j]==0&&j<100) {
						j++;
					}
				
					if(M[i][j]>0) {
						temp=temp+'+';
					} 
						}
					}
				}
			} else {
				j++;
			}
		}
		if(temp.size()!=1&&temp.size()!=0&&isNum(temp)==false) {
			if(temp[temp.size()-1]=='+') {
				temp = temp.substr(0, temp.length()-1);
			}
			string tem="(";
			tem+=temp;
			temp=tem+")";
		} 
		
		if(temp!="") {
		if(i!=0&&i!=1) {
			sum=sum+temp+'x'+'^'+Get_String(i)+'+';
			i++;
		} else {
			if(i==1) {
				sum=sum+temp+'x'+'+';
				i++;
			} else {
				if(i==0) {
					sum=sum+temp+'+';
					i++;
				}
			}
		}	
		} else {
			i++;
		}
	
	}
	if(sum[sum.size()-1]='+') {
				sum = sum.substr(0, sum.length()-1);
			}
	return sum;
}


void Negate(int M1[100][100]) {
	for (int i=0;i<100;i++) {
		for (int j=0;j<100;j++) {
			M1[i][j]=-1*M1[i][j];
		}
	}	
} 

void Get_Run() {
	//string s="(5y^2+7)x^5-(-3y^4+2y-9)x^3-x-(y-6)";
    //string s1="(5y^2+7)x^4+(4y^4-3y+9)x^2+(-2y^2+y)x+(y+9)";
    string s;
	string s1;
    char k='+';
string str1[100];
string str2[100];
char f[100];
char f1[100];
int M[100][100]; 
int M1[100][100];
int choice;
    cout<<"请输入第一个多项式:";
	cin>>s;
	cout<<"请输入第二个多项式: ";
	cin>>s1;
	 
	Get_Y(s,str1,f,k);
	Change_Y(str1,f);
	Get_M(str1,M);
	
	Get_Y(s1,str2,f1,k);
	Change_Y(str2,f1);
	Get_M(str2,M1);
	
	cout<<"请输入你要执行的操作,0表示加法,1表示减法:";
	cin>>choice; 
	switch (choice) {
		case 0:
			Get_Result(M,M1);
		case 1:
			Negate(M1);
			Get_Result(M,M1);
	}
/*	Print(str1);
	Print1(f);*/
	//Print2(M);
	cout<<"降序: "<<Print_S(M)<<endl;
	cout<<"升序: "<<Print_J(M)<<endl;
	

}

int main() {
    
    Get_Run();
	return 0;
}

/*
(5y^2+7)x^5-(-3y^4+2y-9)x^3+x-(y-6)
(5y^2+7)x^4+(3y^4+2y+9)x^2+(2y^2+y)x+(y+9)
*/


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值