主题:急!!一个问了很多人都不知道那里错了的程序 程序虽多请有耐心的高手指点
#include<string.h>
#include<cmath>
#include <stdio.h>
#include<iostream.h>
const int maxsize=100;
class seqstack
{ public :
char stack[maxsize];
int top;
void Inistack();
void Push(char x);
void Push(double x);//重载
void Pop(char &x);
void Pop(double &x);
void Gettop(char &x);
void Gettop(double &x);
bool Empty();
};
void seqstack::Inistack()
{ top=0; }
void seqstack::Push(char x)
{ if(top==maxsize) cout<<"overflow";
else stack[top++]=x;
}
void seqstack::Push(double x)
{ if(top==maxsize) cout<<"overflow";
else stack[top++]=x;
}
void seqstack::Pop(char &x)
{
if(top!=0) x=top--;
}
void seqstack::Pop(double &x)
{
if(top!=0) x=top--;
}
void seqstack::Gettop(char &x)
{
x = stack[top];
}void seqstack::Gettop(double &x)
{
x = stack[top];
}
bool seqstack::Empty()
{ if(top==0) return true;
else return false ;
}
char Procede(char t1,char t2 )//判断符号优先关系
{ char f;
switch(t2)
{
case'+':
case'-': if(t1=='('||t1=='=')
f='<';
else f= '>';
case'*':
case'/': if(t1=='*'||t1=='/'||t1==')')
f= '>';
else f= '<';
case'('://if(t1==')')f= 0;
//else
f='<';
case')':switch(t1)
{
case'(': f='=';
//case'=':cout<<"ERROR"<<endl;f= 0;
default: f= '>';
}
case'=':switch(t1)
{
case'=':f= '=';
//case'(':cout<<"ERROR"<<endl;f= 0;
default:f= '>';
}
}
return f;
}
bool In(char c)//判断是否为运算符
{ switch(c)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'=': return true;
default: return false;
}
}
double Operator(double a,char theta,double b)//计算
{
double c;
switch(theta)
{ case'+':c=a+b;break;
case'-':c=a-b;break;
case'*':c=a*b;break;
case'/':
if(b==0)
cout<<"0 can't be divid!"; break;
c=a/b;break;//
}
return c;
}
double EvaluateExpression()//设置两个栈 ,并输入
{ seqstack OPTR ,OPND;
char c,x1,theta,array[10]={'0'};double a,b,e=0,x2;
OPTR.Inistack();
//OPTR.Push('=');
OPND.Inistack();
c=getchar();
OPTR.Gettop(x1);
while(c!='='||x1!='=')
{
if(In(c))
switch(Procede(x1,c))
{
case'<': OPTR.Push(c);
c=getchar();
break;
case'=': OPTR.Pop(x1);
c=getchar();
break;
case'>': OPTR.Pop(theta);
OPND.Pop(b);
OPND.Pop(a);
OPND.Push(Operator(a,theta,b));
break;
}
else if(c>='0'&&c<='9')//字符串转为数字
{ int i=0;
do{
array[i]=c;
i++;
c=getchar();
}while(c>='0'&&c<='9');
array[i]=0;
for(int j=0;j<i;j++)
e+=(array[j]-'0')*pow(10,i-j-1);
//e=chang(array);
OPND.Push(e);
}
else {cout<<"非法字符"<<endl; break;}
OPTR.Gettop(x1);
}
OPND.Gettop(x2);
return x2;
}
void main()
{
cout<<"请输入算术表达式,并以=结束"<<endl;
cout<<"计算结果为:"<<EvaluateExpression()<<endl;
}