中缀表达式变后缀表达式并求值
2014-12-01 22:56
197 查看
//#include "stdafx.h"
#include <iostream>
#include <stack>//包含栈类
#include <string>
using namespace std;
int prior(char op)//判断运算符号优先级
{
if (op == '+' || op == '-')//+ -优先级为1
return 1;
if (op == '*' || op == '/')//* /优先级为2
return 2;
return 0;
}
string TransForm(string infix)//转换中缀为后缀
{
stack<char> op;//中转栈 用于存放符号
string ans;//存放后缀表达式
for (unsigned int i = 0; i<infix.size(); i++)
{
char c = infix[i];
if (!((c >= 'A'&&c <= 'Z') || c == '(' || c == ')' || c == '*' || c == '/' || c == '+' || c == '-')) throw c;//异常处理
if (c >= 'A'&&c <= 'Z')//如果是字母直接入栈
{
ans.append(1, c);
}
else
{
if (c == '(')//如果为左括弧入栈
op.push('(');
else
{
if (c == ')')//右括弧出栈 直到遇到左括弧为止
{
while (op.top() != '(')
{
ans.append(1, op.top());//栈顶元素写入ans
op.pop();
}
op.pop();
}
else//如果是+-*/符号
{
if (op.empty())//若栈空直接进栈
{
op.push(c);
}
else//不空
{
if (prior(c)>prior(op.top()))//优先级大直接进栈
op.push(c);
else
{
while (!op.empty() && prior(c) <= prior(op.top()))//直到找到优先级大于等于当前运算符的元素或者栈空
{
ans.append(1, op.top());//将栈顶元素写入ans
op.pop();//出栈
}
op.push(c);//将c入栈
}
}
}
}
}
}
while (!op.empty())
{
ans.append(1, op.top());
op.pop();
}
return ans;
}
double calculater(string postfix)//计算转化得到的表达式
{
stack<double> cal;
int j(0);
double left, right;
double a;
for (unsigned int i = 0; i < postfix.length(); i++)
{
if (postfix[i] <= 'Z'&&postfix[i] >= 'A')
{
cout << "请输入变量" << (char)(postfix[i]) << "的值" << endl;
cin >> a;
if (cin.fail()) throw j;
if(postfix[i]=='('||postfix[i]==')') throw a;
cal.push(a);
j++;
}
else
{
right = cal.top();
cal.pop();
left = cal.top();
cal.pop();
if (postfix[i] == '+' )
cal.push(left + right);
if (postfix[i] == '-')
cal.push(left - right);
if (postfix[i] == '*')
cal.push(left * right);
if (postfix[i] == '/')
cal.push(left / right);
}
}
return cal.top();
}
int main()
{
double result;
string infix, postfix;
char c;
do{
try{
cout << "请输入中缀表达式,大写字母表示变量:" << endl;
cin >> infix;
postfix = TransForm(infix);
cout << "后缀表达式为:" << postfix << endl;
result = calculater(postfix);
cout << "表达式的值为:" << result << endl;
}
catch (char){ cout << "输入有误" << endl; }
catch (int){ cout << "请输入数字谢谢!" << endl; }
catch (double){cout<<"表达式有误"<<endl;}
cout<<"是否继续使用?y/n"<<endl;
cin>>c;
}while(c=='y');
return 0;
}
#include <iostream>
#include <stack>//包含栈类
#include <string>
using namespace std;
int prior(char op)//判断运算符号优先级
{
if (op == '+' || op == '-')//+ -优先级为1
return 1;
if (op == '*' || op == '/')//* /优先级为2
return 2;
return 0;
}
string TransForm(string infix)//转换中缀为后缀
{
stack<char> op;//中转栈 用于存放符号
string ans;//存放后缀表达式
for (unsigned int i = 0; i<infix.size(); i++)
{
char c = infix[i];
if (!((c >= 'A'&&c <= 'Z') || c == '(' || c == ')' || c == '*' || c == '/' || c == '+' || c == '-')) throw c;//异常处理
if (c >= 'A'&&c <= 'Z')//如果是字母直接入栈
{
ans.append(1, c);
}
else
{
if (c == '(')//如果为左括弧入栈
op.push('(');
else
{
if (c == ')')//右括弧出栈 直到遇到左括弧为止
{
while (op.top() != '(')
{
ans.append(1, op.top());//栈顶元素写入ans
op.pop();
}
op.pop();
}
else//如果是+-*/符号
{
if (op.empty())//若栈空直接进栈
{
op.push(c);
}
else//不空
{
if (prior(c)>prior(op.top()))//优先级大直接进栈
op.push(c);
else
{
while (!op.empty() && prior(c) <= prior(op.top()))//直到找到优先级大于等于当前运算符的元素或者栈空
{
ans.append(1, op.top());//将栈顶元素写入ans
op.pop();//出栈
}
op.push(c);//将c入栈
}
}
}
}
}
}
while (!op.empty())
{
ans.append(1, op.top());
op.pop();
}
return ans;
}
double calculater(string postfix)//计算转化得到的表达式
{
stack<double> cal;
int j(0);
double left, right;
double a;
for (unsigned int i = 0; i < postfix.length(); i++)
{
if (postfix[i] <= 'Z'&&postfix[i] >= 'A')
{
cout << "请输入变量" << (char)(postfix[i]) << "的值" << endl;
cin >> a;
if (cin.fail()) throw j;
if(postfix[i]=='('||postfix[i]==')') throw a;
cal.push(a);
j++;
}
else
{
right = cal.top();
cal.pop();
left = cal.top();
cal.pop();
if (postfix[i] == '+' )
cal.push(left + right);
if (postfix[i] == '-')
cal.push(left - right);
if (postfix[i] == '*')
cal.push(left * right);
if (postfix[i] == '/')
cal.push(left / right);
}
}
return cal.top();
}
int main()
{
double result;
string infix, postfix;
char c;
do{
try{
cout << "请输入中缀表达式,大写字母表示变量:" << endl;
cin >> infix;
postfix = TransForm(infix);
cout << "后缀表达式为:" << postfix << endl;
result = calculater(postfix);
cout << "表达式的值为:" << result << endl;
}
catch (char){ cout << "输入有误" << endl; }
catch (int){ cout << "请输入数字谢谢!" << endl; }
catch (double){cout<<"表达式有误"<<endl;}
cout<<"是否继续使用?y/n"<<endl;
cin>>c;
}while(c=='y');
return 0;
}
相关文章推荐
- 中缀表达式转换成后缀表达式并求值
- 中缀表达式求后缀,再由后缀表达式求值
- 栈的应用--算术表达式的求值(中缀转后缀然后计算后缀表达式的值)
- 中缀表达式转换成后缀表达式并求值
- NYOJ 35 表达式求值 【中缀转后缀+后缀表达式求值】
- 算术表达式求值(中缀转后缀,后缀求值,java 栈实现)
- 中缀表达式转换为后缀表达式(栈的使用)
- 前缀、中缀、后缀表达式实现表达式的编程
- 中缀转后缀 和 后缀表达式求值
- 中缀表达式转后缀表达式
- 中缀表达式求值
- 中缀表达式变后缀表达式
- 表达式求值(后缀表达式)--栈
- 表达式求值(中缀变后缀再求值)
- 表达式求值(逆波兰式后缀表达式)
- 栈---定义、应用(递归、后缀表达式实现数学表达式求值)
- 栈的应用 后缀表达式求值 后缀表达式与中缀表达式的转换
- 中缀转后缀及表达式求值
- 表达式求值(中缀转后缀及后缀表达式求值)
- 前缀、中缀、后缀表达式(逆波兰表达式)