1.4 19: Simple calculator

Description
One of the simplest calculators, supports +, -, *, / four operations. It is only necessary to consider the case where the input and output are integers, and the data and calculation results will not exceed the range indicated by int.

Input
There is only one line of input, and there are three parameters in total. The first and second parameters are integers, and the third is operators (+,-,*,/).
Output There
is only one line, an integer, which is the result of the operation. however:

  1. If the divisor is 0, the output: Divided by zero!
  2. If an invalid operator appears (that is, not one of +, -, *, /), the output: Invalid operator!
    Sample input
    1 2 +
    sample output
    3
#include <iostream>
using namespace std;
int main()
{
    
    
	int a,b;
	char c;
	cin>>a>>b>>c;
	switch(c)
	{
    
    
		case '+':
			cout<<a+b<<endl;
			break;
		case '-':
			cout<<a-b<<endl;
			break;
		case '*':
			cout<<a*b<<endl;
			break;
		case '/':
			if(b==0)
			{
    
    
				cout<<"Divided by zero!"<<endl;
			} else{
    
    
				cout<<a/b<<endl;
			}
			break;
		default:
			cout<<"Invalid operator!"<<endl;			
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/yansuifeng1126/article/details/111604931