多项式加减乘

10185101150 edited 5 年前

include

include

include

include

using namespace std;
typedef int type;
struct node
{type di;
type zhi;
append(int p=0,int q=0);
};
int cmp(const void a,const void b)
{struct node ch1,ch2;
ch1=(struct node )a;
ch2=(struct node )b;
return ch2.zhi-ch1.zhi;
}
int cmp2(const void a,const void b)
{struct node ch1,ch2;
ch1=(struct node )a;
ch2=(struct node )b;
return ch1.zhi-ch2.zhi;
}
class stack
{public:
stack();
void push(const int &data,const int &zhi);
void pop();
int topzhi();
int topdi();
bool is_empty();
void fuyuan();
int size();
void clear();
int floorzhi();
int floordi();
void floorpop();
void sort();
void sort2();
private:
node entry[100];
int count;
int floor;
int max;
};
stack::stack()
{count=0;
max=0;
floor=0;
}
void stack::sort()
{qsort(entry,count,sizeof(entry[0]),cmp);
}
void stack::sort2()
{qsort(entry,count,sizeof(entry[0]),cmp2);
}
int stack::floordi()
{return entry[floor].di;
}
int stack::floorzhi()
{return entry[floor].zhi;
}
void stack::floorpop()
{floor++;
}
void stack::clear()
{count=0;
}
int stack::size()
{return count;
}
void stack::push(const int &data,const int &zhi)
{entry[count].di=data;
entry[count].zhi=zhi;
count++;
max++;
}
void stack::pop()
{count–;
}
int stack::topzhi()
{return entry[count-1].zhi;
}
int stack::topdi()
{return entry[count-1].di;
}
bool stack::is_empty()
{if (count==0)
return true;
return false;
}
void stack::fuyuan()
{count=max;
}

stack cjn,ckn,hyc;
void add()
{
while (!cjn.is_empty()&&!ckn.is_empty())
if (cjn.topzhi()==ckn.topzhi())
{hyc.push(cjn.topdi()+ckn.topdi(),cjn.topzhi());
cjn.pop();
ckn.pop();
}
else if (cjn.topzhi()>ckn.topzhi())
{hyc.push(ckn.topdi(),ckn.topzhi());
ckn.pop();
}
else
{hyc.push(cjn.topdi(),cjn.topzhi());
cjn.pop();
}

while (!cjn.is_empty())
{ hyc.push(cjn.topdi(),cjn.topzhi());
cjn.pop();
}
while (!ckn.is_empty())
{ hyc.push(ckn.topdi(),ckn.topzhi());
ckn.pop();
}
int pan=1;
while (!hyc.is_empty())
{
if (hyc.topdi()>0)
{if (pan!=1)
printf(“+”);
}
printf(“%d x%d”,hyc.topdi(),hyc.topzhi());
hyc.pop();
pan=0;
}

}//加法,已经成功
void minu()
{while (!cjn.is_empty()&&!ckn.is_empty())
if (cjn.topzhi()==ckn.topzhi())
{hyc.push(cjn.topdi()-ckn.topdi(),cjn.topzhi());
cjn.pop();
ckn.pop();
}
else if (cjn.topzhi()>ckn.topzhi())
{hyc.push(ckn.topdi(),ckn.topzhi());
ckn.pop();
}
else
{hyc.push(cjn.topdi(),cjn.topzhi());
cjn.pop();
}
while (!cjn.is_empty())
{ hyc.push(cjn.topdi(),cjn.topzhi());
cjn.pop();
}
while (!ckn.is_empty())
{ hyc.push(ckn.topdi(),ckn.topzhi());
ckn.pop();
}
int pan=1;
while (!hyc.is_empty())
{if (hyc.topdi()>0)
{if (pan!=1)
printf(“+”);
}
printf(“%d x%d”,hyc.topdi(),hyc.topzhi());
hyc.pop();
pan=0;
}
}//减法。 成功。
stack hl[100];
int m,n;
void add2(int p,int q)
{stack yz;
int i;
if (q==m)
{int pan=1;
hl[p].sort2();
while (!hl[p].is_empty())
{if (hl[p].topdi()>0)
{if (pan!=1)
printf(“+”);
}
printf(“%d x%d”,hl[p].topdi(),hl[p].topzhi());
hl[p].pop();
pan=0;
}
}
else
{while (!hl[p].is_empty()&&!hl[q].is_empty())

       hl[q].pop();
      }
  else 
      {yz.push(hl[p].topdi(),hl[p].topzhi());
       hl[p].pop();
      }

while (!hl[p].is_empty())
{ yz.push(hl[p].topdi(),hl[p].topzhi());
hl[p].pop();
}
while (!hl[q].is_empty())
{ yz.push(hl[q].topdi(),hl[q].topzhi());
hl[q].pop();
}//接下来要把yz的元素送给hl【q];
hl[q].clear();
for (i=0;i<yz.size();i++)
{hl[q].push(yz.floordi(),yz.floorzhi());
yz.floorpop();
}
yz.clear();
}
}//乘法的附庸加法。
void mult()
{
int i,j;
m=cjn.size();
n=ckn.size();
for (i=0;i<m;i++)
{for (j=0;j<n;j++)
{hl[i].push(cjn.topdi()*ckn.topdi(),cjn.topzhi()+ckn.topzhi());
ckn.pop();
}
hl[i].sort();
ckn.fuyuan();
cjn.pop();
}
for (i=0;i<m;i++)
{add2(i,i+1);
}

}
int main()
{
int i;
char a[100],b[100];
scanf(“%s”,a);
scanf(“%s”,b);
int len1=strlen(a);
int len2=strlen(b);
int mi=0;
int data=0;
int fuhao=0;
for (i=0;i=48&&a[i]<=57)
{a[i]-=48;
mi=mi10+a[i];
i++;
}
break;
default :
while (a[i]>=48&&a[i]<=57)
{a[i]-=48;
data=data
10+a[i];
i++;
}
if (fuhao==1)
data=-data;
}
if (mi!=0&&data!=0)
{cjn.push(data,mi);
data=0;
mi=0;
}
if (data!=0&&mi==0&&i==len1)
cjn.push(data,0);
}
data=0;
fuhao=0;
for (i=0;i=48&&b[i]<=57)
{b[i]-=48;
mi=mi10+b[i];
i++;
}
break;
default :
while (b[i]>=48&&b[i]<=57)
{b[i]-=48;
data=data
10+b[i];
i++;
}
if (fuhao==1)
data=-data;
}
if (mi!=0&&data!=0)
{ckn.push(data,mi);
data=0;
mi=0;
}
if (data!=0&&mi==0&&i==len2)
ckn.push(data,0);
}//输入;
getchar();
int op=getchar();
switch (op)
{case ‘+’:add();
case ‘-‘:minu();
default :mult();
}
return 0;
}

Comments

10185101150

include

include

include

include