标题:中缀表达式变成后缀表达式并求值
只看楼主
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
 问题点数:0 回复次数:35 
中缀表达式变成后缀表达式并求值

//以下程序更新了以前的程序,添加了后缀表达式运算的过程。

#include <iostream>
#include <string>
#include <stack>
#include <vector>

namespace{

class Expression
{
public:
Expression();
static void Remove_space(std::string &Translated);
static int Compare(char op);
static void RPN(std::string expression);
static int Clculate();
static void Display(const std::vector<int> &opnd, const std::string &soper);

static std::vector<std::string> sResult;
static std::stack<int> snum;
};
std::vector<std::string> Expression::sResult;
std::stack<int> Expression::snum;

Expression::Expression()
{
sResult.reserve(100);
}

void Expression::Remove_space(std::string &Translate)
{
std::string::size_type position_start = 0;
std::string::size_type position_end = 0;

std::string Result;
Result.reserve(100);
while( (position_end = Translate.find_first_of(' ',position_start)) != std::string::npos)
{
std::string temp = Translate.substr(position_start,position_end - position_start);
Result += temp;
position_start = ++position_end;
}
Result += Translate.substr(position_start,position_end - position_start);
Translate = Result;
}

int Expression::Compare(char op)
{
int Level = 0;
switch(op)
{
case '+':case '-':
Level = 1;
break;
case '*':case '/':
Level = 2;
break;
default:
break;
}
return Level;
}

void Expression::RPN(std::string expression)
{
std::string::size_type nCount = expression.length();
std::string::size_type index = 0;
std::stack<char> stmp;
bool flag = false;

stmp.push('@');
while( index < nCount)
{
if( expression[index] == '(')
{
stmp.push('(');
index++;
}
else if( expression[index] == ')')
{
while( stmp.top() != '(')
{
sResult.push_back( std::string(1, stmp.top()) );
stmp.pop();
}
stmp.pop();
index++;
}
else if( expression[index] == '+' || expression[index] == '-' ||
expression[index] == '*' || expression[index] == '/' )
{
if( expression[index] == '-' || expression[index] == '+' )
{
if(index == 0)//如果第一个数字是负数
{
index ++;
flag = true;
}
else if( expression[index-1] == '(' )//如果括号里第一个数是负数
{
index++;
flag = true;
}
else
{
while( Compare( expression[index] ) <= Compare( stmp.top() ))
{
sResult.push_back( std::string(1, stmp.top()) );
stmp.pop();
}

stmp.push( expression[index] );
index++;
}

}
else
{
while( Compare( expression[index] ) <= Compare( stmp.top() ))
{
sResult.push_back( std::string(1, stmp.top()) );
stmp.pop();
}
stmp.push( expression[index] );
index++;
}
}
else
{
std::string temp;
if(flag == true)
{
temp += expression[index - 1];
flag = false;
}
while( expression[index] >= '0' && expression[index] <= '9')
{
temp += expression[index];
index ++;
}
sResult.push_back( temp );
}
}//while

while(stmp.top() != '@')
{
if(stmp.top() == '(')
{
std::cout << "Error in expression" << std::endl;
exit(-1);
}
sResult.push_back( std::string(1, stmp.top()) );
stmp.pop();
}

}//RPN

int Expression::Clculate()
{
std::vector<int> opnd;

size_t index = 0;

int num1 = 0, num2 = 0;

std::cout<< "OPND栈" << "\t\t" <<"OPTR栈" << "\t\t" <<"输入字符"<<"\t"<<"主要操作"<<std::endl;

for(index = 0; index < sResult.size(); ++index)
{
std::string oper = sResult[index];
if( (oper.size()>1) && (oper[0] == '+' || oper[0] == '-') )
{
snum.push( atoi(sResult[index].c_str()) );
opnd.push_back(atoi(sResult[index].c_str()));
}
else
{
switch(oper[0])
{
case '+':
{
num1 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num1 << "\t\t" << num1<<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
num2 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num2 << "\t\t" << num2 <<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
snum.push( num2 + num1);
opnd.push_back(num2 + num1);
Display(opnd, " ");
std::cout<< "+" << "\t\t" << "计算" << num2 << "+" << num1 << "并将结果压OPND栈";
std::cout<< std::endl;

}
break;
case '-':
{
num1 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num1 << "\t\t" << num1<<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
num2 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num2 << "\t\t" << num2 <<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
snum.push( num2 - num1);
opnd.push_back(num2 - num1);
Display(opnd, " ");
std::cout<< "+" << "\t\t" << "计算" << num2 << "-" << num1 << "并将结果压OPND栈";
std::cout<< std::endl;

}
break;
case '*':
{
num1 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num1 << "\t\t" << num1<<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
num2 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num2 << "\t\t" << num2 <<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
snum.push( num2 * num1);
opnd.push_back(num2 * num1);
Display(opnd, " ");
std::cout<< "+" << "\t\t" << "计算" << num2 << "*" << num1 << "并将结果压OPND栈";
std::cout<< std::endl;

}
break;
case '/':
{
num1 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num1 << "\t\t" << num1<<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
num2 = snum.top();
Display(opnd, sResult[index]);
std::cout<< num2 << "\t\t" << num2 <<" 出OPND栈";
std::cout<< std::endl;
snum.pop();
opnd.pop_back();
if(num1 == 0)
{
std::cerr << "Expression worng!" << std::endl;
exit(-1);
}
snum.push( num2 / num1);
opnd.push_back(num2 / num1);
Display(opnd, " ");
std::cout<< "+" << "\t\t" << "计算" << num2 << "*" << num1 << "并将结果压OPND栈";
std::cout<< std::endl;

}
break;
default:
{
snum.push( atoi(sResult[index].c_str()) );
opnd.push_back( atoi(sResult[index].c_str()) );
}
break;
}//swith
}
}//while

int result = snum.top();
snum.pop();
return result;
}

void Expression::Display(const std::vector<int> &opnd, const std::string &soper)
{
size_t index = 0;
for(index; index< opnd.size(); index++)
{
std::cout << opnd[index] << " ";
}
std::cout<< "\t\t" << soper << "\t\t";
}

}//class

int main()
{

std::string Input;

std::cout << "please input the expression (Enter key for end)" << std::endl;
std::getline(std::cin, Input, '\n');

Expression expression;
Expression::Remove_space(Input);
Expression::RPN(Input);

std::cout << std::endl;
std::cout << "后缀表达式:" << std::endl;

for(size_t index = 0; index < Expression::sResult.size(); ++index)
{
std::cout << Expression::sResult[index] << " ";
}

std::cout << std::endl;

std::cout<< Expression::Clculate() << std::endl;
return 0;
}


please input the expression (Enter key for end)
11-(22-11)*2-(-10)+10/5

后缀表达式:
11 22 11 - 2 * - -10 - 10 5 / +
OPND栈 OPTR栈 输入字符 主要操作
11 22 11 - 11 11 出OPND栈
11 22 - 22 22 出OPND栈
11 11 + 计算22-11并将结果压OPND栈
11 11 2 * 2 2 出OPND栈
11 11 * 11 11 出OPND栈
11 22 + 计算11*2并将结果压OPND栈
11 22 - 22 22 出OPND栈
11 - 11 11 出OPND栈
-11 + 计算11-22并将结果压OPND栈
-11 -10 - -10 -10 出OPND栈
-11 - -11 -11 出OPND栈
-1 + 计算-11--10并将结果压OPND栈
-1 10 5 / 5 5 出OPND栈
-1 10 / 10 10 出OPND栈
-1 2 + 计算10*5并将结果压OPND栈
-1 2 + 2 2 出OPND栈
-1 + -1 -1 出OPND栈
1 + 计算-1+2并将结果压OPND栈
1
Press any key to continu



q0FgLzA5.rar (12.55 KB) 中缀表达式变成后缀表达式并求值


[此贴子已经被作者于2006-7-2 15:35:51编辑过]



0viq6QwS.rar (8.85 KB) 中缀表达式变成后缀表达式并求值



xnl27rn9.rar (8.97 KB) 中缀表达式变成后缀表达式并求值



wQMgRgaQ.rar (9.04 KB) 中缀表达式变成后缀表达式并求值

搜索更多相关主题的帖子: 后缀 求值 static std void 
2005-10-20 13:07
ysc9706572
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-10-11
得分:0 

有沙发就座,真的是我需要什么的时候就来什么,谢谢楼主

2005-11-05 18:22
tempered
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-11-12
得分:0 

顶要,高手原来是这样出现的!~!在人最需要的时候!~嘿嘿!~

2005-11-16 23:25
tempered
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-11-12
得分:0 

但我用C++来实现时它老显示头文件"stack.h"不能打开!~

2005-11-16 23:44
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
得分:0 
你要将"stack.h"头文件加上去才可以啊,你自己看清楚程序

c++/C + 汇编 = 天下无敌
2005-11-17 08:26
独孤逍遥
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2005-9-25
得分:0 
呵呵,是啊,太好了啊

物以方圆 义薄云天 何以载物 四海纵横
2005-12-28 10:11
welldone2006
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2006-1-8
得分:0 
不错

2006-01-09 09:22
anita
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-1-6
得分:0 
请问楼主:中缀表达式转成前缀表达式 应该怎么做呢??

2006-01-11 18:05
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
得分:0 
前缀表达式计算我就做过。但是中缀变前缀就还没有想出,太复杂了。
晚上我将前缀表达式计算发出来吧

c++/C + 汇编 = 天下无敌
2006-01-18 10:44
风飞清笑
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-2-20
得分:0 
谢谢!

2006-02-20 20:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-30728-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.613630 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved