(2)在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
问题及代码
/* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2015 年 4 月 26 日 * 版 本 号:v1.0 */ #include<iostream> using namespace std; class CFraction { private: int nume; // 分子 int deno; // 分母 public: //构造函数及运算符重载的函数声明 CFraction() {} CFraction(int n,int d):nume(n),deno(d) {} void simplify(); void display(); CFraction operator+(CFraction &c); friend CFraction operator+(CFraction &c,int a); friend CFraction operator+(int a,CFraction &c); CFraction operator-(CFraction &c); friend CFraction operator-(CFraction &c,int a); friend CFraction operator-(int a,CFraction &c); CFraction operator*(CFraction &c); friend CFraction operator*(CFraction &c,int a); friend CFraction operator*(int a,CFraction &c); CFraction operator/(CFraction &c); friend CFraction operator/(CFraction &c,int a); friend CFraction operator/(int a,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); }; //重载函数的实现及用于测试的main()函数 void CFraction::simplify() { int i=deno; for(; nume%i!=0||deno%i!=0; i--); nume=nume/i; deno=deno/i; } void CFraction::display() { cout<<nume<<"/"<<deno<<endl; } CFraction CFraction::operator+(CFraction &c) { int i=deno; if(deno==c.deno) return CFraction(nume+c.nume,deno); else { for(; i%deno!=0||i%c.deno!=0; i++); nume=nume*(i/deno); c.nume=c.nume*(i/c.deno); deno=i; c.deno=i; return CFraction(nume+c.nume,deno); } } CFraction operator+(CFraction &c,int a) { CFraction t(a,1); return c+t; } CFraction operator+(int a,CFraction &c) { CFraction t(a,1); return c+t; } CFraction operator-(CFraction &c,int a) { CFraction t(a,1); return c-t; } CFraction operator-(int a,CFraction &c) { CFraction t(a,1); return c-t; } CFraction operator*(CFraction &c,int a) { CFraction t(a,1); return c*t; } CFraction operator*(int a,CFraction &c) { CFraction t(a,1); return c*t; } CFraction operator/(CFraction &c,int a) { CFraction t(a,1); return c*t; } CFraction operator/(int a,CFraction &c) { CFraction t(a,1); return c/t; } CFraction CFraction::operator-(CFraction &c) { int i=deno; if(deno==c.deno) return CFraction(nume-c.nume,deno); else { for(; i%deno!=0||i%c.deno!=0; i++); nume=nume*(i/deno); c.nume=c.nume*(i/c.deno); deno=i; c.deno=i; return CFraction(nume-c.nume,deno); } } CFraction CFraction::operator*(CFraction &c) { return CFraction(nume*c.nume,deno*c.deno); } CFraction CFraction::operator/(CFraction &c) { if(deno*c.nume<0) return CFraction(-nume*c.deno,-deno*c.nume); else return CFraction(nume*c.deno,deno*c.nume); } bool CFraction::operator>(CFraction &c) { int i=deno; if(deno==c.deno) { if(nume>c.nume) return true; else return false; } else { for(; i%deno!=0||i%c.deno!=0; i++); nume=nume*(i/deno); c.nume=c.nume*(i/c.deno); if(nume>c.nume) return true; else return false; } } bool CFraction::operator<(CFraction &c) { int i=deno; if(deno==c.deno) { if(nume<c.nume) return true; else return false; } else { for(; i%deno!=0||i%c.deno!=0; i++); nume=nume*(i/deno); c.nume=c.nume*(i/c.deno); if(nume<c.nume) return true; else return false; } } bool CFraction::operator==(CFraction &c) { if(*this>c||*this<c) return false; else return true; } bool CFraction::operator>=(CFraction &c) { return !(*this<c); } bool CFraction::operator<=(CFraction &c) { return !(*this>c); } bool CFraction::operator!=(CFraction &c) { return !(*this==c); } int main() { CFraction x(1,3),y(-5,10),s; cout<<"分数x=1/3 y=-5/10"<<endl; s=x+y; cout<<"x+y="; s.simplify(); s.display(); s=x-y; cout<<"x-y="; s.simplify(); s.display(); s=x*y; cout<<"x*y="; s.simplify(); s.display(); s=x/y; cout<<"x/y="; s.simplify(); s.display(); x.display(); if (x>y) cout<<"大于"<<endl; if (x<y) cout<<"小于"<<endl; if (x==y) cout<<"等于"<<endl; y.display(); cout<<endl; CFraction c1(5,3),c2; //以下建议在调试环境中完成测试, c2=c1+5; c2=5+c1; c2=c1-5; c2=5-c1; c2=c1*5; c2=5*c1; c2=c1/5; c2=5/c1; return 0; }
运行结果:
时间: 2024-11-08 22:06:59