第九周任务三
来源:互联网 发布:matlab画矩阵灰度图 编辑:程序博客网 时间:2024/04/28 02:35
* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
* 作 者: 张艳明
* 完成日期: 2012 年 04 月 18 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include<iostream.h> #include"stdlib.h" int gcd(int m, int n); class CFraction { private: int nume; // 分子 int deno; // 分母 public: //构造函数及运算符重载的函数声明 CFraction(int nu=0,int de=1); //构造函数,初始化用 void Simplify(); //化简(使分子分母没有公因子) void output(); //输出:以8/6为例,style为0时,输出8/6; bool operator > (CFraction &t); bool operator < (CFraction &t); bool operator >= (CFraction &t); bool operator <= (CFraction &t); bool operator == (CFraction &t); bool operator != (CFraction &t); CFraction operator+(CFraction &c); CFraction operator-(CFraction &c); CFraction operator*(CFraction &c); CFraction operator/(CFraction &c); CFraction operator-(); friend ostream& operator << (ostream &,CFraction &); friend istream& operator >> (istream &,CFraction &); }; istream& operator >> (istream& input,CFraction & c) { input>>c.nume>>c.deno; return input; } ostream& operator << (ostream& output,CFraction & c) { output<<c.nume<<'/'<<c.deno<<endl; return output; } CFraction::CFraction(int nu,int de) //构造函数,初始化用 { if (de!=0) { nume=nu; deno=de; } else { cerr<<"初始化中发生错误,程序退出\n"; exit(0); } } void CFraction::Simplify() //化简(使分子分母没有公因子) { int n; if(nume < 0) { n = gcd(-nume, deno); } else { n = gcd(nume, deno); } nume = nume / n; deno = deno / n; } // 求m,n的最大公约数 int gcd(int m, int n) { int r; if (m<n){r=m;m=n;n=r;} while(r=m%n) // 求m,n的最大公约数 { m=n; n=r; } return n; } bool CFraction::operator > (CFraction &t) { CFraction c2,c3; c2.nume =nume*t.deno ; c3.nume =t.nume *deno; if(c2.nume >c3.nume ) return true; else return false; } bool CFraction::operator < (CFraction &t) { CFraction c2,c3; c2.nume =nume*t.deno ; c3.nume =t.nume *deno; if(c2.nume <c3.nume ) return true; else return false; } bool CFraction::operator >= (CFraction &t) { CFraction c1; c1.nume =nume; c1.deno =deno; if (c1<t) return false; return true; } bool CFraction::operator <= (CFraction &t) { CFraction c1; c1.nume =nume; c1.deno =deno; if (c1>t) return false; return true; } bool CFraction::operator == (CFraction &t) { CFraction c1; c1.nume =nume; c1.deno =deno; if (c1<t) return false; if (c1>t) return false; return false; } bool CFraction::operator != (CFraction &t) { CFraction c1; c1.nume =nume; c1.deno =deno; if (c1==t) return false; return true; } CFraction CFraction::operator+(CFraction &c) { CFraction c2,c3,c4; c2.nume =nume*c.deno ; c3.nume =c.nume *deno; c2.deno =deno*c.deno ; c3.deno =c.deno *deno; c4.nume=c2.nume +c3.nume ; c4.deno =c2.deno ; c4.Simplify (); return c4; } CFraction CFraction::operator-(CFraction &c) { CFraction c2,c3,c4; c2.nume =nume*c.deno ; c3.nume =c.nume *deno; c2.deno =deno*c.deno ; c3.deno =c.deno *deno; c4.nume=c2.nume -c3.nume ; c4.deno =c2.deno ; c4.Simplify (); return c4; } CFraction CFraction::operator*(CFraction &c) { CFraction c2,c3,c4; c2.nume =nume*c.nume ; c2.deno =deno*c.deno ; c2.Simplify (); return c2; } CFraction CFraction::operator/(CFraction &c) { CFraction c2,c3; c2.nume =c.deno ; c2.deno =c.nume ; c3.nume =nume*c2.nume ; c3.deno =deno*c2.deno ; c3.Simplify (); return c3; } CFraction CFraction::operator-() { CFraction c2; c2.nume =nume; c2.deno =deno; c2.Simplify (); if(c2.nume<0 || c2.deno<0) { if(c2.nume <0) { c2.nume =-nume; } else { c2.deno =-deno; } } else { c2.nume =-nume; c2.deno =deno; } return c2; } //用于测试的main()函数 void main() { CFraction c1,c2,c; cout<<"请您输入一个分数c1:(以a b的形式输入)"; cin>>c1; cout<<"请您输入一个分数c1:(以a b的形式输入)"; cin>>c2; cout<<"c1为:"; cout<<c1; cout<<"c2为:"; cout<<c2; cout<<"下面比较两个时间大小:\n"; if (c1>c2) cout<<"c1>c2"<<endl; if (c1<c2) cout<<"c1<c2"<<endl; if (c1==c2) cout<<"c1=c2"<<endl; if (c1!=c2) cout<<"c1≠c2"<<endl; if (c1>=c2) cout<<"c1≥c2"<<endl; if (c1<=c2) cout<<"c1≤c2"<<endl; cout<<endl; cout<<"c1+c2的数值为:"; c=c1+c2; cout<<c; cout<<endl; cout<<"c1-c2的数值为:"; c=c1-c2; cout<<c; cout<<endl; cout<<"c1*c2的数值为:"; c=c1*c2; cout<<c; cout<<endl; cout<<"c1/c2的数值为:"; c=c1/c2; cout<<c; cout<<endl; cout<<"对c1取反的结果为:"; c=-c1; cout<<c; cout<<endl; }
- 第九周 任务三
- 第九周 任务三
- 第九周任务三
- 第九周任务三
- 第九周任务三
- 第九周 任务三
- 第九周任务三
- 第九周任务(三)
- 第九周任务(三)
- 第九周任务(三)
- 第九周作业任务三
- 第九周实验报告任务三
- 第九周实验报告 任务三
- 第九周任务三: CFraction 类的扩展
- 第九周任务三 分数 输入输出运算符重载
- 第九周任务三之流运算符的重载
- C++第九周任务-项目三(分数类)
- 第九周任务
- “android源码目录”/mediatek/source/frameworks路径下的源文件编译
- JVM运行情况分析
- 获取SIM/UIM卡的联系人容量及已用容量信息
- 开源搜索引擎
- 明天测试下最新clang complete,希望已经搞好了!
- 第九周任务三
- 第九周任务(五)
- C语言 实现 顺序栈
- android TextView的常用属性介绍
- HTTP请求头概述
- 将顺序的数据颠倒,复杂度为n和二分之n的算法
- [wp7软件]wp7~~HTC官方软件~~集合贴~~
- FLEX嵌入JSP中
- undefined reference to `__aeabi_unwind_cpp_pr0' 问题解决办法