第9周任务3(实现分数类中<<和>>运算符的重载)
来源:互联网 发布:传奇映射外网端口教程 编辑:程序博客网 时间:2024/05/16 05:26
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: * 作 者: 臧鹏 * 完成日期: 2012 年 4 月 16 日 * 版 本 号: * 对任务及求解方法的描述部分 * 输入描述: * 问题描述:实现分数类中的<<和>>运算符重载,实现分数的输入和输出* 程序头部的注释结束 */ #include<iostream> using namespace std; int gcd(int x,int y) ;//声明为普通函数class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1); //构造函数,初始化用 void Set(int nu=0,int de=1); //置值,改变值时用 CFraction operator+(CFraction &t); CFraction operator-(CFraction &t); CFraction operator*(CFraction &t); CFraction operator/(CFraction &t); friend CFraction operator-(CFraction &t);//取反 bool operator>(CFraction &t); bool operator<(CFraction &t); bool operator>=(CFraction &t); bool operator<=(CFraction &t); bool operator==(CFraction &t); bool operator!=(CFraction &t); void Simplify(int n); //化简(使分子分母没有公因子) friend istream & operator >>(istream &in,CFraction &t);friend ostream & operator <<(ostream &out,CFraction &t);}; CFraction::CFraction(int nu,int de) { nume = nu; deno = de; } void CFraction::Set(int nu,int de) //置值,改变值时用 { if(de !=0) { nume = nu; deno = de; } else { cout<<"分母不能为零"<<endl; exit(0); } nume = nu; deno = de; } 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+(CFraction &t) { CFraction cf; if(deno==t.deno) //分母相同时比较分子 { cf.deno=deno; cf.nume=nume+t.nume; } else //否则同分比较分子 { cf.deno=deno*t.deno; cf.nume=nume*t.deno+deno*t.nume; } return cf; } CFraction CFraction:: operator-(CFraction &t) { CFraction cf; if(deno==t.deno) { cf.deno=deno; cf.nume=nume-t.nume; } else { cf.deno=deno*t.deno; cf.nume=t.deno*nume-t.nume*deno; } return cf; } CFraction CFraction:: operator*(CFraction &t) { CFraction cf; cf.deno = deno*t.deno; cf.nume = nume*t.nume; return cf; } CFraction CFraction:: operator/(CFraction &t) { CFraction cf; cf.deno = deno*t.nume; cf.nume = nume*t.deno; return cf; } CFraction operator-(CFraction &t)//取反 { CFraction cf; cf.deno = -t.deno; cf.nume = t.nume; return(cf); } bool CFraction::operator>(CFraction &t) { if(nume*t.deno>t.nume*deno) return true; else return false; } bool CFraction::operator<(CFraction &t) { if(nume*t.deno<t.nume*deno) return true; else return false; } bool CFraction::operator>=(CFraction &t) { if(nume*t.deno>t.nume*deno||nume*t.deno==t.nume*deno) return true; else return false; }bool CFraction::operator<=(CFraction &t) { if(nume*t.deno<t.nume*deno||nume*t.deno==t.nume*deno) return true; else return false; } bool CFraction::operator==(CFraction &t) { if(nume*t.deno==t.nume*deno) return true; else return false; } bool CFraction::operator!=(CFraction &t) { if(!operator==(t)) return true; else return false; } 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;}int main () { CFraction cf1,cf2,cf3; cin>>cf1;cout<<"cf1 = "<<cf1;cin>>cf2;cout<<"cf2 = "<<cf2; cout<<"cf1*cf2= "<<cf1*cf2; cout<<"cf1-cf2= "<<cf1-cf2; cout<<"cf1+cf2= "<<cf1+cf2; cout<<"cf1/cf2= "<<cf1/cf2; cout<<"-cf1 = "<<-cf1; if (cf1>cf2) cout<<"cf1>cf2"<<endl; if (cf1<cf2) cout<<"cf1<cf2"<<endl; if (cf1==cf2) cout<<"cf1=cf2"<<endl; if (cf1!=cf2) cout<<"cf1≠cf2"<<endl; if (cf1>=cf2) cout<<"cf1≥cf2"<<endl; if (cf1<=cf2) cout<<"cf1≤cf2"<<endl; system("pause"); return 0; }
经验积累:
程序中的 gcd() 求最大公约数的函数要声明为一般函数,因为程序中的友元函数和成员函数都要用到,再就是输入函数需要进行认真的判断。
- 第9周任务3(实现分数类中<<和>>运算符的重载)
- 《C++第九周实验报告3-1》----接第8周任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出
- 第8周任务3(实现分数类中的运算符重载)
- 第9周-任务3-分数类中运算符重载
- 第九周C++任务三。定义分数类中<<和>>运算符重载,实现分数的输入输出
- 第九周任务三之定义分数类中<<和>>运算符重载,实现分数的输入输出
- C++第九周【任务三】定义分数类中<<和>>运算符重载,实现分数的输入输出
- 第8周-任务3-实现分数类中的运算符重载
- c++第八周【任务3】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
- 第8章-任务4-实现分数类中的运算符重载(分数与整数运算)
- 第九周任务三--定义分数类中<<和>>运算符重载,实现分数的输入输出,使程序读起来更自然。
- 第九周实验报告(任务三)【分数类中《和》运算符重载】
- 第九周实验报告任务3 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 第八周任务三实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
- 第7周-项目3-分数类中的运算符重载-拓展分数的加减乘除(分数与整型 )
- 第9周项目3分数类的运算符重载(续)
- 第九周 任务2 定义TIME类中的《和》运算重载,实现分数的输入输出。
- 第八周实验报告任务3 实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除,求反,比较6种关系的运算。
- 慎用枚举型的ordinal()方法~
- Linux程序设计笔记-进程间通信
- Error: This attribute must be localized. 解决方法
- Android基础之项目 环境 Exceptin
- Ext等待动画Ext.MessageBox.wait的使用
- 第9周任务3(实现分数类中<<和>>运算符的重载)
- 《第九周任务三》定义分数类中《和》运算符重载,实现分数的输入输出,改造源程序中对运算结果显示方式,使程序读起来更自然
- VC++各工程项目的含义
- HNOI营业额统计
- 第九周任务报告5
- 一段html代码将百度搜索栏包含到你的页面来
- 第九周实验报告(二)
- 第九周实验报告3
- 第九周实验报告任务三