数据结构:C语言描述栈的应用——算符优先法求中缀表达式的值
1个回答
2008-01-22
展开全部
#include"iostream.h"
#include"math.h"
#include"stdlib.h"
class Calculator
{
public:
//接口方法声明
void Run();
private:
//辅助函数
bool IsOperator(char ch);//判断字符ch是否为操作符
char Precede(char theta1,char theta2);//判断相继出现的theta1和theta2的优先级
double Operate(double left,char theta,double right,int[] &a);//执行运算letf theta right
void Get2Operands(LinkStack<double>&opnd,double &left,double &right);
//从栈opnd中退出两个操作数
};
void Calculator::Get2Operands(LinkStack <double>&opnd,double &left,double &right)
{
}
bool Calculator:: IsOperator(char ch)
{
char ch;
cin>>ch;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'ch=='('ch==')'||ch=='=')
return true;
else
return false;
}
char Calculator:: Operate(double left,char theta,double right)
{ double result;
switch(theta)
{
case'+':result=left+right;break;
case'-':result=left-right;break;
case'*':result=left*right;break;
case'/':result=left/right;break;
}
return result;
}
char Calculator:: Precede(char theta1,char theta2)
{
char ch;
switch(theta1)
{
case'+':
case'-':
{
switch (theta2)
{
case'+':
case'-':
case')':
case'=':
ch='>';
break;
case'*':
case'/':
case'(':
ch='<';
break;
}
break;
}
case'*':
case'/':
{
if(theta2=='(')
ch='<';
else
ch='>';
break;
}
case'(':
{
if(theta2==')')
ch='=';
else ch='<';
break;
}
case')':
{
ch='>';
break;
}
case'=':
{
if(theta2=='=')
ch='=';
else
ch='<';
break;
}
}
return ch;
}
//方法Run()算法实现如下
void Calculator::Run()
{//操作结果:按算符优先法进行表达式求值计算
LinkStack<double> opnd; //操作数栈
LinkStack<char>optr; //操作符栈
optr.push('='); //在optr栈中加入一个'='
char ch; //临时字符
char optrTop; //临时optr栈栈顶字符
double operand; //操作数
char theta; //操作符
cin>>ch; //从输入流获取一字符ch
while((optr.Top(optrTop),optrTop!='=')||ch!='=')
{
if(!IsOperator(ch))
{//ch不是操作字符
cin.putback(ch); //将字符放ch回输入流
cin>>operand; //读操作数operand
opnd.Push(operand); //进入opnd栈
cin>>ch; //读入下一字符ch
}
else
{//ch是操作符
switch(Precede(optrTop,ch))
{
case'<':
optr.Push(ch);
cin>>ch;
break;
case'=':
optr.Pop(optr Top);
cin>>ch;
break;
case'>':
double left,right;
Get2Operands(opnd,left,right);
optr.Pop(theta);
opnd.Push(Operate(left,theta,right));
break;
case'e':
cout<<"操作符匹配出错"<<endl;
exit(2);
}
}
}
opnd.Top(operand);
cout<<"表达式值为:"<<operand<<endl;
}
void main(void)
{
system("pause");
return 0;
}
#include"math.h"
#include"stdlib.h"
class Calculator
{
public:
//接口方法声明
void Run();
private:
//辅助函数
bool IsOperator(char ch);//判断字符ch是否为操作符
char Precede(char theta1,char theta2);//判断相继出现的theta1和theta2的优先级
double Operate(double left,char theta,double right,int[] &a);//执行运算letf theta right
void Get2Operands(LinkStack<double>&opnd,double &left,double &right);
//从栈opnd中退出两个操作数
};
void Calculator::Get2Operands(LinkStack <double>&opnd,double &left,double &right)
{
}
bool Calculator:: IsOperator(char ch)
{
char ch;
cin>>ch;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'ch=='('ch==')'||ch=='=')
return true;
else
return false;
}
char Calculator:: Operate(double left,char theta,double right)
{ double result;
switch(theta)
{
case'+':result=left+right;break;
case'-':result=left-right;break;
case'*':result=left*right;break;
case'/':result=left/right;break;
}
return result;
}
char Calculator:: Precede(char theta1,char theta2)
{
char ch;
switch(theta1)
{
case'+':
case'-':
{
switch (theta2)
{
case'+':
case'-':
case')':
case'=':
ch='>';
break;
case'*':
case'/':
case'(':
ch='<';
break;
}
break;
}
case'*':
case'/':
{
if(theta2=='(')
ch='<';
else
ch='>';
break;
}
case'(':
{
if(theta2==')')
ch='=';
else ch='<';
break;
}
case')':
{
ch='>';
break;
}
case'=':
{
if(theta2=='=')
ch='=';
else
ch='<';
break;
}
}
return ch;
}
//方法Run()算法实现如下
void Calculator::Run()
{//操作结果:按算符优先法进行表达式求值计算
LinkStack<double> opnd; //操作数栈
LinkStack<char>optr; //操作符栈
optr.push('='); //在optr栈中加入一个'='
char ch; //临时字符
char optrTop; //临时optr栈栈顶字符
double operand; //操作数
char theta; //操作符
cin>>ch; //从输入流获取一字符ch
while((optr.Top(optrTop),optrTop!='=')||ch!='=')
{
if(!IsOperator(ch))
{//ch不是操作字符
cin.putback(ch); //将字符放ch回输入流
cin>>operand; //读操作数operand
opnd.Push(operand); //进入opnd栈
cin>>ch; //读入下一字符ch
}
else
{//ch是操作符
switch(Precede(optrTop,ch))
{
case'<':
optr.Push(ch);
cin>>ch;
break;
case'=':
optr.Pop(optr Top);
cin>>ch;
break;
case'>':
double left,right;
Get2Operands(opnd,left,right);
optr.Pop(theta);
opnd.Push(Operate(left,theta,right));
break;
case'e':
cout<<"操作符匹配出错"<<endl;
exit(2);
}
}
}
opnd.Top(operand);
cout<<"表达式值为:"<<operand<<endl;
}
void main(void)
{
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询