6.3w+
社区成员
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
class Counter
{
public:
int operand;
static double total;
Counter(int a=0):operand(a){}
Counter(char a){
operand = int(a) - 48;
}
Counter& operator+(Counter &t);
Counter& operator-(Counter &t);
Counter& operator*(Counter &t);
Counter& operator/(Counter &t);
friend istream& operator >>(istream &input,Counter &s);
friend ostream& operator <<(ostream &output,Counter &s);
};
double Counter::total=0;
Counter& Counter::operator +(Counter &t)
{
operand=operand+t.operand;
total+=operand;
return *this;
}
Counter& Counter::operator-(Counter &t)
{
operand=operand-t.operand;
total+=operand;
return *this;
}
Counter& Counter::operator*(Counter &t)
{
operand=operand*t.operand;
total+=operand;
return *this;
}
Counter& Counter::operator/(Counter &t)
{
if(t.operand==0)
abort();
operand=double(operand)/double(t.operand);
return *this;
}
istream& operator >>(istream &input,Counter &s)
{
input>>s.operand;
return input;
}
ostream& operator << (ostream &output,Counter &s)
{
output << "total=" << s.total <<endl;
return output;
}
void main()
{
char opera;
string s;
cin>>s;//我输入的是1+2
Counter a(s[0]),b(s[2]);
opera=s[1];
switch(opera)
{
case '+':
a=a+b;break;
}
cout <<Counter::total <<endl;//输出的是99
}