当前位置:网站首页>PTA Exercise 2.1 Simple Calculator
PTA Exercise 2.1 Simple Calculator
2022-08-10 08:29:00 【Ge Shi steamed buns】
题目
模拟简单运算器的工作.假设计算器只能进行加减乘除运算,运算数和结果都是整数,四种运算符的优先级相同,按从左到右的顺序计算.
输入格式:
输入在一行中给出一个四则运算算式,没有空格,且至少有一个操作数.遇等号”=”说明输入结束.
输出格式:
在一行中输出算式的运算结果,或者如果除法分母为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;
}
题目本身不难,But I didn't notice it at first “四种运算符的优先级相同” 这句话,It was a mistake to do a lot of work in vain.
小插曲:
The first name I gave the operator wasoperator
,But this will give an error.It must be a duplicate of the keyword.
边栏推荐
- ARM结构体系3:ARM指令的寻址和异常中断处理
- Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
- 90. (cesium house) cesium height monitoring events
- Johnson全源最短路
- Class Notes (7) (1) - #647. Find the root and the child (root)
- 90.(cesium之家)cesium高度监听事件
- CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)
- 高性能短连接设计
- TensorFlow 2.9的零零碎碎(一)
- FFT模板
猜你喜欢
明明加了唯一索引,为什么还是产生重复数据?
Uni-app开发微信小程序使用本地图片做背景图
day16--The use of the packet capture tool Charles
【OAuth2】十九、OpenID Connect 动态客户端注册
DAY26:GetShell专题
如何远程调试对方的H5页面
2022-08-01 Advanced Network Engineering (23) Advanced VLAN Technology - VLAN Aggregation, MUX VLAN
Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention
时序动作定位 | ACGNet:弱监督时序动作定位的动作补充图网络(AAAI 2022)
随机推荐
The probability distribution and its application
Rust学习:6.2_复合类型之元组
SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
js函数聚合的三种实现方式
IDLE development wordCount program (5)
编程老手如何在autojs和冰狐智能辅助之间选择?
Go-Excelize API source code reading (11) - GetActiveSheetIndex()
【转】探秘钉钉的即时消息服务DTIM
Linux下载安装MySql
FFT模板
PTA 习题2.1 简单计算器
placeholder 1
张驰课堂:老板会武术,谁也挡不住!六西格玛培训的魅力
Uni-app develops WeChat applet using local images as background images
机器人控制器编程实践指导书旧版-实践一 LED灯(数字量)
QT下载清华源配置
Ask next CDC mysql to Doris. Don't show the specific number of lines, how to do?
UGUI—事件,iTween插件
The implementation of the seemingly useless component (text gradient) in NaiveUI is so simple
mySQL增删改查进阶