第13周项目-分数中的运算符重载

本文记录了在C++项目中遇到的一个问题,涉及分数运算符重载时,尝试对负数进行操作导致编译错误。作者在查找错误的过程中尚未找到解决方案,计划后续解决后分享完整经验。
摘要由CSDN通过智能技术生成

问题描述及代码:

/*
*copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:hellow.cpp
*作者:田甜
*完成日期:2016年5月27日
*版本号:v1.0
*
*问题描述:(1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。
(2)在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。 
  (3)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。 
  (4)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。 
*输入描述:无
*程序输出:/
*/
#include <iostream>
 #include <Cmath>
 using namespace std;
 class CFraction
 {
 private:
     int nume;
     int deno;
 public:
     CFraction(int num=0,int den=1);
     void simply();
     friend ostream& operator<<(ostream &output,CFraction &f);
     friend istream& operator>>(istream &input,CFraction &f);//如果运行后提示数据是私有但之前声明了友元,很有可能是因为函数的定义跟实现的名字不一样,比如丢了引用符号
     CFraction operator+(const CFraction &f);
     CFraction operator-(const CFraction &f);
     CFraction operator*(const CFraction &f);
     CFraction operator/(const CFraction &f);
     CFraction operator+();
     CFraction operator-();
     CFraction operator~();
     bool operator==(const CFraction &f);
     bool operator!=(const CFraction &f);
     bool operator>(const CFraction &f);
     bool operator<(const CFraction &f);
     bool operator>=(const CFraction &f);
     bool operator<=(const CFraction &f);
 };
CFraction::CFraction(int num,int den):nume(num),deno(den){}
ostream& operator<<(ostream& output,CFraction &f)
 {
     output<<f.nume<<'/'<<f.deno<<endl;
     return output;
 }
 istream& operator>>(istream& input,CFraction &f)
 {
     input>>f.nume>>f.deno;
     return input;
 }
 void CFraction::simply()
 {
     int t;
     int m=fabs(nume);
     int n=fabs(deno);
     while(t=m%n)
     {
         m=n;
         n=t;
     }
     nume/=n;
     deno/=n;
     if(deno<0)
     {
        deno=-deno;
        nume=-nume;
     }
 }
CFraction CFraction::operator+(const CFraction &f)
 {
     CFraction temp;
     temp.nume=nume*f.deno+f.nume*deno;
     //temp.deno=deno*f.deno+f.deno*deno;分母就不用加了
    temp.deno=deno*f.deno;
     temp.simply();
     return temp;
 }
CFraction CFraction::operator*(const CFraction &f)
 {
     CFraction temp;
     temp.nume=nume*f.nume;
     temp.deno=deno*f.deno;
     temp.simply();
     return temp;
 }
CFraction CFraction::operator/(const CFraction &f)
 {
     if(!f.nume) return *this;
     CFraction temp;
     //temp=temp*~f;
     temp.nume=nume*f.deno;
     temp.deno=deno*f.nume;
     temp.simply();
     return temp;
 }
CFraction CFraction::operator-(const CFraction &f)
 {
     CFraction temp;
     temp.nume=nume*f.deno-f.nume*deno;
     temp.deno=deno*f.deno;
     temp.simply();
     return temp;
 }
CFraction CFraction::operator+()
 {
     CFraction temp;
     temp.deno=deno;
     if(nume<0)
         temp.nume=-nume;
         else temp.nume=nume;
         return *this;
 }
CFraction CFraction::operator-()
 {
    CFraction temp;
     temp.nume=-nume;
     temp.deno=deno;
     return temp;
 }
CFraction CFraction::operator~()
 {
     CFraction temp;
     temp.nume=deno;
     temp.deno=nume;
     return temp;
 }
 bool CFraction::operator>(const CFraction &f)
 {
     int this_nume,f_nume;
     this_nume=nume*f.deno;
     f_nume=nume*deno;
     if((this_nume-f_nume)>0)
         return true;
         else return false;
 }
bool CFraction::operator<(const CFraction &f)
 {
     int this_nume,f_nume;
     this_nume=nume*f.deno;
     f_nume=nume*deno;
     if((this_nume-f_nume)<0)
         return true;
         else return false;
 }

 bool CFraction::operator>=(const CFraction &f)
 {
     if(*this<f)
         return false;
     else
         return true;
 }
bool CFraction::operator<=(const CFraction &f)
 {
     if(*this>f)
         return false;
     else
         return true;
 }
bool CFraction::operator==(const CFraction &f)
 {
     if(*this!=f)
         return false;
     else return true;
 }
bool CFraction::operator!=(const CFraction &f)
 {
     if(*this<f||*this>f)
         return false;
     else return true;
 }
 int main()
 {
      CFraction x,y,s;//测试函数用一下下老师的喽~
    cout<<"输入x: ";
    cin>>x;
    cout<<"输入y: ";
    cin>>y;
    s=+x+y;
    cout<<"+x+y="<<s<<endl;
    s=x-y;
    cout<<"x-y="<<s<<endl;
    s=x*y;
    cout<<"x*y="<<s<<endl;
    s=x/y;
    cout<<"x/y="<<s<<endl;
    //cout<<"-x="<<-x<<endl;
    //cout<<"+x="<<+x<<endl;
    //    cout<<"x的倒数: "<<~x<<endl;
    cout<<x;
    if (x>y) cout<<"大于";
    if (x<y) cout<<"小于";
    if (x==y) cout<<"等于";
    cout<<y<<endl;
    return 0;
 }

运行结果:


心得体会:

这段代码有个未解决的问题,分数取负那里编译有错误,而且是一大堆,找了好久也找不到问题出在哪,想把测试没问题的发上来,等到问题解决了再发文记录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值