#include <iostream>
using namespace std;
class rational {
private:
long denom, den; // denom is the numerator
//and den is the denominator
public:
rational(int up = 0, int down = 1);
bool operator<(rational r) const;
bool operator<=(rational r) const;
bool operator == (rational r) const;
bool operator!=(rational r) const;
rational& operator-() ;
friend ostream& operator<<(ostream& out, const rational& r);// Add output operator << overload here
};
rational::rational(int up, int down) {
denom = up;
den = down;
}
bool rational::operator<(rational r) const {
return (this->denom / this->den < r.denom / r.den);
}
bool rational::operator<=(rational r) const {
return (this->denom / this->den <= r.denom / r.den);
}
bool rational::operator==(rational r) const {
return (this->denom / this->den == r.denom / r.den);
}
bool rational::operator!=(rational r) const {
return (this->denom / this->den != r.denom / r.den);
}
rational& rational::operator-() {
rational a(-denom, den);
return a;
}
ostream& operator<<(ostream& out, const rational& r) {
out << r.denom << "/" << r.den;
return out;
}
int main() {
rational r1(1, 2);
rational r2(1, 3);
cout << r1 << " " << r2 << endl;
cout << (r1 < r2) << endl;
cout << (r1 <= r2) << endl;
cout << (r1 != r2) << endl;
cout << -r1 << endl;
}
如图,运行到第38行时结果与预期不符
把“/”改成‘/’时就好了
后面又试了一下,发现只要是一个字符串都会让后面的数字结果出错
这是为什么