C++,重载运算符并且化简怎么老报错!求大神指教啊?真的不知道错在哪里了?
#include<iostream>usingnamespacestd;/*重载运算符实现分数的相加乘,*/classRational{intfz;//分子,intfm;...
#include<iostream>
using namespace std;
/*重载运算符实现分数的相加 乘,*/
class Rational
{
int fz;//分子,
int fm;//分母
public:
Rational(int n=0,int d=1):fz(n),fm(n){}
friend ostream& operator <<(ostream &output,const Rational&obj);
friend istream& operator>>(ostream&input,const Rational&obj);
Rational& operator+(const Rational);
Rational& operator*(const Rational);
Rational& operator=(const Rational&obj);
int Getgcd()
{
int gcd=1;
for(int i=1;i<=(fz<fm?fz:fm);i++)
{
if(fz%i==0&&fm%i==0)
gcd=i;
}
return gcd;
}
void Simplify();
};
ostream& operator<<(ostream &output,const Rationl&obj)
{
output<<obj.fz<<"/"<<obj.fm;
return ouput;
}
istream& operator>>(istream &input,const Ratioanl&obj)
{
char ch;
input>>obj.fz>>ch>>obj.fm;
return input;
}
void Rational::Simplify()
{
int gcd=Getgcd();
fz=fz/gcd;
fm=fm/gcd;
}
Rational & Rational::operator*(const Rational&obj)
{
fz=fz*obj.fz;
fm=fm*obj.fm;
Simplify();
return *this;
}
Rational& Rational::operator +(const Rational&obj)
{
fz=fz*obj.fm+obj.fz;
fm=fm*obj.fz;
Simplify();
return *this;
}
Rational& Rational::operator=(const Ratioanl&obj)
{
fz=obj.fz;
fm=obj.fm;
return *this;
}
int main()
{
Rational A,B,C,D;
cin>>A;
cin>>B;
cout<<endl;
C=A+B;
D=A+B;
cout<<C<<endl;
cout<<D<<endl;
return 0;
} 展开
using namespace std;
/*重载运算符实现分数的相加 乘,*/
class Rational
{
int fz;//分子,
int fm;//分母
public:
Rational(int n=0,int d=1):fz(n),fm(n){}
friend ostream& operator <<(ostream &output,const Rational&obj);
friend istream& operator>>(ostream&input,const Rational&obj);
Rational& operator+(const Rational);
Rational& operator*(const Rational);
Rational& operator=(const Rational&obj);
int Getgcd()
{
int gcd=1;
for(int i=1;i<=(fz<fm?fz:fm);i++)
{
if(fz%i==0&&fm%i==0)
gcd=i;
}
return gcd;
}
void Simplify();
};
ostream& operator<<(ostream &output,const Rationl&obj)
{
output<<obj.fz<<"/"<<obj.fm;
return ouput;
}
istream& operator>>(istream &input,const Ratioanl&obj)
{
char ch;
input>>obj.fz>>ch>>obj.fm;
return input;
}
void Rational::Simplify()
{
int gcd=Getgcd();
fz=fz/gcd;
fm=fm/gcd;
}
Rational & Rational::operator*(const Rational&obj)
{
fz=fz*obj.fz;
fm=fm*obj.fm;
Simplify();
return *this;
}
Rational& Rational::operator +(const Rational&obj)
{
fz=fz*obj.fm+obj.fz;
fm=fm*obj.fz;
Simplify();
return *this;
}
Rational& Rational::operator=(const Ratioanl&obj)
{
fz=obj.fz;
fm=obj.fm;
return *this;
}
int main()
{
Rational A,B,C,D;
cin>>A;
cin>>B;
cout<<endl;
C=A+B;
D=A+B;
cout<<C<<endl;
cout<<D<<endl;
return 0;
} 展开
4个回答
展开全部
//先把所有有问题的变量名等标识改了,这都是编译器会告诉你的
#include<iostream>
using namespace std;
/*重载运算符实现分数的相加 乘,*/
class Rational
{
int fz;//分子,
int fm;//分母
public:
Rational(int n = 0, int d = 1) :fz(n), fm(d){}//你把两个都写成了n
friend ostream& operator<<(ostream &output, const Rational&obj);
friend istream& operator>>(istream&input, Rational&obj);//你的第一个参数类型写错了 ,第二个不能为const引用,因为你要修改它
friend Rational operator+(const Rational&,const Rational&); //这两个的重载不定义为成员函数,不然有很多不便也不好实现
friend Rational operator*(const Rational&,const Rational&); //
Rational& operator=(const Rational&obj);
int Getgcd()
{
int gcd=1;
for(int i=1;i<=(fz<fm?fz:fm);i++)//这个算法效率太低了,不过没错我就不改了
{
if(fz%i==0&&fm%i==0)
gcd=i;
}
return gcd;
}
void Simplify();
};
ostream& operator<<(ostream &output, const Rational&obj)
{
output << obj.fz << "/" << obj.fm;
return output;
}
istream& operator>>(istream &input, Rational&obj)//你的声明里参数类型写错了,所以编译器没把它当友元
{
char ch;
input >> obj.fz >> ch >> obj.fm;
return input;
}
void Rational::Simplify()
{
int gcd = Getgcd();
fz = fz / gcd;
fm = fm / gcd;
}
Rational operator*(const Rational&a, const Rational&obj)//重新定义*操作符
{
Rational A;
A.fz = a.fz*obj.fz;
A.fm = a.fz*obj.fm;
A.Simplify();
return A;
}
/*原来的*操作符
Rational& Rational::operator*(const Rational&obj)
{
fz = fz*obj.fz;
fm = fm*obj.fm;
Simplify();
return *this;
}*/
Rational operator+(const Rational&a, const Rational&obj)//重新定义+操作符
{
Rational A;
A.fz = a.fz*obj.fm + a.fm*obj.fz;
A.fm = a.fm*obj.fm;
A.Simplify();
return A;
}
/*原来的+操作符,你这个算法也有问题
Rational& Rational::operator+(const Rational&obj)
{
fz = fz*obj.fm + obj.fz;
fm = fm*obj.fz;
Simplify();
return *this;
}*/
Rational& Rational::operator=(const Rational&obj)
{
fz = obj.fz;
fm = obj.fm;
return *this;
}
int main()
{
Rational A, B, C, D;
cin >> A;
cin >> B;
cout << endl;
C = A + B;
D = A * B;
cout << C << endl;
cout << D << endl;
return 0;
}
你这个有问题的地方实在太多了,我重新写一个都比改你的要快,改了半天才弄好
所以,如果你觉得满意的话就请采纳吧
还有什么问题可以继续问,只要不是问得太深,因为我也在学习的过程中
展开全部
#include "stdafx.h"
#include<iostream>
using namespace std;
/*重载运算符实现分数的相加 乘,*/
class Rational
{
public:
int fz;//分子,
int fm;//分母
public:
Rational(int n = 0, int d = 1) :fz(d), fm(n) {}
friend ostream& operator<<(ostream &output, const Rational&obj);
friend istream& operator>>(istream &input, const Rational&obj);
Rational& operator+(const Rational&);
Rational& operator*(const Rational&);
Rational& operator=(const Rational&obj);
int Getgcd()
{
int gcd = 1;
for (int i = 1; i <= (fz<fm ? fz : fm); i++)
{
if (fz%i == 0 && fm%i == 0)
gcd = i;
}
return gcd;
}
void Simplify();
};
ostream& operator<<(ostream &output, const Rational&obj)
{
output << obj.fz << "/" << obj.fm;
return output;
}
istream& operator>>(istream &input, const Rational&obj)
{
char ch;
input >> obj.fz >> ch >> obj.fm;
return input;
}
void Rational::Simplify()
{
int gcd = Getgcd();
fz = fz / gcd;
fm = fm / gcd;
}
Rational & Rational::operator*(const Rational&obj)
{
fz = fz*obj.fz;
fm = fm*obj.fm;
Simplify();
return *this;
}
Rational& Rational::operator +(const Rational&obj)
{
fz = fz*obj.fm + obj.fz;
fm = fm*obj.fz;
Simplify();
return *this;
}
Rational& Rational::operator=(const Rational &obj)
{
fz = obj.fz;
fm = obj.fm;
return *this;
}
int main()
{
Rational A, B, C, D;
cin >> A;
cin >> B;
cout << endl;
C = A + B;
D = A + B;
cout << C << endl;
cout << D << endl;
return 0;
}
另外,友元函数貌似递归调用了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-02-13
展开全部
#include<iostream>
using namespace std;
/*重载运算符实现分数的相加 乘,*/
class Rational
{
public:
int fz;//分子,
int fm;//分母
Rational(int n = 0, int d = 1) :fz(n), fm(n){}
friend ostream & operator <<(ostream & output, const Rational & obj);
friend istream & operator >>(ostream & input, const Rational & obj);
Rational & operator +(const Rational);
Rational & operator *(const Rational);
Rational & operator =(const Rational &obj);
int Getgcd()
{
int gcd = 1;
for (int i = 1; i <= (fz<fm ? fz : fm); i++)
{
if (fz%i == 0 && fm%i == 0)
gcd = i;
}
return gcd;
}
void Simplify();
};
//Rational:类名写错。
ostream& operator<<(ostream &output, const Rational &obj)
{
output << obj.fz << "/" << obj.fm;
return output;
}
istream& operator>>(istream &input, const Rational &obj)
{
char ch;
//fz,fm为私有,不能直接访问
input >> obj.fz >> ch >> obj.fm;
return input;
}
void Rational::Simplify()
{
int gcd = Getgcd();
fz = fz / gcd;
fm = fm / gcd;
}
Rational & Rational::operator*(const Rational obj)
{
fz = fz*obj.fz;
fm = fm*obj.fm;
Simplify();
return *this;
}
Rational& Rational::operator +(const Rational obj)
{
fz = fz*obj.fm + obj.fz;
fm = fm*obj.fz;
Simplify();
return *this;
}
Rational& Rational::operator=(const Rational &obj)
{
fz = obj.fz;
fm = obj.fm;
return *this;
}
int main()
{
Rational A, B, C, D;
cin >> A;
cin >> B;
cout << endl;
C = A + B;
D = A + B;
cout << C << endl;
cout << D << endl;
return 0;
}
追问
突然觉得我的VC6真心坑爹。。还是报错.....
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
待我看看再说
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询