四则运算——逆波兰
2015-09-05 16:11
363 查看
写了个四则运算的代码,贴出来给大家看看。代码考虑了负数和不止个位数参与运算的情况,还考虑有中括号大括号的情况。
VS 2010下编译。
<span style="font-size:18px;">#include <iostream>
#include<string>
#include <vector>
using namespace std;
int getPriority(int ch)
{
switch(ch)
{
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
}
return -1;
}
void getSuffixExpre(string str,vector<string>& resultVec)
{
vector<char> stack;
string temp;
bool nagtiveNum = false;
for (int i = 0; i < (int) str.size(); ++i)
{
if (str[i] == ' ')
{
continue;
}
if (str[i] == '-')//处理负数
{
if (i - 1 < 0 || !('0' <= str[i - 1] && '9' >= str[i - 1]))
{
temp.push_back('-');
continue;
}
}
if (str[i] == '[' || str[i] == '{')//处理大括号和中括号
{
str[i] = '(';
}
if (str[i] == ']' || str[i] == '}')
{
str[i] = ')';
}
if (('0' <= str[i] && '9' >= str[i]) )//数字直接输出
{
temp.push_back(str[i]);
}
else//以符号为分界
{
if (!temp.empty())
{
resultVec.push_back(temp);
temp.clear();
}
if (stack.empty()|| str[i] == '(')
{
stack.push_back(str[i]);
}
else
{
if (str[i] == ')')//遇到左括号出栈直到右括号
{
while (stack.back() != '(')
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
}
stack.pop_back();
}
else
{
while (getPriority(str[i]) <= getPriority(stack.back()))//优先级小于栈顶优先级的时候将栈顶元素出栈
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
if (stack.empty())
{
break;
}
}
stack.push_back(str[i]);
}
}
}
}
if (!temp.empty())//最后一个数字
{
resultVec.push_back(temp);
temp.clear();
}
while(!stack.empty())//剩下的符号出栈
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
}
}
vector<string> getInputExpreVec(string str)
{
vector<string> inputVec;
string temp;
for (int i = 0; i < (int)str.length(); ++i)
{
if (str[i] == ' ')
{
continue;
}
if ('0' <= str[i] && '9' >= str[i])
{
temp.push_back(str[i]);
}
else
{
if (!temp.empty())
{
inputVec.push_back(temp);
temp.clear();
}
temp = str[i];
inputVec.push_back(temp);
temp.clear();
}
}
if (!temp.empty())
{
inputVec.push_back(temp);
}
return inputVec;
}
double computRule(string str, double first, double second)
{
switch(str[0])
{
case '+':
return first + second;
case '-':
return first - second;
case '*':
return first * second;
case '/':
return first / second;
}
return -1;
}
double computExpre(vector<string> suffix_expre)//计算后缀表达式
{
vector<double> stack;
double fisrtOperator = 0;
double secondOperator = 0;
for (int i = 0; i < (int)suffix_expre.size(); ++i)
{
if ((suffix_expre[i] != "+") && (suffix_expre[i] != "-") && (suffix_expre[i] != "*") && (suffix_expre[i] != "/"))//数字直接入栈
{
stack.push_back(atof(suffix_expre[i].c_str()));
}
else//符号出栈尾部两个元素运算
{
secondOperator = stack.back();//先出的是第二操作数
stack.pop_back();
fisrtOperator = stack.back();
stack.pop_back();
stack.push_back(computRule(suffix_expre[i], fisrtOperator, secondOperator)) ;
}
}
return stack.back();
}
int main()
{
string inputStr;
getline(cin, inputStr);
vector<string> suffix_vec;
getSuffixExpre(inputStr, suffix_vec);
cout << computExpre(suffix_vec);
return 0;
}</span>
VS 2010下编译。
<span style="font-size:18px;">#include <iostream>
#include<string>
#include <vector>
using namespace std;
int getPriority(int ch)
{
switch(ch)
{
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
}
return -1;
}
void getSuffixExpre(string str,vector<string>& resultVec)
{
vector<char> stack;
string temp;
bool nagtiveNum = false;
for (int i = 0; i < (int) str.size(); ++i)
{
if (str[i] == ' ')
{
continue;
}
if (str[i] == '-')//处理负数
{
if (i - 1 < 0 || !('0' <= str[i - 1] && '9' >= str[i - 1]))
{
temp.push_back('-');
continue;
}
}
if (str[i] == '[' || str[i] == '{')//处理大括号和中括号
{
str[i] = '(';
}
if (str[i] == ']' || str[i] == '}')
{
str[i] = ')';
}
if (('0' <= str[i] && '9' >= str[i]) )//数字直接输出
{
temp.push_back(str[i]);
}
else//以符号为分界
{
if (!temp.empty())
{
resultVec.push_back(temp);
temp.clear();
}
if (stack.empty()|| str[i] == '(')
{
stack.push_back(str[i]);
}
else
{
if (str[i] == ')')//遇到左括号出栈直到右括号
{
while (stack.back() != '(')
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
}
stack.pop_back();
}
else
{
while (getPriority(str[i]) <= getPriority(stack.back()))//优先级小于栈顶优先级的时候将栈顶元素出栈
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
if (stack.empty())
{
break;
}
}
stack.push_back(str[i]);
}
}
}
}
if (!temp.empty())//最后一个数字
{
resultVec.push_back(temp);
temp.clear();
}
while(!stack.empty())//剩下的符号出栈
{
temp = stack.back();
resultVec.push_back(temp);
temp.clear();
stack.pop_back();
}
}
vector<string> getInputExpreVec(string str)
{
vector<string> inputVec;
string temp;
for (int i = 0; i < (int)str.length(); ++i)
{
if (str[i] == ' ')
{
continue;
}
if ('0' <= str[i] && '9' >= str[i])
{
temp.push_back(str[i]);
}
else
{
if (!temp.empty())
{
inputVec.push_back(temp);
temp.clear();
}
temp = str[i];
inputVec.push_back(temp);
temp.clear();
}
}
if (!temp.empty())
{
inputVec.push_back(temp);
}
return inputVec;
}
double computRule(string str, double first, double second)
{
switch(str[0])
{
case '+':
return first + second;
case '-':
return first - second;
case '*':
return first * second;
case '/':
return first / second;
}
return -1;
}
double computExpre(vector<string> suffix_expre)//计算后缀表达式
{
vector<double> stack;
double fisrtOperator = 0;
double secondOperator = 0;
for (int i = 0; i < (int)suffix_expre.size(); ++i)
{
if ((suffix_expre[i] != "+") && (suffix_expre[i] != "-") && (suffix_expre[i] != "*") && (suffix_expre[i] != "/"))//数字直接入栈
{
stack.push_back(atof(suffix_expre[i].c_str()));
}
else//符号出栈尾部两个元素运算
{
secondOperator = stack.back();//先出的是第二操作数
stack.pop_back();
fisrtOperator = stack.back();
stack.pop_back();
stack.push_back(computRule(suffix_expre[i], fisrtOperator, secondOperator)) ;
}
}
return stack.back();
}
int main()
{
string inputStr;
getline(cin, inputStr);
vector<string> suffix_vec;
getSuffixExpre(inputStr, suffix_vec);
cout << computExpre(suffix_vec);
return 0;
}</span>
相关文章推荐
- android 代码实现控件之间的间距
- [Android]在代码里运行另一个程序的方法
- 肯特·贝克:改变人生的代码整理魔法
- 网页恶意代码的预防
- 高手写的Tracer-Flash代码调试类代码下载
- CSS代码缩写技巧
- 非主流Q-zOne代码代码搜集第1/2页
- CreateWeb.vbs 代码
- Lua中编译执行代码相关的函数详解
- 更有效率的css代码编写第1/3页
- 代码中到底应不应当写注释?
- SQL语言查询基础:连接查询 联合查询 代码
- 论坛头像随机变换代码
- .NET 常用功能和代码小结
- C#实现压缩HTML代码的方法
- asp编程中常用的javascript辅助代码第1/2页
- C#超实用代码段合集
- Javascript代码在页面加载时的执行顺序介绍
- JS实现图片无间断滚动代码汇总
- 我的论坛源代码(二)