第九周任务3
来源:互联网 发布:一对一教学软件 编辑:程序博客网 时间:2024/05/17 04:50
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 臧鹏
* 完成日期: 2012 年 4 月 16 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:实现分数类中的<<和>>运算符重载,实现分数的输入和输出
* 程序头部的注释结束
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 臧鹏
* 完成日期: 2012 年 4 月 16 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:实现分数类中的<<和>>运算符重载,实现分数的输入和输出
* 程序头部的注释结束
*/
#include <iostream > using namespace std; int gcd(int,int); class CFraction {private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1); //构造函数,初始化用 void simplify(); //化简(使分子分母没有公因子) CFraction operator+(const CFraction &c);CFraction operator-(const CFraction &c);CFraction operator*(const CFraction &c);CFraction operator/(const CFraction &c);CFraction operator-(); //取反一目运算 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); friend istream & operator >>(istream &in,CFraction &t); friend ostream & operator <<(ostream &out,CFraction &t); };istream & operator >>(istream &in,CFraction &t) { char a; while(1) { cout<<"请输入分数(形式 x/x):"<<endl; cin>>t.nume>>a>>t.deno; if(a != '/'||t.deno ==0)//注意符号不对和分母为0的情况 { cout<<"格式不正确,请重新输入:"<<endl; continue; } else break; } return cin; } ostream & operator <<(ostream &out,CFraction &t)//在输出时进行化简 { if(t.nume ==0) { cout<<"0"<<endl; } else { int m = gcd(t.nume,t.deno); cout<<(t.nume/m)<<'/'<<(t.deno/m)<<endl; } return out; } //构造函数,初始化用 CFraction::CFraction(int nu,int de) { if(de!=0) { nume = nu; deno = de; } else { cout << "分母不能为零!"; exit(0); } } //化简(使分子分母没有公因子) void CFraction::simplify() { int n ; n = gcd(nume,deno); nume = nume/n; deno = deno/n; } //求最大公约数的函数 int gcd(int x,int y) { int r; while(y!=0) { r = x%y; x = y; y = r; } return x; } CFraction CFraction::operator+(const CFraction &c){CFraction s;s.deno = deno*c.deno;s.nume = nume*c.deno+deno*c.nume;s.simplify();return s;}CFraction CFraction::operator-(const CFraction &c){CFraction s;s.deno = deno*c.deno;s.nume = nume*c.deno+deno*c.nume;s.simplify();return s;}CFraction CFraction::operator*(const CFraction &c){CFraction s;s.deno = deno*c.deno;s.nume = nume*c.nume;s.simplify();return s;}CFraction CFraction::operator/(const CFraction &c){CFraction s;s.deno = deno*c.nume;s.nume = nume*c.deno;s.simplify();return s;} //取反一目运算 CFraction CFraction::operator-(){CFraction s;s.deno=-deno;s.nume=nume;s.simplify();return s;}// 分数比较大小 bool CFraction::operator>(const CFraction &c) { int this_nume,c_nume,common_deno; this_nume=nume*c.deno; // 计算分数通分后的分子,同分母为deno*c.deno c_nume=c.nume*deno; common_deno=deno*c.deno; if (this_nume>c_nume&&common_deno>0||this_nume<c_nume&&common_deno<0) // 将通分后的分子比较大小return true; else return false; } // 分数比较大小 bool CFraction::operator<(const CFraction &c) { int this_nume,c_nume,common_deno; this_nume=nume*c.deno; c_nume=c.nume*deno; common_deno=deno*c.deno; if ((this_nume-c_nume)*common_deno<0) return true; else return false; } bool CFraction::operator==(const CFraction &c){if(*this>c||*this<c)return false;elsereturn true;}bool CFraction::operator!=(const CFraction &c){if(*this<c||*this>c)return false;elsereturn true;}bool CFraction::operator>=(const CFraction &c){if(*this<c)return false;elsereturn true;}bool CFraction::operator<=(const CFraction &c){if(*this>c)return false;elsereturn true;}int main() { CFraction x,y,s;cout<<"请输入两个分数"<<endl;cin>>x>>y; cout<<"分数x="<<x; cout<<"y="<<y; s=x+y; cout<<"x+y="; cout<<s; s=x-y; cout<<"x-y="; cout<<s; s=x*y; cout<<"x*y="; cout<<s; s=x/y; cout<<"x/y="; cout<<s; s=-x+y; cout<<"-x+y="; cout<<s; cout<<x; ; if (x>y) cout<<"大于"<<endl; if (x<y) cout<<"小于"<<endl; if (x==y) cout<<"等于"<<endl; cout<<y; cout<<endl; system("pause"); return 0; }
感言:这三个任务都比较简单。。但我好像没有真正的弄明白 这样改变 输入输出 有什么用处。。。
- 第九周任务3
- 第九周任务3
- 第九周任务3
- 第九周任务3
- 第九周任务3
- 第九周 任务3
- 第九周上机任务3
- 《第九周实验报告任务3》
- 第九周实验报告(任务3)
- 第九周任务3—利息计算器!
- 第九周任务3:个人所得税计算器
- 第九周任务
- 第九周 任务一
- 第九周 任务二
- 第九周 任务三
- 第九周 任务五
- 第九周任务一
- 第九周 任务四
- xml+css样式表
- 简单的matlab分布式计算
- uboot copy_from_nand代码详解
- poj 1573
- MSSQL 中将数字转换为字符串
- 第九周任务3
- 统一码Unicode(UTF-8 UTF-16 GB18030等)的理解
- 11个重要的数据库设计规则
- STM32精确延时(非中断,非ST库函数)
- wireshark 插件开发
- VC、VS2005调试技巧
- Unity3D优化之Optimizing Script Performance
- Struts2 参数 表单 提交 对象 POJO 方法大全
- scanf家族 正则式