当前位置:网站首页>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
,但是这样子会报错。应该是跟关键字重复了吧。
边栏推荐
- DGIOT supports industrial equipment rental and remote control
- CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)
- 90.(cesium之家)cesium高度监听事件
- PHP笔记 28 29 30 31
- 2022-08-01 Advanced Network Engineering (23) Advanced VLAN Technology - VLAN Aggregation, MUX VLAN
- 【搜索引擎】Solr:提高批量索引的性能
- Docker搭建Mysql一主一从
- .NET-8. My Thought Notes
- If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
- JS reduce
猜你喜欢
CV+Deep Learning - network architecture Pytorch recurrence series - classification (3: MobileNet, ShuffleNet)
34. Talk about why you want to split the database?What methods are there?
Pieces of TensorFlow 2.9 (1)
2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)
Synchronization lock synchronized traces the source
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
推荐几个高质量的软件测试实战项目
机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
随机推荐
每日一题,数组字符串的匹配问题
SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
高性能短连接设计
90. (cesium house) cesium height monitoring events
nrm 使用详解
VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention
第十六天&charles的基本操作
每日一题,二叉树中增加一行
CV-人脸识别-2018:ArcFace
Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training
自动化测试框架搭建 ---- 标记性能较差用例
阿里云数据库 RDS SQL Server 版的服务器绑定域名www.cxsdkt.cn.的呢?
神经网络样本太少怎么办,神经网络训练样本太少
数据库公共字段自动填充
人工神经网络工作原理,神经网络的工作原理
The probability distribution and its application
上课笔记(7)(1)——#647. 找树根和孩子(root)
11111
进程管理(动态的)
神经网络的三种训练方法,神经网络训练全过程