怎么调试都是很多错误、不知道错误出在那、各位帮看下 改好在后面不上加注释
先谢谢了
代码:
//基本要求:
//(1) 分数的输入。
//(2) 分数的约分。
//(3) 分数的比较、加、减、乘、除。
//(4) 分数的输出。
#include <iostream>
using namespace std;
class Cfraction
{
private:
int nume; // 分子
int deno; // 分母
int gcd(int m, int n);
public:
//构造函数及运算符重载的函数声明
CFraction(int n = 1, int d = 1)
{
if (d == 0) return;
nume = n, deno = d;
}
friend Cfraction operator + (cfraction &c1, Cfraction &c2);
friend Cfraction operator - (Cfraction &c1, Craction &c2);
friend Cfraction operator * (Cfraction &c1, Cfraction &c2);
friend Cfraction operator / (Cfraction &c1, Cfraction &c2);
friend Cfraction operator - (Cfraction &c);
bool operator > (Cfraction &c);
bool operator < (Cfraction &c);
bool operator >= (Cfraction &c);
bool operator <= (Cfraction &c);
bool operator == (Cfraction &c);
bool operator != (Cfraction &c);
void simplify();
void display();
};
void Cfraction::simplify()
{
int n = gcd(deno, nume);
deno /= n; // 化简
nume /= n;
}
int Cfraction::gcd(int m, int n)
{
int r;
if (m < n)
{r = m; m = n; n = r;}
while(r = m % n) // 求m,n的最大公约数
{
m = n;
n = r;
}
return n;
}
void Cfraction::display()
{
cout << nume << "/" << deno << endl;
}
Cfraction operator + (Cfraction &c1, Cfraction &c2)
{
Cfraction c;
c.deno = c1.deno * c2.deno;
c.nume = c1.nume*c2.deno + c2.nume*c1.deno;
c.simplify();
return c;
}
Cfraction operator - (Cfraction &c1, Cfraction &c2)
{
Cfraction c;
c.deno = c1.deno * c2.deno;
c.nume = c1.nume*c2.deno - c2.nume*c1.deno;
c.simplify();
return c;
}
Cfraction operator * (Cfraction &c1, Cfraction &c2)
{
Cfraction c;
c.deno = c1.deno * c2.deno;
c.nume = c1.nume * c2.nume;
c.simplify();
return c;
}
Cfraction operator / (Cfraction &c1, Cfraction &c2)
{
Cfraction c;
c.deno = c1.deno * c2.nume;
c.nume = c1.nume * c2.deno;
c.simplify();
return c;
}
Cfraction operator - (Cfraction &c)
{
return Cfraction(-c.nume, c.deno);
}
bool Cfraction::operator > (Cfraction &c)
{
Cfraction cf(*this / c);
if (cf.nume/cf.deno > 1)
return true;
else
return false;
}
bool Cfraction::operator < (Cfraction &c)
{
Cfraction cf(*this / c);
if (cf.nume/cf.deno < 1)
return true;
else
return false;
}
bool Cfraction::operator >= (Cfraction &c)
{
Cfraction cf(*this / c);
if (cf.nume/cf.deno >= 1)
return true;
else
return false;
}
bool Cfraction::operator <= (Cfraction &c)
{
Cfraction cf(*this / c);
if (cf.nume/cf.deno <= 1)
return true;
else
return false;
}
bool Cfraction::operator == (Cfraction &c)
{
Cfraction cf(*this / c);
if (cf.nume/cf.deno == 1)
return true;
else
return false;
}
bool Cfraction::operator != (Cfraction &c)
{
Cfraction cf(*this / c);
if (cf.nume/cf.deno != 1)
return true;
else
return false;
}
//重载函数的实现及用于测试的main()函数
void main()
{
Cfraction c1(3, 8), c2(1, 8), c3(1, 8);
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();
cout << "c3=";
c3.display();
if (c1 > c2) cout << "c1 > c2" << endl;
if (c2 < c1) cout << "c2 < c1" << endl;
if (c2 >= c3) cout << "c2 >= c3" << endl;
if (c2 <= c3) cout << "c2 <= c3" << endl;
if (c2 == c3) cout << "c2 == c3" << endl;
if (c1 != c2) cout << "c1 != c2" << endl;
cout << "c1+c2=";
(c1 + c2).display();
cout << "c1-c2=";
(c1 - c2).display();
cout << "c1*c2=";
(c1 * c2).display();
cout << "c1/c2=";
(c1 / c2).display();
c3= -c3;
c3.display();
system("PAUSE");
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课