博客
关于我
BUAA(2021春)表达式求值——巧妙利用数组完成
阅读量:81 次
发布时间:2019-02-26

本文共 3016 字,大约阅读时间需要 10 分钟。

为了解决这个问题,我们需要计算一个整数算术表达式的结果。表达式中包含加、减、乘、除运算符,且运算顺序遵循先乘除后加减的规则。除法运算结果为整数,结果需从标准输入读取并输出。

方法思路

我们可以使用栈来处理运算顺序。栈的基本思想是从左到右遍历表达式,将数值压入栈中。当遇到乘除运算符时,弹出栈顶的两个数进行运算,然后将结果压回栈中。遇到加减运算符时,弹出栈顶的数进行运算,并将结果压回栈中。处理完所有运算符后,栈中只剩下一个数,即为最终结果。

解决代码

#include 
#include
#include
int calculate(char* s) { int n = strlen(s); int stk[n]; int top = 0; char preSign = '+'; int num = 0; for (int i = 0; i < n; ++i) { if (isdigit(s[i])) { num = num * 10 + (s[i] - '0'); } else if (!isdigit(s[i]) || i == n - 1) { // 运算符或末尾 switch (preSign) { case '+': if (top > 0) { int temp = stk[--top]; int res = num + temp; stk[top++] = res; } else { stk[top++] = num; } preSign = '+'; num = 0; break; case '-': if (top > 0) { int temp = stk[--top]; int res = num - temp; stk[top++] = res; } else { stk[top++] = num; } preSign = '-'; num = 0; break; case '*': if (top > 0) { int temp = stk[--top]; int res = num * temp; stk[top++] = res; } else { stk[top++] = num; } preSign = '*'; num = 0; break; default: if (top > 0) { int temp = stk[--top]; int res = num / temp; stk[top++] = res; } else { stk[top++] = num; } preSign = '/'; num = 0; break; } } } if (preSign == '+' || preSign == '-' || preSign == '*' || preSign == '/') { if (top > 0) { int temp = stk[--top]; if (preSign == '+') { stk[top++] = num + temp; } else if (preSign == '-') { stk[top++] = num - temp; } else if (preSign == '*') { stk[top++] = num * temp; } else { stk[top++] = num / temp; } } else { stk[top++] = num; } } return stk[0];}int main() { char s[2000]; gets(s); s[strcspn(s, " \t\n\r")] = '\0'; s[strlen(s) - 1] = '\0'; printf("%d\n", calculate(s)); return 0;}

代码解释

  • 读取输入:从标准输入读取字符串,并去除末尾的‘=’和空格。
  • 初始化栈:使用一个数组stk来模拟栈,top变量记录栈顶指针位置。
  • 遍历字符:遍历字符串中的每个字符,如果是数字,压入栈中;如果是运算符,根据运算顺序处理并弹出栈顶的数,进行运算后将结果压回栈中。
  • 处理剩余运算:遇到运算符后,处理剩余的加减运算。
  • 输出结果:栈中最后一个元素即为计算结果,输出该结果。
  • 这种方法确保了运算顺序的正确性,能够处理先乘除后加减的规则,并且能够正确处理整数除法。

    转载地址:http://cpak.baihongyu.com/

    你可能感兴趣的文章
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>