当前位置:网站首页>牛客网 实现简单计算器功能
牛客网 实现简单计算器功能
2022-06-24 07:54:00 【SZU治愈系BUG】
目录
题目描述
键盘输入一个字符串,格式:运算方式 整数1 整数2,编写程序解析出字符串中的 3 部分内容,然后做相应的运算,并输出结果。
例如:
输入“add 10 20”,则做加法运算(10+20);
输入“sub 10 20”,则做减法运算(10-20);
输入“mul 10 20”,则做乘法运算(10*20);
输入“div 10 20”,则做除法运算(10/20),如果除数为 0,则不做运算,输出“Error”;
注意:运算方式忽略大小写,即 “add” 同 “Add”、“ADD”等。
输入描述:
键盘输入一个字符串,格式:运算方式 整数1 整数2
整数范围为[-100, 100]
输出描述:
输出运算后的结果(除法不考虑小数),如果除数为 0,则不做运算,输出“Error”
示例1
输入:
add 10 3
复制输出:
13
复制
示例2
输入:
sub 10 3
复制输出:
7
复制
示例3
输入:
mul 10 3
复制输出:
30
复制
示例4
输入:
div 10 3
复制输出:
3
复制
示例5
输入:
div 10 0
复制输出:
Error
复制
示例6
输入:
ADD 20 20
复制输出:
40
AC代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
string code;
int a, b;
cin >> code >> a >> b;
transform(code.begin(), code.end(), code.begin(), ::toupper);
if (code == "ADD")
cout << a + b;
else if (code == "SUB")
cout << a - b;
else if (code == "MUL")
cout << a*b;
else {
if (b)
cout << a / b;
else
cout << "Error";
}
}边栏推荐
- GradScaler MaxClipGradScaler
- Leetcode -- wrong set
- tcpdump抓包实现过程
- Squid代理服务器应用
- Become an IEEE student member
- "Unusual proxy initial value setting is not supported", causes and Solutions
- Unable to change the virtual machine power status and report an error solution
- Data middle office: overview of data governance
- Microblog writing - flow chart - sequence chart - Gantt chart - Mermaid flow chart - good results
- [redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization
猜你喜欢

支持向量机(SVC,NuSVC,LinearSVC)

Ebanb B1 Bracelet brush firmware abnormal interrupt handling

【使用 PicGo+腾讯云对象存储COS 作为图床】

Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems

On the routing tree of gin

KaFormer个人笔记整理

EasyExcel单sheet页与多sheet页写出

Squid proxy application

"I can't understand Sudoku, so I choose to play Sudoku."

【LeetCode】415. String addition
随机推荐
【LeetCode】415. 字符串相加
RISC-V架构下 FPU Context 的动态保存和恢复
"Unusual proxy initial value setting is not supported", causes and Solutions
Pytoch read data set (two modes: typical data set and user-defined data set)
cookie加密 4 rpc方法确定cookie加密
520. detect capital letters
浮点数表示法(总结自CS61C和CMU CSAPP)
4275. Dijkstra序列
110. balanced binary tree recursive method
"I can't understand Sudoku, so I choose to play Sudoku."
Lu Qi: I am most optimistic about these four major technology trends
Remote connection of raspberry pie without display by VNC viewer
1844. replace all numbers with characters
2022.06.23 (traversal of lc_144,94145\
Common emoticons
Webrtc series - network transmission 5: select the optimal connection switching
linux(centos7.9)安装部署mysql-cluster 7.6
Get post: do you really know the difference between requests??????
当程序员被问会不会修电脑时… | 每日趣闻
Data middle office: middle office practice and summary