赞
踩
四则运算表达式的计算步骤:
(1)利用栈模型将四则表达式转换为后缀表达式;
(2)利用栈模型对后缀表达式进行计算;
需要注意的点:(1)3+2*{1+2*[-4/(8-6)+7]} 这里有一个-4需要处理
(2)5-3+9*6*(6-10-2) 中缀表达式中可能出现非个位数的数值如10
- #include "iostream"
- using namespace std;
- #include <string>
- #include <stack>
- #include <sstream>
- #include <vector>
- #include "math.h"
- void Infix2Suffix(string& str, char* strtmp)
- {
- // 需要注意负号表示负数的问题
- stack<char> s;
- int len = 0;
- for (unsigned int i=0; i<str.length(); i++)
- {
- if (str[i] >= 48 && str[i] <= 57)
- {
- strtmp[len] = str[i];
- len++;
- if (i<str.length() && (str[i+1] <48 || str[i+1]>57))
- {
- strtmp[len] = ' ';
- len++;
- }
- }
- else if (str[i] == '[' || str[i] == '{' || str[i] == '(')
- s.push(str[i]);
- else if(str[i] == '+')
- {
- if (s.empty())
- s.push(str[i]);
- else
- {
- while (!s.empty())
- {
- if (s.top() == '-' || s.top() == '*' || s.top() == '/'|| s.top() == '+')
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- if (s.empty())
- {
- s.push(str[i]);
- break;
- }
- }
- else if ( s.top() == '(' || s.top() == '{' || s.top() == '[')
- {
- s.push(str[i]);
- break;
- }
- }
- }
- }
- else if(str[i] == '-')
- {
- // 得先判断减号是不是代表负数
- if (i==0 || ((str[i-1] == '+' || str[i-1] == '*' || str[i-1] == '/' || str[i-1] == '(' || str[i-1] == '[' || str[i-1] == '{')))
- {
- strtmp[len] = '0';
- len++;
- strtmp[len] = ' ';
- len++;
- }
- if (s.empty())
- s.push(str[i]);
- else
- {
- while (!s.empty())
- {
- if (s.top() == '+' || s.top() == '*' || s.top() == '/' ||s.top() == '-')
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- if (s.empty())
- {
- s.push(str[i]);
- break;
- }
- }
- else if (s.top() == '(' || s.top() == '{' || s.top() == '[')
- {
- s.push(str[i]);
- break;
- }
-
- }
-
- }
- }
- else if(str[i] == '*')
- {
- if(s.empty())
- s.push(str[i]);
- else
- {
- while (!s.empty())
- {
- if ( s.top() == '/'||s.top() == '*' )
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- if (s.empty())
- {
- s.push(str[i]);
- break;
- }
- }
- else if (s.top() == '+' ||s.top() == '-' || s.top() == '(' || s.top() == '{' || s.top() == '[')
- {
- s.push(str[i]);
- break;
- }
-
- }
- }
- }
- else if(str[i] == '/')
- {
- if (s.empty())
- s.push(str[i]);
- else
- {
- while (!s.empty())
- {
- if ( s.top() == '*' || s.top() == '/' )
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- if (s.empty())
- {
- s.push(str[i]);
- break;
- }
- }
- else if (s.top() == '+' ||s.top() == '-' || s.top() == '(' || s.top() == '{' || s.top() == '[')
- {
- s.push(str[i]);
- break;
- }
- }
- }
- }
- else if (str[i] == ')')
- {
- while (s.top() != '(')
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- }
- s.pop();
- }
- else if (str[i] == ']')
- {
- while (s.top() != '[')
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- }
- s.pop();
- }
- else if (str[i] == '}')
- {
- while (s.top() != '{')
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- }
- s.pop();
- }
- }
- while (!s.empty())
- {
- strtmp[len] = s.top();
- len++;
- strtmp[len] = ' ';
- len++;
- s.pop();
- }
- }
-
- int getNumber(string& tmp)
- {
- stack<int> a;
- int tmp1 = 0;
- int tmp2 = 0;
- for (int i=0; i<tmp.length(); i++)
- {
- if (tmp[i] >=48 && tmp[i] <=57)
- {
- int count = 0;
- int j = i+1;
-
- while (j<tmp.length() && (tmp[j]>=48 && tmp[j]<=57) )
- j++;
- for (int k=i; k<j; k++)
- {
- count += (tmp[k]-48)*pow((double)10, (double)(j-i-1));
- }
- i = j;
- a.push(count);
- }
-
- else
- {
- if (tmp[i] == '+')
- {
- tmp1 = a.top();
- a.pop();
- tmp2 = a.top();
- a.pop();
- tmp1 = tmp2 + tmp1;
- a.push(tmp1);
- }
- else if (tmp[i] == '-')
- {
- tmp1 = a.top();
- a.pop();
- tmp2 = a.top();
- a.pop();
- tmp1 = tmp2 - tmp1;
- a.push(tmp1);
- }
- else if (tmp[i] == '*')
- {
- tmp1 = a.top();
- a.pop();
- tmp2 = a.top();
- a.pop();
- tmp1 = tmp2 * tmp1;
- a.push(tmp1);
- }
- else if (tmp[i] == '/')
- {
- tmp1 = a.top();
- a.pop();
- tmp2 = a.top();
- a.pop();
- tmp1 = tmp2 / tmp1;
- a.push(tmp1);
- }
- }
- }
-
- return a.top();;
- }
- int main(void)
- {
- string str;
- cin >> str;
- char strtmp[100] = {0};
- Infix2Suffix(str, strtmp);
- string strout = strtmp;
-
- //cout << strtmp << endl;
- cout << getNumber(strout) << endl;
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。