当前位置:网站首页>PTA 习题2.1 简单计算器
PTA 习题2.1 简单计算器
2022-08-10 08:24:00 【盖世馒头】
题目
模拟简单运算器的工作。假设计算器只能进行加减乘除运算,运算数和结果都是整数,四种运算符的优先级相同,按从左到右的顺序计算。
输入格式:
输入在一行中给出一个四则运算算式,没有空格,且至少有一个操作数。遇等号”=”说明输入结束。
输出格式:
在一行中输出算式的运算结果,或者如果除法分母为0或有非法运算符,则输出错误信息“ERROR”。
输入样例:
1+2*10-10/2=
输出样例:
10
解答
#include<stdio.h>
int main(){
int num,result;
char ch;
scanf("%d%c",&num,&ch);
result=num;
while(ch!='='){
scanf("%d",&num);
switch (ch){
case '+':
result+=num;
break;
case '-':
result-=num;
break;
case '*':
result*=num;
break;
case '/':
if(num==0){
printf("ERROR");
return 0;
}
result/=num;
break;
default:
printf("ERROR");
return 0;
}
scanf("%c",&ch);
}
printf("%d",result);
return 0;
}
题目本身不难,但我一开始没注意到 “四种运算符的优先级相同” 这句话,白干了不少活还算错了。
小插曲:
我一开始给运算符的命名是operator,但是这样子会报错。应该是跟关键字重复了吧。
边栏推荐
猜你喜欢

什么是长轮询
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节

NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单

PLSQL学习第三天

day16--抓包工具Charles的使用

Quickly enter the current date and time

Rust learning: 6.3_ Tuples of composite types

It is obvious that a unique index is added, why does it still generate duplicate data?

iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵

iwemeta metaverse: a doll sells for 9999 yuan, and Bubble Mart thinks it is not expensive at all
随机推荐
Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
It is obvious that a unique index is added, why does it still generate duplicate data?
QT下载清华源配置
phpstudy开机自启
Rust学习:6.1_复合类型之切片
UGUI—事件,iTween插件
问下cdc mysql to doris.不显示具体行数,怎么办?
Synchronization lock synchronized traces the source
并查集模板
菜鸟、小白在autojs和冰狐智能辅助之间如何选择?
iwemeta metaverse: a doll sells for 9999 yuan, and Bubble Mart thinks it is not expensive at all
JS reduce
MySQL的用户临时表与内部临时表
全连接神经网络结构图,神经网络示意图怎么画
Docker搭建Mysql一主一从
.NET-7.WPF learning experience summary
【搜索引擎】Solr:提高批量索引的性能
Pieces of TensorFlow 2.9 (1)
qrcode-----生成二维码
不同的命令行风格