我想把(5*(12-3)+4)/2;这个多项式求出来,可是有一步老是错误,请各位老大指教下!!!
#include<iostream>
#include<stack>
using namespace std;
bool b=false;
class StackOperat
{
public:
char operat;
StackOperat *point;
};
class StackData
{
public:
int number;
StackData *point;
};
class Container
{
public:
StackData *data;
StackOperat *operat;
};
Container *calcu(StackOperat *q,StackData *p);
void initStack()
{
Container *c;
int v;
StackOperat *q,*curret;
StackData *p,*next;
next=new StackData;next->point=NULL;next->number=0;
p=new StackData;p->point=next;next=p;//按相反的顺序指
curret=new StackOperat;curret->point=NULL;curret->operat='0';
q=new StackOperat;q->point=curret;curret=q;//按相反的顺序指
file://int num,count=0;
file://cout<<"要动态生成几个元素呢?";
char num;
cin>>num;
while(num!=';')
switch(num)
{
case'(':cout<<num<<" ";
curret->operat=num;q=new StackOperat;q->point=curret;curret=q;cin>>num;break;
case'+':case'-':cout<<num<<" ";
if(b==false)while(curret->point->operat!='(')
c=calcu(curret->point,next->point);
else if(b==true)while(curret->operat!='(')
c=calcu(curret,next);else
curret->operat=num;q=new StackOperat;q->point=curret;curret=q;cin>>num;break;
case'*':case'/':cout<<num<<" ";
if(curret->operat=='*'||curret->operat=='/')
c=calcu(curret->point,next->point);
curret->operat=num;q=new StackOperat;q->point=curret;curret=q;cin>>num;break;
case')':cout<<num<<" ";
while(curret->point->operat!='(')
c=calcu(curret->point,next->point);next=c->data;
curret=c->operat;q=curret->point;delete curret;curret=q;b=true;
cin>>num;break;
default:v=0;
do
{
v=10*v+num-'0';
cin>>num;
}while((num>='0')&&(num<='9'));
next->number=v;cout<<next->number<<" ";p=new StackData;p->point=next;next=p;
}
while(curret->operat!='0')c=calcu(curret->point,next->point);
cout<<next->number;
}
Container *calcu(StackOperat *p,StackData *q)
{
Container *c;
c=new Container;
int x1,x2,x;
StackData *op;
StackOperat *s;
char ch=p->operat;
s=p->point;delete p;p=s;
switch(ch)
{
case'+':x1=q->number;cout<<"x1:"<<x1;op=q->point;delete q;q=op;x2=q->number;cout<<"x2:"<<x2;op=q->point;delete q;q=op;x=x2+x1;break;
case'-':x1=q->number;cout<<"x1:"<<x1;op=q->point;delete q;q=op;x2=q->number;cout<<"x2:"<<x2;op=q->point;delete q;q=op;x=x2-x1;cout<<"q:"<<q->number;break;
case'*':x1=q->number;cout<<"x1:"<<x1;op=q->point;delete q;q=op;x2=q->number;cout<<"x2:"<<x2;op=q->point;delete q;q=op;x=x2*x1;break;
case'/':x1=q->number;cout<<"x1:"<<x1;op=q->point;delete q;q=op;x2=q->number;cout<<"x2:"<<x2;op=q->point;delete q;q=op;x=x2/x1;
}
op=new StackData;op->point=q;q=op;q->number=x;
c->data=q;cout<<"c->data->number:"<<c->data->number<<endl;
c->operat=p;cout<<"c->operat->operat:"<<c->operat->operat<<endl;
return c;
}
void main()
{
cout<<"请输入表达式:";
initStack();
}
如果有人运行下,就会知道错误在哪里了,我找了很厂时间都没找出啊!!!