个人写的多项式的处理程序,输入单元多项式,输出整理后的结果

输入格式为(x^2+1)*(x-1+x^2)这种前面是个括号,后面是个括号,两个多项式

代码如下:

#include<iostream>
#include<vector>
#include <algorithm>
#include<string>

using namespace std;

char c[100];
int t=0;

struct ploy
{
	double xishu;
	int    zhishu;
};
//定义的结点
int first_kh(int i)
{
	int r=i;
	while((c[r]!=')')&&(r<t))
	{
		r++;
	}
	return r;
}

int first_aom(int i,int n)
{  
	int r=i+1;
we:
	while((c[r]!='+')&&(c[r]!='-')&&(r<n))
	{
		r++;
	}
    if((c[r]=='-')&&(c[r-1]=='^'))
	{
		r++;
		goto we;
	}
	return r;
}

vector<ploy> get_vect(int v,int n)
{
	vector<ploy> head;
    int w;
	for(int i=v;i<n;)
	{
		struct ploy p;
		p.xishu=1;
		p.zhishu=0;
		w=first_aom(i,n);
		bool flag=true;
		for(int j=i;j<w;j++)
		{
			if(((c[j]=='-')||(isdigit(c[j])))&&flag)
			{
				char*s=&c[j];
				p.xishu=strtod(s,NULL);
				if((c[j+1]=='x')&&(c[j]=='-'))
				{
					p.xishu=-1;
				}	
				flag=false;
			}
			if(c[j]=='x')
			{
				p.zhishu=1;
			}
			if(c[j]=='^')
			{
				char*s=&c[j+1];
				p.zhishu=strtod(s,NULL);
				break;
			}
		}
		if((w-v)!=1)
		{
			head.push_back(p);	
		}
		i=w;
	}
	return head;
}

void print1(const ploy& v)
{
	int i=0;
	if(v.xishu<0)
	{
		if(v.xishu==-1)
		{
			if(v.zhishu<0)
			{
				i=1;
			}
			if(v.zhishu==0)
			{
				i=2;
			}
			if(v.zhishu==1)
			{
				i=3;
			}
			if(v.zhishu>1)
			{
				i=4;
			}
		}
		if(v.xishu!=-1)
		{
			if(v.zhishu<0)
			{
				i=5;
			}
			if(v.zhishu==0)
			{
				i=6;
			}
			if(v.zhishu==1)
			{
				i=7;
			}
			if(v.zhishu>1)
			{
				i=8;
			}
		}
	}
	if(v.xishu>0)
	{
		if(v.xishu==1)
		{
			if(v.zhishu<0)
			{
				i=9;
			}
			if(v.zhishu==0)
			{
				i=10;
			}
			if(v.zhishu==1)
			{
				i=11;
			}
			if(v.zhishu>1)
			{
				i=12;
			}
		}
		if(v.xishu!=1)
		{
			if(v.zhishu<0)
			{
				i=13;
			}
			if(v.zhishu==0)
			{
				i=14;
			}
			if(v.zhishu==1)
			{
				i=15;
			}
			if(v.zhishu>1)
			{
				i=16;
			}
		}
	}
	switch(i)
	{
		case 1:
			cout<<"x^"<<v.zhishu;		
		break;
		case 2:
			cout<<1;		
		break;
		case 3:
			cout<<"x";		
		break;
		case 4:
			cout<<"x^"<<v.zhishu;
		break;
		case 5:
			cout<<-v.xishu<<"x^"<<v.zhishu;
		break;
		case 6:
			cout<<-v.xishu;
		break;
		case 7:
			cout<<-v.xishu<<"x";
		break;
		case 8:
			cout<<-v.xishu<<"x^"<<v.zhishu;
		break;
		case 9:
			cout<<"x^"<<v.zhishu;
		break;
		case 10:
			cout<<1;
		break;
		case 11:
			cout<<"x";
		break;
		case 12:
			cout<<"x^"<<v.zhishu;
		break;
		case 13:
			cout<<v.xishu<<"x^"<<v.zhishu;
		break;
		case 14:
			cout<<v.xishu;
		break;
		case 15:
			cout<<v.xishu<<"x";
		break;
		case 16:
			cout<<v.xishu<<"x^"<<v.zhishu;
		break;
	}
}

void print(vector<ploy>& v)
{
	if(v.empty()){cout<<0;return;}
	vector<ploy>::iterator it=v.begin();
	if((*it).xishu<0)
	{
			cout<<"-";
	}
	else
	{
		cout<<"\0";
	}
	print1(*it);
	it++;
	for(;it!=v.end();it++)
	{
		if((*it).xishu<0)
		{
			cout<<"-";
		}
		else
		{
			cout<<"+";
		}
		print1(*it);
	}
}

vector<ploy> operator+(vector<ploy>& op1,vector<ploy>& op2){//重载加法运算符
	if(op1.empty())return op2;if(op2.empty())return op1;
	vector<ploy> answer;
	vector<ploy>::iterator it1=op1.begin();
	vector<ploy>::iterator it2=op2.begin();
	while(it1!=op1.end() && it2!=op2.end()){
		if(((*it1).zhishu)>((*it2).zhishu)){
			answer.push_back(*it1);
			it1++;
			continue;
		}
		else{
			if(((*it1).zhishu<(*it2).zhishu)){
				answer.push_back(*it2);
				it2++;
				continue;
			}
			else{
				if((*it1).xishu!=-(*it2).xishu){					
					ploy temp;
					temp.xishu=(*it1).xishu+(*it2).xishu;temp.zhishu=(*it1).zhishu;
					answer.push_back(temp);
				}
				it1++;it2++;
				continue;
			}
		}
	}
	if(it1!=op1.end()){
		while(it1!=op1.end()){
			answer.push_back(*it1);
			it1++;
		}
	}
	else{
		if(it2!=op2.end()){
			while(it2!=op2.end()){
				answer.push_back(*it2);
				it2++;
			}
		}
	}
	return answer;
}

vector<ploy> operator-(vector<ploy>& op1,vector<ploy>& op2){//重载减法运算符
	vector<ploy> answer;
	vector<ploy>::iterator it1=op1.begin();
	vector<ploy>::iterator it2=op2.begin();
	if(op2.empty()){return op1;}
	if(op1.empty()){
		for(;it2!=op2.end();it2++)
		{
			ploy temp;
			temp.xishu=-(*it2).xishu;
			temp.zhishu=(*it2).zhishu;
			answer.push_back(temp);
		}
		return answer;
	}
	it2=op2.begin();
	while(it1!=op1.end() && it2!=op2.end()){
		if(((*it1).zhishu)>((*it2).zhishu)){
			answer.push_back(*it1);it1++;
			continue;
		}
		else{
			if(((*it1).zhishu<(*it2).zhishu)){
				ploy temp;temp.zhishu=(*it2).zhishu;temp.xishu=-(*it2).xishu;
				answer.push_back(temp);it2++;
				continue;
			}
			else{
				ploy temp;
				if((temp.xishu=(*it1).xishu-(*it2).xishu)!=0){
					temp.zhishu=(*it1).zhishu;
					answer.push_back(temp);
				}
				it1++;it2++;
				continue;
			}
		}
	}
	if(it1!=op1.end()){
		while(it1!=op1.end()){
			answer.push_back(*it1);
			it1++;
		}
	}
	else{
		while(it2!=op2.end()){
			ploy temp;
			temp.xishu=-(*it2).xishu;temp.zhishu=(*it2).zhishu;
			answer.push_back(temp);
			it2++;
		}
	}
	return answer;
}

vector<ploy> operator*(vector<ploy>& op1,ploy& op2){//乘法运算符重载
	vector<ploy> answer;
	vector<ploy>::iterator it1=op1.begin();
	for(;it1!=op1.end();it1++){
		ploy temp;
		temp.xishu=(*it1).xishu*op2.xishu;temp.zhishu=(*it1).zhishu+op2.zhishu;
		answer.push_back(temp);
	}
	return answer;
}
vector<ploy> operator*(ploy& op1,vector<ploy>& op2){
	vector<ploy> answer;
	vector<ploy>::iterator it2=op2.begin();
	for(;it2!=op2.end();it2++){
		ploy temp;
		temp.xishu=(*it2).xishu*op1.xishu;temp.zhishu=(*it2).zhishu+op1.zhishu;
		answer.push_back(temp);
	}
	return answer;
}

vector<ploy> operator*(vector<ploy>& op1,vector<ploy>& op2){
	vector<ploy> answer;
	vector<ploy>::iterator it1=op1.begin();
	vector<ploy>::iterator it2=op2.begin();
	for(;it2!=op2.end();it2++){
		answer=answer+op1*(*it2);
	}
	return answer;
}

vector<ploy> operator/(vector<ploy>& op1,vector<ploy>& op2){
	vector<ploy> answer;
	if(op1.empty())return answer;if(op2.empty())return op1;
	if(op1[0].zhishu<op2[0].zhishu){return answer;}
	vector<ploy> v1=op1;
	vector<ploy>::iterator it1=v1.begin();
	vector<ploy>::iterator it2=op2.begin();
	while(!v1.empty()){
		ploy temp;temp.zhishu=v1[0].zhishu-op2[0].zhishu;temp.xishu=v1[0].xishu/op2[0].xishu;
		answer.push_back(temp);
		v1=v1-op2*temp;
		if(v1[0].zhishu<op2[0].zhishu)break;
	}
	return answer;
}

vector<ploy> operator%(vector<ploy>& op1,vector<ploy>& op2){
	vector<ploy> answer;
	if(op1.empty())return answer;if(op2.empty())return op1;
	answer=op1-op2*(op1/op2);
	return answer;
}

vector<ploy> operator*(double d,vector<ploy>& v){
	vector<ploy> answer;
	ploy temp;temp.xishu=d;temp.zhishu=0;
	answer=temp*v;
	return answer;
}

vector<ploy> operator*(vector<ploy>& v,double d){
	vector<ploy> answer;
	ploy temp;temp.xishu=d;temp.zhishu=0;
	answer=temp*v;
	return answer;
}

int cmp(const ploy &a, const ploy &b){
    if(a.zhishu >=b.zhishu )
       return 1;
    else
       return 0;
}

int main(){
	while(1)
	{
		vector<ploy> head1,head2;
		int ch,i=0;
		while((ch=getchar())!=EOF&&(ch!='\n'))
		c[t++]=ch;
		int m=first_kh(0);
		head1=get_vect(0,m);
		char s=c[m+1];
	    head2=get_vect(m+2,t-1);
		sort(head1.begin(),head1.end(),cmp);
        sort(head2.begin(),head2.end(),cmp);
#if 0
		for(int k=0;k<head1.size();k++)
		{
			cout<<head1[k].xishu<<"  "<<head1[k].zhishu<<endl;
		}
		for(int t=0;t<head2.size();t++)
		{
			cout<<head2[t].xishu<<"  "<<head2[t].zhishu<<endl;
		}
#endif
		switch(s)
		{
			case '+':
			{
				vector<ploy> answer=head1+head2;
				print(answer);cout<<endl;
			}
			break;
			case '-':
			{
				vector<ploy> answer2=head1-head2;
				print(answer2);cout<<endl;
			}
			break;
			case '*':
			{
				vector<ploy> answer3=head1*head2;
				print(answer3);cout<<endl;
			}
			break;
			case '/':
			{
				vector<ploy> answer4=head1/head2;
				print(answer4);cout<<endl;
			}
			break;
			case '%':
			{
				vector<ploy> answer5=head1%head2;
				print(answer5);cout<<endl;
			}
			break;			
		}

		memset(c,'\0',sizeof(c));
	}
	return 0;
}


整体思想就是:字符分析器——将(x+1)*(x+1)转换成结点(1 1)(1 0)这是第一个多项式,(1 1)(1 0)这是第二个多项式

第一行是输入,第二行是输出结果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值