当前位置:网站首页>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.
边栏推荐
猜你喜欢
Unity—UGUI控件
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节
mySQL增删改查进阶
Synchronization lock synchronized traces the source
二叉树 --- 堆
【一起学Rust | 进阶篇 | RMQTT库】RMQTT消息服务器——安装与集群配置
iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵
day16--The use of the packet capture tool Charles
NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单
DGIOT 30 million meters set pressure reading
随机推荐
.NET-7.WPF learning experience summary
Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
iwemeta metaverse: a doll sells for 9999 yuan, and Bubble Mart thinks it is not expensive at all
MySQL的用户临时表与内部临时表
LaTeX出现错误代码Command \algorithmic already defined
Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
1-31部 1-31套 和硬件工程师90天学习资料及笔记汇总
C# 获取PCI等设备的插槽位置信息
明明加了唯一索引,为什么还是产生重复数据?
AFNetworking概述和4.0的实践
如何远程调试对方的H5页面
js-----数组转换成树形结构
FFT模板
【一起学Rust | 进阶篇 | RMQTT库】RMQTT消息服务器——安装与集群配置
机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
uni 小程序腾讯地图polygon背景透明度
DAY25:逻辑漏洞
VMware ESX Server常用命令行
It is obvious that a unique index is added, why does it still generate duplicate data?
day16--抓包工具Charles的使用