分数类-运算符重载修改bug

分数运算与化简
本文介绍了一个用于进行分数加减乘除运算的C++程序。该程序能够处理两个分数之间的基本算术操作,并能正确处理分母为零的情况,通过化简确保结果分数的最简形式。

原先的程序,如果两个数相同的情况下,会停止运行,(化简的时候分母为零),修改后的代码为:

#include<iostream>

using namespace std;

class  CFraction
{
private:
    int nume;
    int deno;
public:
    CFraction(int n=1,int d=1):nume(n),deno(d){}
    CFraction  operator+(CFraction &c);
    CFraction  operator-(CFraction &c);
    CFraction  operator*(CFraction &c);
    CFraction  operator/(CFraction &c);
    CFraction  operator-();
    bool  operator>(CFraction &c);
    CFraction  simply(CFraction &c);
    friend int divisor(CFraction &c);
    friend int multiple(CFraction &c);
    void display();
};
CFraction CFraction::operator+(CFraction &c)
{
      CFraction a;
     a.deno=deno*c.deno;
      a.nume=nume*c.deno+c.nume*deno;
      a=simply(a);
      return a;

  }
CFraction  CFraction::operator-(CFraction &c)
{
      CFraction a;
      a.deno=deno*c.deno;
      a.nume=nume*c.deno-c.nume*deno;
      if(a.nume==0)
      {
          return a;
      }else{a=simply(a);}

      return a;


}
CFraction  CFraction::operator*(CFraction &c)
{
    CFraction a;
    a.deno=deno*c.deno;
    a.nume=nume*c.nume;
    a=simply(a);
      return a;
}
CFraction  CFraction::operator/(CFraction &c)
{
    CFraction a;
    a.nume=nume*c.deno;
    a.deno=deno*c.nume;
    a=simply(a);
      return a;
}
CFraction  CFraction::operator-()
{
     CFraction a;
    a.deno=nume;
    a.nume=deno;

    a=simply(a);
      return a;
}
bool CFraction::operator>(CFraction &c)
{
    if(nume/deno>c.nume/c.deno)
    return true;
    else
    return false;

}
CFraction  CFraction::simply(CFraction &c)  //化简
{
    CFraction a;
    int b=divisor(c);
    a.nume=c.nume/b;
    a.deno=c.deno/b;
    return a;
}
int divisor(CFraction &c)       //求最大公约数
{
    int n=min(c.deno,c.nume);
    for(int i=n;i>=0;i--)
    {
        if(c.deno%i==0 && c.nume%i==0)
        {
          return i;
          break;
        }
    }
}

void CFraction::display()
{
    if(nume==0)
    cout<<0<<endl;
    else
    cout<<nume<<"/"<<deno<<endl;
}
int main(){
   CFraction t1(1,3),t2(3,9),t3,t4,t5;
   cout<<"第一个分数为:";
   t1.display();
   cout<<"第二个分数为:";
   t2.display();
   cout<<endl;
   cout<<"t1+t2=";
   t3=t1+t2;
   t3.display();
   cout<<"t2-t1=";
   t3=t2-t1;
   t3.display();
   cout<<"t1*t2=";
   t3=t1*t2;
   t3.display();
   cout<<"t1/t2=";
   t3=t1/t2;
   t3.display();
   cout<<"去倒数";
   t3=-t1;
   t3.display();
   if(t1>t2){
   cout<<"t1>t2"<<endl;
   }else{
   cout<<"t1<t2"<<endl;
   }
  return 0;
}

结果:


心得体会:

细心,心细。


### 运算符重载的实现 运算符重载是一种面向对象编程中的特性,允许程序员为自定义数据类型定义运算符的行为。在C++中,可以通过定义成员函数或非成员函数来实现运算符重载[^1]。 #### 类内实现 在类内部实现运算符重载时,运算符函数作为类的成员函数。以下是一个简单的加法运算符重载示例: ```cpp class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 重载加法运算符 Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } void display() const { cout << real << " + " << imag << "i" << endl; } }; int main() { Complex c1(3, 7); Complex c2(1, 4); Complex c3 = c1 + c2; // 使用重载的加法运算符 c3.display(); return 0; } ``` #### 类外实现 运算符也可以通过友元函数在类外部实现。这种方式适用于需要访问类私有成员的情况。以下是一个减法运算符重载示例: ```cpp class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} friend Complex operator-(const Complex& lhs, const Complex& rhs); void display() const { cout << real << " - " << imag << "i" << endl; } }; // 定义友元函数实现减法运算符 Complex operator-(const Complex& lhs, const Complex& rhs) { return Complex(lhs.real - rhs.real, lhs.imag - rhs.imag); } int main() { Complex c1(5, 3); Complex c2(2, 1); Complex c3 = c1 - c2; // 使用重载的减法运算符 c3.display(); return 0; } ``` #### 注意事项 - 不能创造新的运算符,只能重载已有的运算符[^2]。 - 某些运算符(如`::`、`.*`、`?:`和`sizeof`)不能被重载[^3]。 - 重载后的运算符的操作数至少有一个是用户定义的类型[^3]。 - 赋值运算符(`operator=`)只能被类的成员函数重载[^4]。 #### 示例:赋值运算符重载 以下是一个赋值运算符重载的示例: ```cpp class Data { private: int* pData; public: Data(int value = 0) { pData = new int(value); } ~Data() { delete pData; } // 赋值运算符重载 Data& operator=(const Data& other) { if (this != &other) { // 防止自我赋值 delete pData; pData = new int(*other.pData); } return *this; } void display() const { cout << *pData << endl; } }; int main() { Data d1(10); Data d2(20); d2 = d1; // 使用重载的赋值运算符 d2.display(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值