关于严蔚敏数据结构中的表达式计算的程序的编纂
关于严蔚敏数据结构中的表达式计算的程序的编写
就是要写一个以栈为基础的 能直接运算表达式的程序 我写完之后 发现一位数的运算是可以了
有一些两位数三位数运算也可以了 但是有一些两位数三位数运算就不可以
我就觉得很奇怪 对着代码看了两天了都不知道为什么会这样
现在只能求助于大家了 代码有点乱 谢谢大家啊~
------解决思路----------------------
char Operate(int a,char b,int c)
返回值应该是int吧
int Operate(int a,char b,int c)
就是要写一个以栈为基础的 能直接运算表达式的程序 我写完之后 发现一位数的运算是可以了
有一些两位数三位数运算也可以了 但是有一些两位数三位数运算就不可以
我就觉得很奇怪 对着代码看了两天了都不知道为什么会这样
现在只能求助于大家了 代码有点乱 谢谢大家啊~
#include <iostream>
#include <math.h>
using namespace std;
#define MAXNUM 50
class Stack_for_number
{
private:
int num[MAXNUM];
int top;
public:
Stack_for_number(){top=0;}
int push(int);
int pop(void);
int get_top();
};//运算数栈
class Stack_for_operator
{
private:
char op[MAXNUM];
int top;
public:
Stack_for_operator(){op[0]='#';top=1;}
char push(char);
char pop(void);
char get_top();
};//运算符栈
char Operate(int a,char b,int c)
{
switch(b)
{
case'+':{return a+c; break;}
case'-':{return a-c; break;}
case'*':{return a*c; break;}
case'/':{return a/c; break;}
default:break;
}
}
char Precede(char a,char b)
{
switch(a)
{
case'+':
{
if(b=='+'||b=='-'||b==')'||b=='#')
return '>';
else return '<';
break;
}
case'-':
{
if(b=='+'||b=='-'||b==')'||b=='#')
return '>';
else return '<';
break;
}
case'*':
{
if(b=='(')
return '<';
else return '>';
break;
}
case'/':
{
if(b=='(')
return '<';
else return '>';
break;
}
case'(':
{
if(b==')')
return '=';
else return '<';
break;
}
case')':
{
return '>';
break;
}
case'#':
{
if(b=='#')
return '=';
else return '<';
break;
}
}
}//运算符优先级比较
int CalculateExpression(Stack_for_number OPND,Stack_for_operator OPTR)
{
char c=getchar();
int n[MAXNUM];
int i=0,j=0,m=0,k=0,tmp=1;
while(c!='#'||OPTR.get_top()!='#')
{ if(c!='+'&&c!='-'&&c!='*'&&c!='/'&&c!='#'&&c!='('&&c!=')')
{
while(c!='+'&&c!='-'&&c!='*'&&c!='/'&&c!='#'&&c!='('&&c!=')')
{n[i]=int(c)-48;i++;c=getchar();}
for(j=0;j<i;j++)
{ int ii;
for(ii = 0; ii < i-j-1; ii++)
{tmp *= 10;}
k=n[j]*tmp;
m=m+k;
k=0;
}
OPND.push(m);
m=0;
i=0; //i初始化
tmp=1;
} //if
else switch(Precede(OPTR.get_top(),c))
{
case'<':
OPTR.push(c);
c=getchar();
break;
case'=':
OPTR.pop();
c=getchar();
break;
case'>':
int x=OPND.pop();int y=OPND.pop();
OPND.push(Operate(y,OPTR.pop(),x));
break;
} //switch
} //while
return OPND.get_top();
}
int Stack_for_number::push(int e)
{
if(top==MAXNUM)
{
cout<<"The Stack_for_number is full"<<endl;
return 0;
}
num[top]=e;
top++;
}
int Stack_for_number::pop(void)
{
if(top<0)
{
cout<<"The Stack_for_number is empty"<<endl;
}
top--;
return num[top];
}
char Stack_for_operator::push(char e)
{
if(top==MAXNUM)
{
cout<<"The Stack_for_operator is full"<<endl;
return 0;
}
op[top]=e;
top++;
}
char Stack_for_operator::get_top()
{
if(top<1)
{cout<<"The Stack_for_operator is empty";}
else return op[top-1];
}
int Stack_for_number::get_top()
{
return num[top-1];
}
char Stack_for_operator::pop(void)
{
top--;
if(top<0)
{
cout<<"The Stack_for_operator is empty"<<endl;
}
return op[top];
}
int main()
{
Stack_for_number OPND;
Stack_for_operator OPTR;
cout<<CalculateExpression(OPND,OPTR)<<endl;
}
------解决思路----------------------
char Operate(int a,char b,int c)
返回值应该是int吧
int Operate(int a,char b,int c)