所以我正在为我的 OO 课程编写一个混合数字课程。我们需要重载每个比较和布尔操作数(除其他外),但我在如何考虑“<”和“>”操作数方面遇到了麻烦。这是我的 '==' 和 '!=' 操作数函数,用于某些上下文。
编辑:另外,如果有人对加法/减法方法有任何提示,我将不胜感激。
bool operator ==(Mixed& mn1, Mixed& mn2){
mn1.ToFraction();
mn2.ToFraction();
mn1.Simplify();
mn2.Simplify();
if(mn1.numerator == mn2.numerator && mn1.denominator == mn2.denominator)
return true;
else
return false;
}
和
bool operator !=( Mixed& mn1, Mixed& mn2){
mn1.ToFraction();
mn2.ToFraction();
mn1.Simplify();
mn2.Simplify();
if(mn1.numerator == mn2.numerator && mn1.denominator == mn2.denominator)
return false;
else
return true;
}
如果有人可以提供一些指导,我将不胜感激。哦,我们不能将它们转换为小数进行比较。
编辑:这是我的标题。
#include <iostream>
using namespace std;
class Mixed
{
public:
Mixed(int integer, int numerator = 0, int denominator = 1);
Mixed(int integer = 0);
double Evaluate();
void ToFraction();
void Simplify();
friend istream& operator >>(istream& in, Mixed& mn);
friend ostream& operator <<(ostream& out, Mixed& mn);
friend bool operator ==( Mixed& mn1, Mixed& mn2);
friend bool operator !=( Mixed& mn1, Mixed& mn2);
friend bool operator >( Mixed& mn1, Mixed& mn2);
friend bool operator <( Mixed& mn1, Mixed& mn2);
friend bool operator <=( Mixed& mn1, Mixed& mn2);
friend bool operator >=( Mixed& mn1, Mixed& mn2);
friend const Mixed operator +( Mixed& mn1, Mixed& mn2);
friend const Mixed operator -( Mixed& mn1, Mixed& mn2);
friend const Mixed operator *( Mixed& mn1, Mixed& mn2);
friend const Mixed operator /( Mixed& mn1, Mixed& mn2);
private:
int GCD(int a, int b);
int integer, numerator, denominator;
};