# 帮看下这个问题

xiaofang3100 2008-04-08 05:13:34
#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& operator+(Counter &t);
Counter& operator-(Counter &t);
Counter& operator*(Counter &t);
Counter& operator/(Counter &t);
friend istream& operator>>(istream &input,Counter &s);
friend istream& 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
}

...全文
66 9 打赏 收藏 举报

9 条回复

OenAuth.Core 2008-04-08
• 打赏
• 举报

friend 0stream& operator < <(ostream &output,Counter &s);
OenAuth.Core 2008-04-08
• 打赏
• 举报

#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){
switch(a){
case '1':
operand=1;
break;
case '2':
operand=2;
break;
//自己加CASE语句，一直到9，不过这只能实现10以内的计算。
}
}

Counter& operator+(Counter &t);
friend ostream& operator<<(ostream &output,Counter &s);
};

double Counter::total=0;

Counter& Counter::operator +(Counter &t)
{
operand=operand+t.operand;
total+=operand;
return *this;
}
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
}

• 打赏
• 举报

``````#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
} ``````

• 打赏
• 举报

jieao111 2008-04-08
• 打赏
• 举报

9 9 TAB(制表符)

49 31 1
50 32 2
51 33 3
52 34 4
53 35 5
54 36 6
55 37 7
56 38 8
57 39 9

49＋50＝99
taodm 2008-04-08
• 打赏
• 举报

friend istream& operator < <(ostream &output,Counter &s);

jieao111 2008-04-08
• 打赏
• 举报

• 打赏
• 举报

a在count类里面，仅仅是一个函数的参数，而不是一个成员变量，你需要额外的添加a的声明！
p0303230 2008-04-08
• 打赏
• 举报

s是字符串
s[0] s[2]是'1' '2'

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-04-08 05:13

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下