当前位置:网站首页>016:简单计算器
016:简单计算器
2022-07-21 05:15:00 【龙星尘】
016:简单计算器
总时间限制:
1000ms
内存限制:
65536kB
描述
一个最简单的计算器,支持+, -, *, / 四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围。
输入
输入只有一行,共有三个参数,其中第1、2个参数为整数,第3个参数为操作符(+,-,*,/)。
输出
输出只有一行,一个整数,为运算结果。然而:
1. 如果出现除数为0的情况,则输出:Divided by zero!
2. 如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
样例输入
1 2 +
样例输出
3
现在就是五一假期的最后一天了,之前我们所讲的题实在是太简单了,今天我们来搞一个稍微有一点难度的题“简单计算器”,我看了之后,刚开始以为挺难得,结果只是输入两个数,和一个符号,连括号都不必,而且只输入一个运算符,不愧是“简单”!
真正的计算器,+-*/()......全都可以用,然而就在这几天内,我在上一个关于递归方面的课程,里面就有一个和计算机功能一样的题“表达式求值”,这道题太难了,我想了几天都没有出结果,好了,扯远了,回归正题。
简单计算器这道题我们可以用switch和if来判断,大家对switc可能不太了解,在这里我就跟大家简单介绍一下:
一般形式:
switch(表达式){
case 常量表达式1: 语句1;
case 常量表达式2: 语句2;
…
case 常量表达式n: 语句n;
default: 语句n+1;
}
工作原理:
对表达式(通常是变量)进行一次计算。
把表达式的值与结构中 case 的值进行比较。
如果存在匹配,则执行与 case 关联的代码。
代码执行后,break语句阻止代码跳入下一个 case 中继续执行。
如果没有 case 为真,则使用 default 语句。
给大家看一个样例程序:
样例代码:
#include <stdio.h>
using namespace std;
int main()
{
int a;
scanf("%d",&a);
switch (a)
{
case 1:printf("Monday\n");
case 2:printf("Tuesday\n");
case 3:printf("Wednesday\n");
case 4:printf("Thursday\n");
case 5:printf("Friday\n");
case 6:printf("Saturday\n");
case 7:printf("Sunday\n");
default:printf("error\n");
}
return 0;
}
你们觉得这样的switvh程序对不对呢?如果有特殊需要的话,这个程序是可以运行的,但是在这个程序里面,这样数不行的,。
假设你输入的a是4,按理来说他只会输出“”Thursday ,但是在这个程序里,他会把“Thursday”后面的全部都输出,这样我们应该怎么办呢?有一个函数“break”,他是用来终止这一小部分程序的。
C++ 中 break 语句有以下两种用法:
- 当 break 语句出现在一个循环内时,循环会立即终止,且程序流将继续执行紧接着循环的下一条语句。
- 它可用于终止 switch 语句中的一个 case
所以每次case结束了,记得在之后添加一个break;哦!
正确switch代码:
#include <stdio.h>
using namespace std;
int main()
{
int a;
scanf("%d",&a);
switch (a)
{
case 1:printf("Monday\n");break;
case 2:printf("Tuesday\n");break;
case 3:printf("Wednesday\n");break;
case 4:printf("Thursday\n");break;
case 5:printf("Friday\n");break;
case 6:printf("Saturday\n");break;
case 7:printf("Sunday\n");break;
default:printf("error\n");break;
}
return 0;
}
如果需要大量的判断时, 一些优秀的程序员通常会使用switch语句而不是if语句,因为if语句需要一长串的判断,如果出现错误的话不好找出来,而switch就更简洁及美观一些,可以更好的发现错误。
现在回归正题,数字肯定可以不用switch语句判断,那么需要判断的就只是运算符了(+-*/)。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b;
char c;
float x;
cin>>a>>b>>c;
switch(c)
{
case '+':x=a+b;break;
case '-':x=a-b;break;
case '*':x=a*b;break;
case '/':x=a/b;break;
}
cout<<x<<endl;
}
这个代码行不行呢?不行,如果你输入的是‘/’号,并且除数是0,那么程序将会死机,无输出,而且题目上也说了的, 如果出现除数为0的情况,则输出:Divided by zero!如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
所以我们要判断一下:
修改代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b;
char c;
float x;
cin>>a>>b>>c;
switch(c)
{
case '+':x=a+b;break;
case '-':x=a-b;break;
case '*':x=a*b;break;
case '/':
if(b==0)
{
cout<<"Divided by zero!"<<endl;return 0;
}
else
{
x=a/b;
break;
}
}
cout<<x<<endl;
}
我们在运算符为‘/’的情况下,再次判断除数是否是0,如果是就输出Divided by zero!,不是就依旧按原来的来,如果出现除数为0的情况我们解决了。
还有如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!没有解决,这个好办,如果没有 case 为真,则使用 default 语句。
最终代码1:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b;
char c;
float x;
cin>>a>>b>>c;
switch(c)
{
case '+':x=a+b;break;
case '-':x=a-b;break;
case '*':x=a*b;break;
case '/':
if(b==0)
{
cout<<"Divided by zero!"<<endl;return 0;
}
else
{
x=a/b;
break;
}
default :cout<<"Invalid operator!"<<endl;return 0;
}
cout<<x<<endl;
}
将default语句加上后,这道题也算完结了,但是之前我说过,还可以运用if判断来接觉,就给大家看看吧!
最终代码2:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b;
char c;
float x;
cin>>a>>b>>c;
if(c=='+')
{
x=a+b;
cout<<x;
}
else if(c=='-')
{
x=a-b;
cout<<x;
}
else if(c=='*')
{
x=a*b;
cout<<x;
}
else if(c=='/')
{
if(b==0)
{
cout<<"Divided by zero!"<<endl;
}
else
{
x=a/b;
cout<<x;
}
}
else
{
cout<<"Invalid operator!"<<endl;return 0;
}
return 0;
}
在if判断中,else if就相当于switch中的case,else就相当于default。
边栏推荐
- Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
- Task and Await: Consuming Awaitable Methods
- Teach you how to use Charles to grab bags
- In depth analysis of multiple knapsack problem (Part 1)
- Source insight 4.0 personalization and shortcut keys
- Pytorch2onnx2tensorflow environment preparation win10
- "Running image technology" obtained Angel + financing to build a new generation of real-time data infrastructure platform
- Teach you how to use yolov4 training and testing data set on the server (nanny level)
- Interview question: what is the difference between clustered index and non clustered index?
- beta.4 版发布啦,国产开源数据可视化 datart 接下来将会小跑进入 rc 阶段
猜你喜欢
234. 回文链表
"Running image technology" obtained Angel + financing to build a new generation of real-time data infrastructure platform
概率论-假设检验
Principles and advantages of wireless charging module development
04 cadre d'essai de l'unit é unitest
What is an electronic scale weighing module? What functions does it have?
What is the gateway? What is the Internet of things gateway?
RobotFramework(ride)关键字无法使用或关键字为黑色
108. 将有序数组转换为二叉搜索树
干货 | 分布式系统的数据分片难题
随机推荐
Learn SCP NFS TFTP together
In depth analysis of multiple knapsack problem (Part 1)
Summary of Niuke online question brushing -- top101 must be brushed for interview
74. 搜索二维矩阵
快速排序
One article explains the problem of data fragmentation in distributed systems
01背包面试题系列(一)
地产巨头,数据一体化建设项目方案(拿走不谢)
What opportunities and challenges does the development of instrument control panel face?
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
230. 二叉搜索树中第K小的元素
Heap - principle to application - heap sorting, priority queue
面试官:你的update 更新影响行数做判断了吗?
datart 自定义插件,不改动源代码,让 BI 顺利完成又一次创新
西瓜书第三章-线性模型
三类基于贪心思想的区间覆盖问题
深入剖析多重背包问题(下篇)
机器学习-频率派vs贝叶斯派
Mstest cannot exit