在分数类中的运算符重载基础上
(1)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。
(2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
/* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:Demo.cpp * 完成时间:2015年05月14日 * 版本号:v1.0 */ #include <iostream> using namespace std; class CFraction { private: int nume; // 分子 int deno; // 分母 public: //构造函数及运算符重载的函数声明 CFraction(int n=1,int d=1); CFraction operator+(CFraction &d); CFraction operator-(CFraction &d); CFraction operator*(CFraction &d); CFraction operator/(CFraction &d); bool operator>(const CFraction &c); bool operator<(const CFraction &c); bool operator==(const CFraction &c); bool operator!=(const CFraction &c); bool operator>=(const CFraction &c); bool operator<=(const CFraction &c); void setCFraction(int n,int d); void display(); void simplify(); friend istream &operator>>(istream &in,CFraction &x); friend ostream &operator<<(ostream &out,CFraction x); CFraction operator+(); //取正一目运算 CFraction operator-(); //取反一目运算 CFraction operator~(); //取倒数一目运算 }; int gcd(int m, int n); //重载函数的实现及用于测试的main()函数 CFraction :: CFraction(int n,int d) { nume=n; deno=d; } void CFraction::setCFraction(int n, int d) { nume=n; deno=d; } void CFraction::display() { cout<<nume<<"/"<<deno<<endl; } void CFraction::simplify()//化简 { int n=gcd(deno, nume); deno/=n; nume/=n; } int gcd(int m, int n) //求最大公约数 { int r; if (m<n) { r=m; m=n; n=r; } while(r=m%n) { m=n; n=r; } return n; } CFraction CFraction::operator+(CFraction &d) { CFraction m; m.nume=nume*d.deno+d.nume*deno; m.deno=deno*d.deno; m.simplify(); return m; } CFraction CFraction::operator-(CFraction &d) { CFraction m; m.nume=nume*d.deno-d.nume*deno; m.deno=deno*d.deno; m.simplify(); return m; } CFraction CFraction::operator*(CFraction &d) { CFraction m; m.nume=nume*d.nume; m.deno=deno*d.deno; m.simplify(); return m; } CFraction CFraction::operator/(CFraction &d) { CFraction m; m.nume=nume*d.deno; m.deno=deno*d.nume; m.simplify(); return m; } bool CFraction::operator>(const CFraction &c) { int anume1,anume2,both; both=this->deno*c.deno; anume1=nume*c.deno; anume2=c.nume*deno; if ((anume1>anume2&&both>0)||(anume1<anume2&&both<0)) return true; return false; } bool CFraction::operator<(const CFraction &c) { int anume1,anume2,both; both=this->deno*c.deno; anume1=nume*c.deno; anume2=c.nume*deno; if ((anume1-anume2)*both<0) return true; return false; } bool CFraction::operator==(const CFraction &c) { if (*this!=c) return false; return true; } bool CFraction::operator>=(const CFraction &c) { if (*this<c) return false; return true; } bool CFraction::operator<=(const CFraction &c) { if (*this>c) return false; return true; } bool CFraction::operator!=(const CFraction &c) { if (*this>c || *this<c) return true; return false; } istream &operator>>(istream &in,CFraction &x) { char ch; while(1) { cin>>x.nume>>ch>>x.deno; if (x.deno==0) cerr<<"分母为0, 请重新输入\n"; else if(ch!='/') cerr<<"格式错误(形如m/n)! 请重新输入\n"; else break; } return cin; } ostream &operator<<(ostream &out,CFraction x) { cout<<x.nume<<'/'<<x.deno; return cout; } CFraction CFraction:: operator+() { return *this; } CFraction CFraction:: operator-() { CFraction x; x.nume=-nume; x.deno=deno; return x; } CFraction CFraction:: operator~() { CFraction x; x.nume=deno; x.deno=nume; //未对原分子为0的情况进行处理 if(x.deno<0) //保证负分数的负号在分子上 { x.deno=-x.deno; x.nume=-x.nume; } return x; } 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; }
运行结果:
@ Mayuko
时间: 2024-11-02 23:35:42