005: Simple Calculator

 

Total time limit: 
1000ms
 
Memory Limit: 
65536kB
description

One of the most simple calculator, supports +, -, *, / four operations. Consider only the input and output is an integer, and the calculation result data does not exceed the range of an int.

Entry
Only one line input, a total of three parameters, wherein the first and second parameter is an integer, the third parameter to the operator (+, -, *, /).
Export
Output only one line, an integer, for the calculation result. However:
1. If the divisor 0 occurs, output: Divided by ZERO!
2. If the operator appears invalid (i.e., not +, -, *, /), one output: Invalid operator!
Sample input
1 2 +
Sample Output
3
prompt
Consider using if and switch structure.
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6     int a,b;
 7     char c;
 8     cin>>a>>b>>c;
 9     if(!(c=='+'||c=='-'||c=='*'||c=='/')){
10         c='0';
11     }
12     switch(c){
13         case '+':
14             cout<<a+b;break;
15         case '-':
16             cout<<a-b;break;
17         case '*':
18             cout<<a*b;break;    
19         case '/':
20             if(b==0){
21                 cout<<"Divided by zero!";break;
22             }else{
23                 cout<<a/b;break;
24             }
25         case'0':
26             cout<<"Invalid operator!";
27     }
28     
29     
30     
31     return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/geyang/p/12329827.html