标题:请教一个多项式问题
只看楼主
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
 问题点数:0 回复次数:0 
请教一个多项式问题

我想把(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();
}
如果有人运行下,就会知道错误在哪里了,我找了很厂时间都没找出啊!!!

搜索更多相关主题的帖子: 多项式 StackData StackOperat public Container 
2006-09-24 16:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-92645-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.175056 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved