当前位置:网站首页>Niuke network realizes simple calculator function

Niuke network realizes simple calculator function

2022-06-24 09:16:00 SZU healing system bug

Catalog

Title Description

AC Code


Title Description

Enter a string on the keyboard , Format : How it works Integers 1 Integers 2, Write a program to parse the... In the string 3 Part content , Then do the corresponding operation , And output the result .

for example :

Input “add 10 20”, Then add (10+20);

Input “sub 10 20”, Then subtract (10-20);

Input “mul 10 20”, Then do multiplication (10*20);

Input “div 10 20”, Then do the division operation (10/20), If the divisor is 0, Then do no operation , Output “Error”;

Be careful : Operation mode ignores case , namely “add” Same as “Add”、“ADD” etc. .

Input description :

Enter a string on the keyboard , Format : How it works Integers 1 Integers 2

The integer range is [-100, 100]

Output description :

Output the result of the operation ( Division does not take into account decimals ), If the divisor is 0, Then do no operation , Output “Error”

Example 1

Input :

add 10 3

Copy output :

13

Copy

Example 2

Input :

sub 10 3

Copy output :

7

Copy

Example 3

Input :

mul 10 3

Copy output :

30

Copy

Example 4

Input :

div 10 3

Copy output :

3

Copy

Example 5

Input :

div 10 0

Copy output :

Error

Copy

Example 6

Input :

ADD 20 20

Copy output :

40

AC Code

#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";
	}
}
原网站

版权声明
本文为[SZU healing system bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240754004224.html