c++定义一个有理数类
测试数据:解有理数方程a/bx+c/d=e/f,其中,a/b,c/d,e/f,分别为:1/2-3/45/6可求解得到x=19/6,带分数的解为:31/64要求:*设计有理...
测试数据:解有理数方程a/bx+c/d=e/f,其中,a/b,c/d,e/f,分别为:1/2 -3/4 5/6可求解得到x=19/6,带分数的解为:3 1/6
4要求:*设计有理数类Rational,利用重载技术,重新定义各运算符
*将有理数类Rational的声明与实现分别放在文件Rational.h和Rational.cpp中;
*利用定义的Rational类求解有理分式方程
这是学校c++程序要的编程,老师没教过,找了很多资料,奈何本人太学渣。。。。。有没有哪位过路的高手可以帮帮忙。 展开
4要求:*设计有理数类Rational,利用重载技术,重新定义各运算符
*将有理数类Rational的声明与实现分别放在文件Rational.h和Rational.cpp中;
*利用定义的Rational类求解有理分式方程
这是学校c++程序要的编程,老师没教过,找了很多资料,奈何本人太学渣。。。。。有没有哪位过路的高手可以帮帮忙。 展开
展开全部
以前写过分数类的运算,跟有理数是一样的,你可以参考下
#include <iostream>
using namespace std;
class Fraction {
private:
int numerator;
unsigned int denominator;
int gcd(int, int);
void merge();
public:
Fraction(int, unsigned int);
friend Fraction operator+ (Fraction, Fraction);
friend Fraction operator- (Fraction, Fraction);
friend Fraction operator* (Fraction, Fraction);
friend Fraction operator/ (Fraction, Fraction);
friend bool operator== (Fraction, Fraction);
friend bool operator!= (Fraction, Fraction);
friend bool operator< (Fraction, Fraction);
friend bool operator<= (Fraction, Fraction);
friend bool operator> (Fraction, Fraction);
friend bool operator>= (Fraction, Fraction);
friend ostream& operator<< (ostream&, Fraction&);
};
int Fraction::gcd(int x, int y) {
if(x < y)
return gcd(y, x);
if(x % y != 0)
return gcd(y, x % y);
else
return y;
}
void Fraction::merge() {
if(numerator) {
int g = gcd(abs(numerator), denominator);
numerator /= g;
denominator /= g;
} else {
denominator = 1;
}
}
Fraction::Fraction(int num = 0, unsigned int den = 1) {
numerator = num;
denominator = den;
merge();
}
Fraction operator + (Fraction frac1, Fraction frac2) {
return Fraction(frac1.numerator * frac2.denominator + frac2.numerator * frac1.denominator, frac2.denominator * frac1.denominator);
}
Fraction operator - (Fraction frac1, Fraction frac2) {
return Fraction(frac1.numerator * frac2.denominator - frac2.numerator * frac1.denominator, frac2.denominator * frac1.denominator);
}
Fraction operator * (Fraction frac1, Fraction frac2) {
return Fraction(frac1.numerator * frac2.numerator, frac2.denominator * frac1.denominator);
}
Fraction operator / (Fraction frac1, Fraction frac2) {
return Fraction(frac1.numerator * frac2.denominator, frac2.numerator * frac1.denominator);
}
bool operator == (Fraction frac1, Fraction frac2) {
return (frac1.numerator * frac2.denominator) == (frac2.numerator * frac1.denominator);
}
bool operator != (Fraction frac1, Fraction frac2) {
return !(frac1 == frac2);
}
bool operator < (Fraction frac1, Fraction frac2) {
return (frac1.numerator * frac2.denominator) < (frac2.numerator * frac1.denominator);
}
bool operator <= (Fraction frac1, Fraction frac2) {
return (frac1 < frac2) || (frac1 == frac2);
}
bool operator > (Fraction frac1, Fraction frac2) {
return !(frac1 <= frac2);
}
bool operator >= (Fraction frac1, Fraction frac2) {
return !(frac1 < frac2);
}
ostream& operator<< (ostream& os, Fraction& frac) {
os << frac.numerator << "/" << frac.denominator;
return os;
}
更多追问追答
追问
还是不太懂……那个是要类的声明和实现放在不同的文件夹里面,可不可以写具体一点T^T学渣真的压力山大
我提高了悬赏求学霸帮帮忙T^T
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询