主题:恳请高人指点 急!!!小弟这个程序编译没有错 但就是出不了结果
小弟这个程序编译没有错 但就是出不了结果是利用栈来计算四则运算 的
#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;
}
/*int strchang(char c)//字符装换为数字
{
int n;
switch(c)
{
case '0': n=0;break;
case '1': n=1;break;
case '2': n=2;break;
case '3': n=3;break;
case '4': n=4;break;
case '5': n=5;break;
case '6': n=6;break;
case '7': n=7;break;
case '8': n=8;break;
case '9': n=9;break;
} n=c-'0';
return n;
}
int chang(char array[])//字符串转为数字
{
int len=strlen(array),i, d=0;//int h;
for( i=0;i<=len;i++)
{ //h=strchang(array[i]);
d+=(array[i]-'0')*pow(10,len-i-2);
}
return d;
}*/
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;
}
#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;
}
/*int strchang(char c)//字符装换为数字
{
int n;
switch(c)
{
case '0': n=0;break;
case '1': n=1;break;
case '2': n=2;break;
case '3': n=3;break;
case '4': n=4;break;
case '5': n=5;break;
case '6': n=6;break;
case '7': n=7;break;
case '8': n=8;break;
case '9': n=9;break;
} n=c-'0';
return n;
}
int chang(char array[])//字符串转为数字
{
int len=strlen(array),i, d=0;//int h;
for( i=0;i<=len;i++)
{ //h=strchang(array[i]);
d+=(array[i]-'0')*pow(10,len-i-2);
}
return d;
}*/
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;
}