一个最简单的计算器,支持+、-、*、/四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int 表示的范围。

简单计算器
Description

一个最简单的计算器,支持+、-、*、/四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int 表示的范围。

Input
输入只有一行,共有三个参数,其中第 1、2 个参数为整数,第 3 个参数为操作符(+、-、*、/)。

Output
输出只有一行,一个整数,为运算结果。然而: 1. 如果出现除数为 0 的情况,则输出:Divided by zero!; 2. 如果出现无效的操作符 (即不为+、-、*、/之一),则输出:Invalid operator!。

Sample Input 1

1 2 +
Sample Output 1

3
Sample Input 2

1 0 /
Sample Output 2

Divided by zero!
Sample Input 3

1 0 XOR
Sample Output 3

Invalid operator!
Hint

可以考虑使用if 和switch 结构。

Source

none
具体代码:#include<stdio.h>
int main ()
{
char c;
int a ,b ,d;

scanf("%d %d %c",&a,&b,&c);
switch ( c )
{
case ‘+’: printf("%d\n",d,d = a +b);break;
case ‘-’: printf("%d\n",d, d =a-b);break;
case '’: printf("%d\n",d, d = ab);break;
case ‘/’:
if (b ==0)
printf(“Divided by zero!\n”);
else
{
if (b != 0)
printf("%d\n",d,d = a /b);}
break;

default : printf (“Invalid operator!\n”);
}
return 0;
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值