zoukankan      html  css  js  c++  java
  • 华为笔试题--表达式求值

    题目描述

    给定一个字符串描述的算术表达式,计算出结果值。

    输入字符串长度不超过100,合法的字符包括”+, -, *, /, (, )”,”0-9”,字符串内容的合法性及表达式语法的合法性由做题者检查。本题目只涉及整型计算。

    输入:

    400+50/2-30*(3-6)

    输出:

    515

     1 #include <iostream>
     2 #include <string>
     3 #include <vector>
     4 #include <cstdlib>
     5 #include <cctype>
     6 #include <stack>
     7 #include <map>
     8 using namespace std;
     9 map<char, int> priority = {
    10     { '+', 1 },{ '-', 1 },{ '*', 2 },{ '/', 2 },{ '(', 0 }
    11 }; // 定义运算符优先级
    12 vector<string> postExpression; //存放后缀表达式
    13 stack<char> op; //存放计算后缀表达式是的辅助运算符栈
    14 
    15 void convertToPostExpression(string& str)
    16 {
    17     string num;
    18     for (int i = 0; i < str.size(); ++i) {
    19         if (isdigit(str[i])) {
    20             num.clear();
    21             while (isdigit(str[i])) {
    22                 num.push_back(str[i]);
    23                 ++i;
    24             }
    25             postExpression.push_back(num);//是数字则直接push
    26             --i;
    27         }
    28         else if (str[i] == '(') {
    29             op.push(str[i]);
    30         }
    31         else if (str[i] == ')') {
    32             while (op.top() != '(') {
    33                 string tmp;
    34                 tmp.push_back(op.top());
    35                 postExpression.push_back(tmp);
    36                 op.pop();
    37             }
    38             op.pop();  //将')'也pop出来
    39         }
    40         else {
    41             while (!op.empty() && priority[str[i]] <= priority[op.top()]) {
    42                 string tmp;
    43                 tmp.push_back(op.top());
    44                 postExpression.push_back(tmp);
    45                 op.pop();
    46             }
    47             op.push(str[i]);
    48         }
    49     }
    50     while (!op.empty()) {  //遍历完字符串可能栈里还有运算符 一起弹出
    51         string tmp;
    52         tmp.push_back(op.top());
    53         postExpression.push_back(tmp);
    54         op.pop();
    55     }
    56 }
    57 
    58 int calculateExpression(string& str)
    59 {
    60     stack<int> res;
    61     convertToPostExpression(str);
    62     /*for (int i = 0; i < postExpression.size(); ++i) {
    63         cout << postExpression[i] << " ";
    64     }
    65     cout << endl;*/
    66     for (int i = 0; i < postExpression.size(); ++i) {
    67         if (isdigit(postExpression[i][0])) {
    68             int num = atoi(postExpression[i].c_str());
    69             res.push(num);
    70         }
    71         else {
    72             int tmp1 = res.top();
    73             res.pop();
    74             int tmp2 = res.top();
    75             res.pop();
    76             switch (postExpression[i][0]) {
    77             case '+': res.push(tmp1 + tmp2); break;
    78             case '-': res.push(tmp2 - tmp1); break;
    79             case '*': res.push(tmp2 * tmp1); break;
    80             case '/': res.push(tmp2 / tmp1); break;
    81             }
    82         }
    83     }
    84     return res.top();
    85 }
    86 
    87 int main()
    88 {
    89     string str;
    90     while (cin >> str) {
    91         cout << calculateExpression(str) << endl;
    92     }
    93 }
  • 相关阅读:
    333333
    钢镚儿冲刺一周期第六天
    钢镚儿冲刺一周期第五天
    钢镚儿冲刺一周期第四天
    钢镚儿冲刺一周期第三天
    钢镚儿冲刺一周期第二天
    钢镚儿冲刺一周期第一天
    软件工程第九周总结
    CVPR论文的爬取
    构建之法读后感第五
  • 原文地址:https://www.cnblogs.com/joker1937/p/10669949.html
Copyright © 2011-2022 走看看