一元多项式乘法字符运算(关联容器map)

来源:互联网 发布:电脑淘宝扫一扫在哪 编辑:程序博客网 时间:2024/05/18 06:28

一元多项式乘法运算和化简

 

编程实现如下功能:对两个输入的一元多项式,进行多项式乘法运算,输出结果一元多项式,要求结果中除单项式外不再含乘法,并经过化简(同类项合并,并按指数降序排序)。

 

说明: 

1、多项式由若干个单项式组成,单项式之间为加、减(+,-)关系。
2、单项式指数字与字母幂的乘积构成的代数式。对一元多项式,字母只有一种。
3、多项式乘法:一个多项式的每一项分别乘以另一个多项式每一项(系数相乘,指数相加),再把所得的积相加减。
4、同类项合并指将多项式中指数相同的单项式,系数经过加减求和,合并为一个单项式。按指数降序指多项式中,单项式按指数从大到小顺序相连。

 

格式说明

一元多项式输入输出时以字符串形式表示,格式如下

1、单项式之间用单个加减运算符相连,运算符:+,- 
2单项式由系数、字母、指数标识符、指数依次直接相连组成,各部分均不能省略。
系数:只由若干0到9数字字符组成(系数不等于0,且不以0开头) 
字母:X
指数标识符:^
指数:只由若干0到9数字字符组成(指数可等于0,不等于0时不以0开头) 

 

其他约定
输入不为空串,输出若为0则以空串表示 
字符串中除以上字符,不包含空格等其他字符,字符串尾部以’\0’结束
多项式中第一个单项式前加运算时省略+符号,减运算时有-符号 

 

注意:输入多项式符合上述格式,无需检查;输出多项式格式由考生程序保证

规格:

输入多项式满足如下规格,考生程序无需检查: 

–0<单项式系数<=1000<> 
–0<=单项式指数<=3000<> 
–单项式个数不限制,但相乘运算和同类项合并处理后,单项式的系数小于65535。

 

示例:

例一

输入:

 "7X^4" 

 "-7X^4"

输出:

 "-49X^8"

 

例二

输入:

 "7X^4-5X^3+3X^3+1X^0" 

 "-7X^4+5X^3-3X^3+3X^3+1X^0"

输出:

 "-49X^8+49X^7-10X^6+3X^3+1X^0"

代码:------------------------------------------------》》》》》》》》》》》》》》》》》》》》

#include<iostream>

#include<map>
#include<string>
using namespace std;
/*string answer(string a,string b){
int i,j,pos_a,pos_b;
string c;
if(isdigit(a[0]-'0'))
a.insert(0,1,'+');
if(isdigit(b[0]-'0'))
b.insert(0,1,'+');
for(i=0;i<a.size();i++){
pos_a = a.find(i,'X');
for(j=0;j<b.size();j++){
pos_b = b.find(j,'X');
c.insert(0,"");
}
}
}*/
string answer(string a,string b){
int i,j,k,s,q,pos_a,pos_b;
string value_pos,key_pos,c;
map <int,int> ca;
map <int,int> cb;
map <int,int> cc;
if(isdigit(a[0]))
a.insert(0,1,'+');
if(isdigit(b[0]))
b.insert(0,1,'+');
cout<<a<<"  *  "<<b<<endl;
//cout<<a.size()<<"  "<<a.find('X',i)<<" "<<a.find_last_of('X')<<endl;
string::size_type p=a.find_last_of('X');
//cout<<isdigit(a[1]);
for(i=0;i<p+1;i=pos_a+1){
pos_a = a.find('X',i);
k = pos_a - 1;
while(isdigit(a[k]))k--;
s = pos_a + 2;
while((s<a.size())&&isdigit(a[s]))s++;
value_pos = a.substr(k,pos_a-k);
key_pos = a.substr(pos_a+2,s-pos_a-2);
int pos1 = atoi(value_pos.c_str());
int pos2 = atoi(key_pos.c_str());
pair<map<int,int>::iterator,bool> ret1 = ca.insert(make_pair(pos2,pos1));
if(!ret1.second)
ca[pos2] = ca[pos2] + pos1;
}
p=b.find_last_of('X');
for(i=0;i<p+1;i=pos_b+1){
pos_b = b.find('X',i);
k = pos_b - 1;
while(isdigit(b[k]))k--;
s = pos_b + 2;
while((s<b.size())&&isdigit(b[s]))s++;
value_pos = b.substr(k,pos_b-k);
key_pos = b.substr(pos_b+2,s-pos_b-2);
int pos1 = atoi(value_pos.c_str());
int pos2 = atoi(key_pos.c_str());
pair<map<int,int>::iterator,bool> ret2 = cb.insert(make_pair(pos2,pos1));
if(!ret2.second)
cb[pos2] = cb[pos2] + pos1;
}
int ia,ib;
for(map<int,int>::iterator i=ca.begin();i!=ca.end();i++){
for(map<int,int>::iterator j=cb.begin();j!=cb.end();j++){
cout<<i->second<<"   "<<j->second<<endl;
ia = (i->first)+(j->first);
ib = (i->second)*(j->second);
pair<map<int,int>::iterator,bool> ret = cc.insert(make_pair(ia,ib));
if(!ret.second){
cc[ia] = cc[ia] + ib;
}
}
}
cout<<"----- "<<endl;
for(map<int,int>::iterator u=cc.begin();u!=cc.end();u++)
cout<<u->first<<"   "<<u->second<<endl;
char y[10];
map<int,int>::iterator w=cc.begin();
itoa(w->second,y,10);
if(w->second > 0) {c.insert(c.size(),y),c.erase(0,1);}
c.insert(c.size(),y);
c.insert(c.size(),"X^");
itoa(w->first,y,10);
c.insert(c.size(),y);
for(w++;w!=cc.end();w++){
if(w->second == 0){
continue ;
}
else if((w->second) > 0){
y[0] = '+';
itoa(w->second,y+1,10);
}
else
itoa(w->second,y,10);
c.insert(c.size(),y);
c.insert(c.size(),"X^");
itoa(w->first,y,10);
c.insert(c.size(),y);
}
cout<<"as"<<endl<<c<<endl;
system("pause");
return NULL;
}
void main(){
string a="7X^4-5X^3+3X^3+1X^0";
string b="-7X^4+5X^3-3X^3+3X^3+1X^0";
answer(a,b);
}
0 0
原创粉丝点击