/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: * 作 者: 苗影 * 完成日期: 2012 年 4 月 16 日 * 版 本 号: * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束 */
#include<iostream> using namespace std; class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1):nume(nu),deno(de){} void simplify(); friend ostream& operator << (ostream&,CFraction&);//声明重载运算符“<<”函数 CFraction operator+( CFraction &c); //两个分数相加,结果要化简 CFraction operator-( CFraction &c); //两个分数相减,结果要化简 CFraction operator*( CFraction &c); //两个分数相乘,结果要化简 CFraction operator/( CFraction &c); //两个分数相除,结果要化简 CFraction operator+(); //取正一目运算 CFraction operator-(); //取反一目运算 bool operator>(CFraction &c); bool operator<( CFraction &c); bool operator==( CFraction &c); bool operator!=( CFraction &c); bool operator>=( CFraction &c); bool operator<=( CFraction &c); }; ostream& operator << (ostream& output,CFraction& c)//定义重载运算符“<<”函数 { output << "(" <<c. nume<< "/" << c.deno<<")"; return output; } void CFraction::simplify() { int h,m,i,j; for(i=2;i<=6;i++) { { h=nume%i; m=deno%i; } while(h==0&&m==0) { j=i; nume=nume/j; deno=deno/j; h=nume%i; m=deno%i; } } cout<<nume<<"/"<<deno<<endl; } CFraction CFraction::operator+( CFraction &c) { CFraction t; t.nume=nume*c.deno+c.nume*deno; t.deno=deno*c.deno; return t; } CFraction CFraction:: operator-( CFraction &c) { CFraction t; t.nume=nume*c.deno-c.nume*deno; t.deno=deno*c.deno; return t; } CFraction CFraction:: operator/( CFraction &c) { CFraction t; t.nume=nume*c.nume; t.deno=deno*c.deno; return t; } CFraction CFraction:: operator*( CFraction &c) { CFraction t; t.nume=nume*c.deno; t.deno=deno*c.nume; return t; } CFraction CFraction:: operator+() { CFraction t; nume=nume; deno=deno; return t; } CFraction CFraction:: operator-() { CFraction t; t.nume=-nume; t.deno=deno; return t; } bool CFraction::operator>( CFraction &c) { int t,g,h; t=nume*c.deno; g=c.nume*deno; h=deno*c.deno; if(t>g&&h>0||t<g&&h<0) return true; else return false; } bool CFraction::operator<( CFraction &c) { int t,g,h; t=nume*c.deno; g=c.nume*deno; h=deno*c.deno; if(t<g&&h>0||t>g&&h<0) return true; else return false; } bool CFraction::operator==( CFraction &c) { int t,g,h; t=nume*c.deno; g=c.nume*deno; h=deno*c.deno; if(t==g) return true; else return false; } bool CFraction::operator!=( CFraction &c) { int t,g,h; t=nume*c.deno; g=c.nume*deno; h=deno*c.deno; if(t!=g) return true; else return false; } bool CFraction::operator>=( CFraction &c) //这种方法很方便哦 { if (*this<c) return false; else return true; } bool CFraction::operator<=( CFraction &c) { if (*this>c) return false; else return true; } void main() { CFraction x(3,4),y(2,6),s; cout<<"分数x=3/4 y=2/6"<<endl; s=x+y; cout<<"x+y="; s.simplify(); cout<<endl; s=x-y; cout<<"x-y="; s.simplify(); cout<<endl; s=x*y; cout<<"x*y="; s.simplify(); cout<<endl; s=x/y; cout<<"x/y="; s.simplify(); cout<<endl; if(x>y) { cout<<x<<">"<<y; cout<<endl; } if(x<y) { cout<<x<<">"<<y; cout<<endl; } if(x==y) { cout<<x<<"=="<<y; cout<<endl; } if(x!=y) { cout<<x<<"!="<<y; cout<<endl; } if(x<=y) { cout<<x<<"<="<<y; } cout<<endl; s=-x; cout<<"(3/4)的相反数是"; cout<<s; system("pause"); }
第九周(3)
最新推荐文章于 2022-02-25 21:57:21 发布