利用逆波兰表达式(后缀表达式),利用两个栈(算符以及数字)栈之间进行表达式的计算。由于此算法是以前写的,现在仅贴上一些程序,具体思想一般数据结构书都有介绍。
例子:
Input A Expression To Caulate...
Input A * To Exit...
9-(4-2)+9/(5-2)=
The Caulate Result Is : 10
Press any key to continue
源程序:cpp文件
#include"Expression.h"
/*******利用栈求解表达式的值**************/
/*****2006-12-13**************************/
void main()
{
//求解表达式程序如下--------------------------------------------
vector<char> expression;
double result;
Input(expression);
result = calu(expression);
Output(result);
}
头文件:Expression.h
#include"iStack.h"
#include<vector>
using namespace ZBH_Stack;
void Input(vector<char> &ch);
double calu(const vector<char> &ch);
void Output(const double &a);
int Priority(const char &ch1,const char &ch2);
double Operator(double d1,double d2,char ch);
bool check(const vector<char> &ch);
void Input(vector<char> &ch) //输入函数
{
char temp[15];
int i = 0;
int size = 0;
cout<<"Input A Expression To Caulate..."<<endl;
cout<<"Input A * To Exit..."<<endl;
do //输入字符串
{
cin>>temp[i];
i++;
size = i;
}while(temp[i-1]!='=');
int pass = 0; //中间存贮变量
for(i = 0;i<size;i++)
{
if(temp[i]<48) //添加运算符
{
ch.push_back(temp[i]);
}
else
{
if(temp[i+1]<48||temp[i+1] == '=') //添加数字
{
pass = pass*10+temp[i]-48;
ch.push_back(pass+48);
pass = 0;
}
else
{
pass = pass*10+temp[i]-48; //递加
}
}
}
ch.pop_back();
}
//bool check(const vector<char> &ch)
void Output(const double &a)
{
cout<<"The Caulate Result Is : "<<a<<endl;
}
//ch1 not in stack,ch2 top of the stack;
//return 1 to say ch1<ch2,
//return 0 to say ch1=ch2,
//return -1 to say ch1>ch2,
int Priority(const char &ch1,const char &ch2) //运算符优先级函数
{
if(ch2 == '#'||ch1 == '('||ch2 == '(')
return 0;
if((ch1 == '+'||ch1 == '-')&&(ch2 == '*'||ch2 == '/'))
return 1;
if((ch1 == '*'||ch1 == '/')&&(ch2 == '+'||ch2 == '-'))
return 0;
if((ch1 == '*'||ch1 == '/')&&(ch2 == '*'||ch2 == '/'))
return 1;
if((ch1 == '+'||ch1 == '-')&&(ch2 == '+'||ch2 == '-'))
return 1;
if(ch1 == ')')
return 1;
}
double Operator(double d1,double d2,char ch) //计算函数
{
switch (ch)
{
case '+':return d1+d2;
break;
case '-':return d1-d2;
break;
case '*':return d1*d2;
break;
case '/':return d1/d2;
break;
default :return 0;
}
}
double calu(const vector<char> &ch)
{
int size = ch.size();
char stack_peek;
int Doit; //所进行的入栈出栈操作
double dTemp,dResult; //进行运算时的一个参数
iStack<char> char_stack('#');
iStack<double> double_stack(10000);
vector<char>::const_iterator it = ch.begin();
while(it != ch.end())
{
if((*it) < 48) //is a char
{
stack_peek = char_stack.Peek();
Doit = Priority((*it),stack_peek); //对运算符或数字进行处理
switch (Doit)
{
case 0: char_stack.Push((*it)); //新加进来的优先级高,进栈
break;
// case 0: char_stack.Push((*it)); //新加进来的优先级,进栈
// break;
case 1: dTemp = double_stack.Peek(); //加进来的优先级低,相等,需要先运算
double_stack.Pop();
dResult = Operator(double_stack.Peek(),dTemp,char_stack.Peek());
double_stack.Pop(); //出栈后重新赋值入栈
double_stack.Push(dResult);
char_stack.Pop(); //运算符出栈
if((*it) != ')')
{
char_stack.Push((*it));
}
break;
}
if(((*it) == ')')&&(char_stack.Peek() !='('))
{
//nothing to do
}
else
{
if(((*it) == ')')&&(char_stack.Peek() == '('))
{
char_stack.Pop();
}
it++;
}
}
else if((*it) >= 48)
{
double_stack.Push((*it)-48);
it++;
}
}
while(char_stack.Peek() != '#') //所有字符都处理完,留下栈内的字符处理
{
dTemp = double_stack.Peek(); //加进来的优先级低,相等,需要先运算
double_stack.Pop();
dResult = Operator(double_stack.Peek(),dTemp,char_stack.Peek());//实行运算
double_stack.Pop(); //出栈后重新赋值入栈
double_stack.Push(dResult);
char_stack.Pop(); //运算符出栈
}
return double_stack.Peek();
}
栈类的头文件略,一般都会有介绍