#include <bits/stdc++.h>
using namespace std;
class Calculator
{
private :
int operand1;
char oprate;
int operand2;
public:
Calculator(int num1,char op,int num2);
Calculator(char op,int num2);
int calculator();
};
Calculator::Calculator(char op,int num2)
{
this->oprate = op;
this->operand2 = num2;
};
Calculator::Calculator(int num1,char op,int num2)
{
this->operand1 = num1;
this->oprate = op;
this->operand2 = num2;
};
int Calculator::calculator()
{
int ans;
switch(this->oprate)
{
case '+':
ans = this->operand1+this->operand2;
break;
case '-':
ans = this->operand1-this->operand2;
break;
case '*':
ans = this->operand1*this->operand2;
break;
case '/':
if(this->operand2==0)
throw -1;
else
ans = this->operand1/this->operand2;
break;
}
return ans;
}
int main()
{
int num1,num2;
char op;
int ok = 1;
while(true)
{
if(ok)
{
cin>>num1>>op>>num2;
ok = 0;
}
else
{
cin>>op>>num2;
}
try
{
Calculator data(num1,op,num2);
num1 = data.calculator();
cout<<num1<<endl;
}
catch(int)
{
cout<<"除数为零"<<endl;
}
}
return 0;
}
c++类--计算机
最新推荐文章于 2021-11-16 21:33:05 发布