首页
社区
课程
招聘
[求助]C++遇到问题了,要求助下
发表于: 2014-12-13 23:27 3917

[求助]C++遇到问题了,要求助下

2014-12-13 23:27
3917
怎么调试都是很多错误、不知道错误出在那、各位帮看下  改好在后面不上加注释   

先谢谢了

代码:

//基本要求:
//(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");
}

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 200
活跃值: (38)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
很多该加const的地方你没加
2014-12-14 00:10
0
雪    币: 52
活跃值: (27)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
感觉你这不是你写的。。。。单词都是错的怎么会编译过,CFraction、cfraction 和Craction 替换为Cfraction就可以编译成功了,也可以运行,但是计算结果没验证,自己去计算吧
2014-12-14 11:53
0
雪    币: 9
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
这个还真不是我写的
2014-12-21 11:38
0
游客
登录 | 注册 方可回帖
返回
//