C++实现表达式的计算
2017-03-07 00:37
309 查看
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<stack>
#include<map>
#include<cctype>
using namespace std;
string expression;
string post_e;
stack<char> s1;
stack<string> s2;
map<char, int> t1;
map<char, int> t2;
bool isop(string s)
{
if (s == "+" || s == "-" || s == "*" || s == "/")return true;
return false;
}
string cal(int a, int b, char op) {
int ans = 0;
switch (op)
{
case '+': ans = a + b; break;
case '-': ans = a - b; break;
case '*': ans = a*b; break;
case '/':ans = a / b; break;
default:break;
}
stringstream ss;
string an;
ss << ans;
an = ss.str();
return an;
}
int main() {
t1['('] = 1;
t1['+'] = 3;
t1['-'] = 3;
t1['*'] = 5;
t1['/'] = 5;
t1[')'] = 6;
t1['#'] = 0;
t2['#'] = 0;
t2['('] = 6;
t2['+'] = 2;
t2['-'] = 2;
t2['*'] = 4;
t2['/'] = 4;
t2[')'] = 1;
cin >> expression;
expression =expression+ "#";
s1.push('#');
string t;
int len = expression.length();
int i = 0;
while (true) {
if (isdigit(expression[i])) {
t += expression[i++];
}
else {
if (!t.empty()) {
s2.push(t);
t.clear();
}
if (t2[expression[i]] > t1[s1.top()]) {
s1.push(expression[i++]);
}
else if (t2[expression[i]] <= t1[s1.top()]) {
if (s1.top() == '(')s1.pop(), i++;
else if (t1[s1.top()] == t2[expression[i]])break;
else {
string temp(1,s1.top());
s2.push(temp);
s1.pop();
}
}
}
}
stack<string> s3;
while ((s2.size()+s3.size())!=1) {
if (isop(s2.top())) {
s3.push(s2.top());
s2.pop();
}
else {
if (!s2.empty() && !isop(s3.top()) && !isop(s2.top())) {
s2.push(s3.top());
s3.pop();
continue;
}
string p1 = s2.top();
s2.pop();
string p2 =
8c6d
s2.top();
s2.pop();
if (!isop(p1) && !isop(p2)) {
int num2, num1;;
stringstream ss(p1);
ss >> num1;
stringstream ss2(p2);
ss2 >> num2;
s2.push(cal(num2, num1, s3.top()[0]));
s3.pop();
}
else {
s3.push(p1);
s3.push(p2);
}
}
}
cout << s2.top() << endl;
return 0;
}
#include<cstring>
#include<string>
#include<sstream>
#include<stack>
#include<map>
#include<cctype>
using namespace std;
string expression;
string post_e;
stack<char> s1;
stack<string> s2;
map<char, int> t1;
map<char, int> t2;
bool isop(string s)
{
if (s == "+" || s == "-" || s == "*" || s == "/")return true;
return false;
}
string cal(int a, int b, char op) {
int ans = 0;
switch (op)
{
case '+': ans = a + b; break;
case '-': ans = a - b; break;
case '*': ans = a*b; break;
case '/':ans = a / b; break;
default:break;
}
stringstream ss;
string an;
ss << ans;
an = ss.str();
return an;
}
int main() {
t1['('] = 1;
t1['+'] = 3;
t1['-'] = 3;
t1['*'] = 5;
t1['/'] = 5;
t1[')'] = 6;
t1['#'] = 0;
t2['#'] = 0;
t2['('] = 6;
t2['+'] = 2;
t2['-'] = 2;
t2['*'] = 4;
t2['/'] = 4;
t2[')'] = 1;
cin >> expression;
expression =expression+ "#";
s1.push('#');
string t;
int len = expression.length();
int i = 0;
while (true) {
if (isdigit(expression[i])) {
t += expression[i++];
}
else {
if (!t.empty()) {
s2.push(t);
t.clear();
}
if (t2[expression[i]] > t1[s1.top()]) {
s1.push(expression[i++]);
}
else if (t2[expression[i]] <= t1[s1.top()]) {
if (s1.top() == '(')s1.pop(), i++;
else if (t1[s1.top()] == t2[expression[i]])break;
else {
string temp(1,s1.top());
s2.push(temp);
s1.pop();
}
}
}
}
stack<string> s3;
while ((s2.size()+s3.size())!=1) {
if (isop(s2.top())) {
s3.push(s2.top());
s2.pop();
}
else {
if (!s2.empty() && !isop(s3.top()) && !isop(s2.top())) {
s2.push(s3.top());
s3.pop();
continue;
}
string p1 = s2.top();
s2.pop();
string p2 =
8c6d
s2.top();
s2.pop();
if (!isop(p1) && !isop(p2)) {
int num2, num1;;
stringstream ss(p1);
ss >> num1;
stringstream ss2(p2);
ss2 >> num2;
s2.push(cal(num2, num1, s3.top()[0]));
s3.pop();
}
else {
s3.push(p1);
s3.push(p2);
}
}
}
cout << s2.top() << endl;
return 0;
}
相关文章推荐
- 用C++程序实现复杂表达式的计算问题
- 逆波兰表达式 转换和计算 C++实现
- 计算表达式的值c++逆波兰式实现方法
- 用 C++ 实现的加、减、乘、除表达式计算
- C++实现计算器功能(包括计算含未知量的式子),输出后缀表达式
- 算术表达式的计算(基于逆波兰表达式)的c++实现
- C++实现 逆波兰表达式计算问题
- 表达式计算(C++实现)
- 数据结构—中缀表达式转后缀表达式算法及实现—栈的应用—计算表达式(C++代码实现)(1)
- 表达式的计算(C++实现)
- C++实现 逆波兰表达式计算问题
- C++实现顺序计算输入表达式的值
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- C++实现顺序计算输入表达式的值
- #C++实现先中缀转后缀的算术表达式计算
- 用栈实现表达式计算——C++
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- C++实现简易表达式的计算(操作室为10以内)
- 实数四则运算表达式的计算,C++ 实现
- 在单链表的基础上用c++实现的链栈,并使用进制转换,表达式求值两个小程序来测试