帮看下这个问题

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
}
帮忙看下这个程序我输入1+2怎么结果输出99,而不是输出3,帮我找下哪里出错了,谢谢了.

...全文
69 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
OenAuth.Core 2008-04-08
  • 打赏
  • 举报
回复
还有friend istream& operator < <(ostream &output,Counter &s); 改成:
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
}
就呆在云上 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){
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
}
就呆在云上 2008-04-08
  • 打赏
  • 举报
回复
正常
程序用的是1和2的字符对应的十进制数
相加就是99
jieao111 2008-04-08
  • 打赏
  • 举报
回复
以下是常用的ASCII码对照表:

十进制 十六进制 字符
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
  • 打赏
  • 举报
回复
编译都通不过,,不知道你怎么运行的。。friend istream& operator < <(ostream &output,Counter &s);
独孤过儿 2008-04-08
  • 打赏
  • 举报
回复
a在count类里面,仅仅是一个函数的参数,而不是一个成员变量,你需要额外的添加a的声明!
p0303230 2008-04-08
  • 打赏
  • 举报
回复
s是字符串
s[0] s[2]是'1' '2'
明白了吧

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧