第九周实验报告(三)
来源:互联网 发布:数据加密系统 编辑:程序博客网 时间:2024/06/01 10:01
接第八周实验任务3,定义分数类中<<与>>运算符重载,实现分数类的输入与输出。
#include <iostream> using namespace std; class CFraction { private: int nu; int de; char c; public: CFraction(int nu = 0,int de = 1); void set(int nu = 0,int de = 1); void input(); void simplify(); void output(); CFraction operator +(CFraction &a); CFraction operator -(CFraction &a); CFraction operator *(CFraction &a); CFraction operator /(CFraction &a); 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); friend ostream & operator <<(ostream &,CFraction &); friend istream & operator >>(istream &,CFraction &); }; ostream & operator <<(ostream &output,CFraction &t){ output << t.nu << "/" <<t.de; cout<<endl; return output; } istream & operator >>(istream &input,CFraction &t){ input>>t.nu>>t.de; return input;}void CFraction::input() { cout<<"输入分数按照nu/de格式"<<endl; cin>>nu; cin>>c; cin>>de; } void CFraction::simplify() { int x; if(nu>de) x = de; else x = nu; for(int i = 2; i<=x;i++) { if(nu%i==0&& de%i==0) { nu = nu/i; de = de/i; i = 1; } } } CFraction::CFraction(int n,int d) { nu = n; de = d; } CFraction CFraction::operator +(CFraction &a) { CFraction c; c.nu = a.nu*de+nu*a.de; c.de = a.de*de; return c; } CFraction CFraction::operator -(CFraction &a) { CFraction c; c.nu = nu*a.de - a.nu*de; c.de = a.de*de; return c; } CFraction CFraction::operator *(CFraction &a) { CFraction c; c.nu = nu*a.nu; c.de = de*a.de; return c; } CFraction CFraction::operator /(CFraction &a) { CFraction c; c.nu = a.nu*de; c.de = a.de*nu; return c; } CFraction CFraction::operator -() { CFraction c; c.de = -de; c.nu = -nu; return c; } void CFraction::output() { cout<<nu<<"/"<<de<<endl; } bool CFraction::operator>(CFraction &c) { if((nu*c.de - de*c.nu)>0) return true; else return false; } // 分数比较大小 bool CFraction::operator<( CFraction &c) { if((nu*c.de - de*c.nu)<0) return true; else return false; } // 分数比较大小 bool CFraction::operator==( CFraction &c) { if((nu*c.de - de*c.nu)==0) return true; else return false; } // 分数比较大小 bool CFraction::operator!=( CFraction &c) { if((nu*c.de - de*c.nu)==0) return false; else return true; } // 分数比较大小 bool CFraction::operator>=( CFraction &c) { if((nu*c.de - de*c.nu)>=0) return true; else return false; } // 分数比较大小 bool CFraction::operator<=( CFraction &c) { if((nu*c.de - de*c.nu)<0) return true; else return false; } int main() { CFraction a,b,c; int t; cout<<"请输入分数a的值"<<endl; cin>>a; cout<<"请输入分数b的值"<<endl; cin>>b; cout<<"计算分数"; cout<<a; cout<<"与"; cout<<b; cout<<"和"; c=a+b; c.simplify(); cout<<c; cout<<"差"; c=a-b; c.simplify(); cout<<c; cout<<"乘"; c=a*b; c.simplify(); cout<<c; cout<<"除"; c=a/b; c.simplify(); cout<<c; cout<<"对分数"; cout<<a; cout<<"求反"; -a; cout<<a; if (a>b) cout<<"大于"<<endl; if (a<b) cout<<"小于"<<endl; if (a==b) cout<<"等于"<<endl; system("pause"); return 0; }
- 第九周实验报告(三)
- 第九周实验报告(三)
- 第九周实验报告(三)
- 第九周C++实验报告(三)
- 第九周实验报告三
- 第九周实验报告三
- 第九周实验报告三
- 第九周实验报告任务三
- 第九周实验报告 任务三
- 第九周实验报告
- 第九周实验报告
- 第九周实验报告(2)
- 第九周实验报告(1)
- 第九周实验报告(一)
- 第九周实验报告(2)
- 第九周实验报告(3)
- 第九周实验报告(—)
- 第九周实验报告(二)
- web第一章
- I - An Easy Problem!解题报告(来自网络)
- linux网络编程之:TCP[13] flag combinations[转]
- stringstream用法(载)
- shape画listview分割线
- 第九周实验报告(三)
- BIRT 报表的动态交互性
- 11级_Java_曹建波4.17 数组
- 第9周任务4(建立一个二维数组类,完成矩阵的输入、输出、加、减、相等判断操作)
- 自考让我成就梦想
- spring 依赖注入方式总结详解
- birt 开发整理。 一些小技巧
- 简单看了一下源码的实现,这里贴一点个人觉得比叫重要的代码。
- git 使用详解(8)-- 分支HEAD