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

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

BUAA数据结构第一次编程题——表达式求值

看前须知

.

第一次上机题汇总

.

.

.

.

.

题目内容

问题描述

从标准输入中读入一个整数算术运算表达式,如5 - 1 * 2 * 3 + 12 / 2 / 2 = 。计算表达式结果,并输出。

要求:

  1. 表达式运算符只有+、-、*、/,表达式末尾的’=’字符表示表达式输入结束,表达式中可能会出现空格;

  2. 表达式中不含圆括号,不会出现错误的表达式;

  3. 出现除号/时,以整数相除进行运算,结果仍为整数,例如:5/3结果应为1。

输入形式

从键盘输入一个以=结尾的整数算术运算表达式。操作符和操作数之间可以有空格分隔。

输出形式

向控制台输出计算结果(为整数)。

样例

输入

5 - 1 * 2 * 3 + 12 / 2 / 2 =

输出

2

输入

500 =

输出

500

样例说明

【样例1说明】

输入的表达式为5 - 1 * 2 * 3 + 12 / 2 / 2 =,按照整数运算规则,计算结果为2,故输出2。

【样例2说明】

输入的表达式为500 = ,没有运算符参与运算,故直接输出500。

题解

易错点和难点

算法之一提示:

1、可以利用gets函数,读取整行表达式;
2、对于空格,可以考虑首先去除表达式中的所有空格
3、可以设一计数器用来记录已读取、但未参加运算的运算符的个数,根据该计数器来判断如何进行运算;
4、可以设计一函数:实现二元整数算术运算。

参考代码

1.栈(最最基础的,只有入栈)

#include
#include
#include
#include
#include
int calculate(char* s) { int n = strlen(s); int stk[n], top = 0; char preSign = '+'; int num = 0; for (int i = 0; i < n; ++i) { if (isdigit(s[i])) { num = num * 10 + (int)(s[i] - '0'); } if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) { switch (preSign) { case '+': stk[top++] = num; break; case '-': stk[top++] = -num; break; case '*': stk[top - 1] *= num; break; default: stk[top - 1] /= num; } preSign = s[i]; num = 0; } } int ret = 0; for (int i = 0; i < top; i++) { ret += stk[i]; } return ret;}int main() { char s[2000]; gets(s); s[strlen(s)-1]='\0'; printf("%d\n",calculate(s)); return 0; }

2.搜寻加减号的位置,然后作为分割线,分别计算之间的乘除

#include
#include
char fh[10000];int num[10000];int cnt_fh=1;int main(){ fh[0]='+'; int cnt_num=0; while(fh[cnt_fh-1]!='='){ scanf("%d",&num[cnt_num++]); char tmp_c; scanf("%c",&tmp_c); while(tmp_c==' ') scanf("%c",&tmp_c); fh[cnt_fh++]=tmp_c; } //数据读取完毕; int cnt_jj=0; int position_jj[1000]; int i; for(i=0;i

补充测试的数据

没有特别注意的地方.

题单链接

有考虑过负数和乘方怎么解决吗好兄弟

.

.

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

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
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 compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
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 qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>