64,637
社区成员
发帖
与我相关
我的任务
分享
// 定义在堆栈外面时运算符的优先级
int isp(char c) {
if (c == '+' || c =='-')
return 3;
else if (c == '*' || c == '/')
return 5;
else if (c == '(')
return 1;
else if (c == ')')
return 7;
else
return 0;
}
// 定义在堆栈外面时运算符的优先级
int osp(char c) {
if (c == '+' || c =='-')
return 2;
else if (c == '*' || c == '/')
return 4;
else if (c == '(')
return 7;
else if (c == ')')
return 1;
else
return 0;
}
//中缀表达式转后缀表达式
void InfixToPostfix()
{
char ch, y;
s.push('#');
while (cin>>ch,ch != '#') {
if (isdigit(ch) || isalpha(ch))
cout<<ch; //扫描到操作数直接输出
else if (ch == ')') //扫描到右括号时的处理
for (y = s.top(), s.pop(); y != '('; y = s.top(), s.pop())
cout<<ch;
else { //扫描到其它操作符时的处理
for (y = s.top(), s.pop(); osp(ch) < isp(y); y = s.top(), s.pop()) //弹出栈顶操作符
cout<<ch; //刚弹出的栈顶操作符的优先级高时输出
s.push(y); //当刚弹出的栈顶操作符的优先级低时,将其重新压回栈中
s.push(ch); //然后扫描到的操作符进栈
}
}
//输出栈中剩余操作符
while (!s.empty()){
y = s.top();
s.pop();
if (y != '#')
cout<<y;
}
}
//出错后清空
void Clear()
{
while(num.empty())
num.pop();
}
void PushOperand(double op)
{
num.push(op);
}
//获取操作数
bool GetOperands(double &op1, double &op2)
{
if (num.empty()){
cerr<<"Missing operand!"<<endl;
return false;
}
op1 = num.top();
num.pop();
if (num.empty()){
cerr<<"Missing operand!"<<endl;
return false;
}
op2 = num.top();
num.pop();
return true;
}
//处理操作符
void DoOperator(char oper)
{
bool result;
double oper1,oper2;
result = GetOperands(oper1,oper2); //从栈中弹出2个操作数
if (result)
switch(oper) //根据操作符做相应的运算,先出栈的操作数oper1
{ //放在操作符的右边,后出栈的oper2放在左边
case '+': num.push(oper2+oper1); break;
case '-': num.push(oper2-oper1); break;
case '*': num.push(oper2*oper1); break;
case '/':
if (fabs(oper1)<1e-6) { //如果分母为0,则做出错处理
cerr<<"Divide by 0!"<<endl;
Clear();
}
else
num.push(oper2/oper1); break;
case '^': num.push(pow(oper2,oper1)); break;
}
else Clear();
}
//计算表达式
void Run()
{
char c;
double newop;
while (cin>>c,c!='#') { //从输入流试读入一个字符,遇结束符结束
switch(c) { //读入的字符做如下处理
case '+':
case '-':
case '*':
case '/':
case '^': DoOperator(c); break; //是操作符则进行相应的计算
default: cin.putback(c); //如不是操作符,则将试读入的字符放回输入流
cin>>newop; //读入一个操作数
PushOperand(newop); break; //操作数进栈
}
}
}
#include <iostream>
#include <vector>
#include <stack>
#include <sstream>
#include <cmath>
using namespace std;
stack<double> num; //存放操作数
stack<char> s; //存放表达式转换过程中的变量
string expr;
int isp(char c) {
if (c == '+' || c =='-')
return 3;
else if (c == '*' || c == '/')
return 5;
else if (c == '(')
return 1;
else if (c == ')')
return 7;
else
return 0;
}
// 定义在堆栈外面时运算符的优先级
int osp(char c) {
if (c == '+' || c =='-')
return 2;
else if (c == '*' || c == '/')
return 4;
else if (c == '(')
return 7;
else if (c == ')')
return 1;
else
return 0;
}
//中缀表达式转后缀表达式
void InfixToPostfix()
{
char ch, y;
s.push('#');
while (cin>>ch,ch != '#') {
if (isdigit(ch) || isalpha(ch))
{
//cout<<ch; //扫描到操作数直接输出
expr.push_back(ch);
expr.push_back(' ');
}
else if (ch == ')') //扫描到右括号时的处理
for (y = s.top(), s.pop(); y != '('; y = s.top(), s.pop())
{
cout<<ch; //扫描到操作数直接输出
}
else { //扫描到其它操作符时的处理
for (y = s.top(), s.pop(); osp(ch) < isp(y); y = s.top(), s.pop()) //弹出栈顶操作符
{
cout<<ch; //扫描到操作数直接输出
} //刚弹出的栈顶操作符的优先级高时输出
s.push(y); //当刚弹出的栈顶操作符的优先级低时,将其重新压回栈中
s.push(ch); //然后扫描到的操作符进栈
}
}
//输出栈中剩余操作符
while (!s.empty()){
y = s.top();
s.pop();
if (y != '#')
{
//cout<<y;
expr.push_back(y);
expr.push_back(' ');
}
}
expr.push_back('#');
}
void Clear()
{
while(num.empty())
num.pop();
}
void PushOperand(double op)
{
num.push(op);
}
//获取操作数
bool GetOperands(double &op1, double &op2)
{
if (num.empty()){
cerr<<"Missing operand!"<<endl;
return false;
}
op1 = num.top();
num.pop();
if (num.empty()){
cerr<<"Missing operand!"<<endl;
return false;
}
op2 = num.top();
num.pop();
return true;
}
//处理操作符
void DoOperator(char oper)
{
bool result;
double oper1,oper2;
result = GetOperands(oper1,oper2); //从栈中弹出2个操作数
if (result)
switch(oper) //根据操作符做相应的运算,先出栈的操作数oper1
{ //放在操作符的右边,后出栈的oper2放在左边
case '+': num.push(oper2+oper1); break;
case '-': num.push(oper2-oper1); break;
case '*': num.push(oper2*oper1); break;
case '/':
if (fabs(oper1)<1e-6) { //如果分母为0,则做出错处理
cerr<<"Divide by 0!"<<endl;
Clear();
}
else
num.push(oper2/oper1); break;
case '^': num.push(pow(oper2,oper1)); break;
}
else Clear();
}
//计算表达式
void Run()
{
char c;
double newop;
stringstream ss(expr);
while (ss>>c,c!='#') { //从输入流试读入一个字符,遇结束符结束
switch(c) { //读入的字符做如下处理
case '+':
case '-':
case '*':
case '/':
case '^': DoOperator(c); break; //是操作符则进行相应的计算
default: ss.putback(c); //如不是操作符,则将试读入的字符放回输入流
ss>>newop; //读入一个操作数
PushOperand(newop); break; //操作数进栈
}
}
}
int main(void)
{
InfixToPostfix();
Run();
cout<<num.top()<<endl;
system("pause");
return 0;
}