zoukankan      html  css  js  c++  java
  • 蓝桥杯练习系统 表达式计算【堆栈】

    题目:

    问题描述
      输入一个只包含加减乖除和括号的合法表达式,求表达式的值。其中除表示整除。
    输入格式
      输入一行,包含一个表达式。
    输出格式
      输出这个表达式的值。
    样例输入
    1-2+3*(4-5)
    样例输出
    -4
    数据规模和约定
      表达式长度不超过100,表达式运算合法且运算过程都在int内进行。
     
     
    上代码:
     1 #include<stack>
     2 #include<iostream>
     3 #include<string>
     4 using namespace std;
     5 int mat[5][6] = {//+,-,*,/,(
     6     1, 1, 0, 0, 0, 1,
     7     1, 1, 0, 0, 0, 1,
     8     1, 1, 1, 1, 0, 1,
     9     1, 1, 1, 1, 0, 1,
    10     0, 0, 0, 0, 0, 1
    11 };
    12 string str;
    13 stack<int> nu;
    14 stack<int> op;
    15 int len;
    16 void get(bool &flag, int &retn, int &i){//返回字符串的第i个数据 
    17     char tmp;
    18     tmp = str.at(i);
    19     if (tmp <= '9'&&tmp >= '0'){//是数字 
    20         int x = 0;
    21         while (i<len&&str.at(i) <= '9'&&str.at(i) >= '0'){
    22             x = x * 10 + str.at(i) - 48;
    23             i++;
    24         }
    25         i--;
    26         flag = false;
    27         retn = x;
    28     }
    29     else{//是运算符 
    30         flag = true;
    31         if (tmp == '+') retn = 0;
    32         else if (tmp == '-') retn = 1;
    33         else if (tmp == '*') retn = 2;
    34         else if (tmp == '/') retn = 3;
    35         else if (tmp == '(') retn = 4;
    36         else retn = 5;
    37     }
    38 }
    39 int main(){
    40     cin >> str;
    41     len = str.size();
    42     int idx = 0;
    43     bool isstring;
    44     int retnum;
    45     while (idx<len){
    46         get(isstring, retnum, idx);
    47         if (isstring == false){//是数字 
    48             nu.push(retnum);
    49         }
    50         else{//是操作符 
    51             if (op.empty() || mat[op.top()][retnum] == 0){//符号入栈 
    52                 op.push(retnum);
    53             }
    54             else{//需要进行出栈 
    55                 while (!op.empty() && mat[op.top()][retnum] == 1){//如果符号栈非空并且 需要进行出栈操作 
    56                     int tmp = op.top();//栈顶符号 
    57                     op.pop();
    58                     if (tmp == 4) break;//如果栈顶是左括号,直接左括号出栈。
    59                     //如果栈顶不是左括号,取出数字栈的最上面两个 和运算符运算。 
    60                     int a = nu.top();
    61                     nu.pop();
    62                     int b = nu.top();
    63                     nu.pop();
    64                     if (tmp == 0) nu.push(a + b);
    65                     else if (tmp == 1) nu.push(b-a);
    66                     else if (tmp == 2) nu.push(a*b);
    67                     else if (tmp == 3) nu.push(b / a);
    68                 }
    69                 if (retnum != 5) op.push(retnum);
    70             }
    71         }
    72         idx++;
    73     }
    74     while (!op.empty()){//最后依次取出符号栈来进行运算
    75         int tmp = op.top();//栈顶符号 
    76         op.pop();
    77         int a = nu.top();
    78         nu.pop();
    79         int b = nu.top();
    80         nu.pop();
    81         if (tmp == 0) nu.push(a + b);
    82         else if (tmp == 1) nu.push(b-a);
    83         else if (tmp == 2) nu.push(a*b);
    84         else if (tmp == 3) nu.push(b/a);
    85     }
    86     cout << nu.top() << endl;
    87 }
  • 相关阅读:
    MYSQL查询和插入数据的流程是怎样的
    Nacos服务心跳和健康检查源码介绍
    Nacos使用和注册部分源码介绍
    实用程序包utils
    SOLID原则
    前端实用程序包utils
    实现 strStr()
    记一次华为机试
    十分钟入门 Python 教程
    字符串转换整数 (atoi)
  • 原文地址:https://www.cnblogs.com/Elaine-DWL/p/6535733.html
Copyright © 2011-2022 走看看