我遇到了麻烦,实际上我被卡住了。我必须为有理数定义一个类。现在它的一部分是重载输入和输出运算符,现在我已经为输出运算符完成了,但我坚持输入。就我而言,这就是我被卡住的地步
istream& operator >>(istream& ins, rational& frac)
{
int numerator, denominator;
ins >> numerator;
if()
return ins;
}
我的问题是如何让它们将分子和分母分开。如果有帮助,我的班级标题如下。
#ifndef RAT_H
#define RAT_H
#include <iostream>
#include <cstring>
using namespace std;
class rational
{
public:
rational();
rational(int n,int d);
rational(int whole_number);
friend ostream& operator <<(ostream& outs, const rational& frac);
friend istream& operator >>(istream& ins, rational& frac);
private:
void split(string fract);
int numerator;
int denominator;
string farray[2];
string fraction;
};
#endif // RAT_H
输出运算符执行以下操作:
ostream& operator <<(ostream& outs, const rational& frac)
{
outs << frac.numerator<<'/'<<frac.denominator;
return outs;
}