C++走向远洋——55(项目一3、分数类的重载、>>
*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:text.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年5月25日
* 版本号:V1.0
* 问题描述:分数类的重载取倒数+输入输出重载
* 程序输入:无
* 程序输出:见运行结果
*/
#include<iostream>
#include<Cmath>
using namespace std; class CFraction{
private:
int nume; //分子
int deno; //分母
public:
CFraction(int nu=0,int de=0);
//输入输出的重载
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(const CFraction &n); //分数相加
CFraction operator-(const CFraction &n); //分数相减
CFraction operator*(const CFraction &n); //分数相乘
CFraction operator/(const CFraction &n); //分数相除
void display(); //输出分数
void simplify(); //分数化简
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);
CFraction operator+(); //取正一目运算
CFraction operator-(); //取反一目运算
CFraction operator~(); //分数取倒数
}; CFraction::CFraction(int nu,int de) //构造函数
{
nume=nu;
deno=de;
}
void CFraction::display() //输出函数
{
cout<<nume<<"/"<<deno<<endl;
}
void CFraction::simplify() //分数化简
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
if(nume==0)
deno=0;
else{
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
deno/=n; // 化简
nume/=n;
if (deno<0) // 将分母转化为正数
{
deno=-deno;
nume=-nume;
}
}
}
istream &operator>>(istream &in,CFraction &x)
{
char ch;
while(1)
{
cin>>x.nume>>ch>>x.deno;
if (x.deno==0)
cerr<<"分母为0, 请重新输入\n";
else if(ch!='/')
cerr<<"格式错误(形如m/n)! 请重新输入\n";
else
break;
}
return cin;
}
// 重载输出运算符<<
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
} CFraction CFraction::operator +(const CFraction &n) //定义分数相加
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno+n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator -(const CFraction &n) //定义分数相减
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno-n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator *(const CFraction &n) //定义分数相乘
{
CFraction t;
t.deno=n.deno*this->deno;
t.nume=n.nume*this->nume;
t.simplify();//化简
return t;
}
CFraction CFraction::operator /(const CFraction &n) //定义分数相除
{
CFraction t;
t.deno=n.nume*this->deno;
t.nume=n.deno*this->nume;
t.simplify();//化简
return t;
} //比较运算符重载
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) return true;
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;
return false;
} // 分数比较大小
bool CFraction::operator==(const CFraction &c)
{
if (*this!=c) return false;
return true;
} // 分数比较大小
bool CFraction::operator!=(const CFraction &c)
{
if (*this>c || *this<c) return true;
return false;
} // 分数比较大小
bool CFraction::operator>=(const CFraction &c)
{
if (*this<c) return false;
return true;
} // 分数比较大小
bool CFraction::operator<=(const CFraction &c)
{
if (*this>c) return false;
return true;
}
// 分数取正号
CFraction CFraction:: operator+()
{
return *this;
} // 分数取负号
CFraction CFraction:: operator-()
{
CFraction x;
x.nume=-this->nume;
x.deno=this->deno;
return x;
}
CFraction CFraction::operator ~()
{
CFraction t;
t.deno=this->nume;
t.nume=this->deno;
return t;
}
int main()
{
CFraction a,b;
CFraction c;
cin>>a>>b;
c=a+b;
cout<<"c=";
c.display();
cout<<" "<<c<<"重载<<后输出"<<endl;
c=a*b;
cout<<"c=";
c.display();
cout<<" "<<c<<endl;
c=a-b;
cout<<"c=";
c.display();
cout<<" "<<c<<endl;
c=a/b;
cout<<"c=";
c.display();
cout<<" "<<c<<endl;
if(a>b)
cout<<"a>b"<<endl;
c=~a;
c.display();
cout<<" "<<c<<endl;
return 0;
}
C++走向远洋——55(项目一3、分数类的重载、>>的更多相关文章
- C++走向远洋——54(项目一2、分数类的重载、取倒数)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++走向远洋——53(项目一1、分数类的重载、加减乘除、比较)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++走向远洋——(项目二、存储班长信息的学生类、派生)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- 连分数(分数类模板) uva6875
//连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...
- OC2_分数类
// // Fraction.h // OC2_分数类 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhangxu ...
- 第十七周oj刷题——Problem B: 分数类的四则运算【C++】
Description 编写分数类Fraction,实现两个分数的加.减.乘和除四则运算.主函数已给定. Input 每行四个数,分别表示两个分数的分子和分母,以0 0 0 0 表示结束. Outpu ...
- Problem F: 分数类的类型转换
Description 封装一个分数类Fract,用来处理分数功能和运算,支持以下操作: 1. 构造:传入两个参数n和m,表示n/m:分数在构造时立即转化成最简分数. 2. show()函数:分数 ...
- Problem E: 分数类的输出
Problem E: 分数类的输出 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2699 Solved: 1227[Submit][Status][ ...
- java的分数类
概述 分数类在算法中非常重要, 而在java中不那么重要,java基础类库提供 了biginteger了,提供类似方式, package 组合数学; public class Fraction { p ...
随机推荐
- 吴裕雄--天生自然 JAVA开发学习:包(package)
package pkg1[.pkg2[.pkg3…]]; package net.java.util; public class Something{ ... } package animals; i ...
- Python语言学习:homework1
'''购物车程序1.启动程序后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4.可随时退出,退出时,打印已购买商品和余额 ...
- TPO1-3Timberline Vegetation on Mountains
At the upper timberline the trees begin to become twisted and deformed. This is particularly true fo ...
- Perl: print @globbing."\n"; 和 print @globbing; 不一样,一个已经转换为数组元素个数了
48 print @globbing."\n"; 输出: 3
- C2. Power Transmission (Hard Edition)(线段相交)
This problem is same as the previous one, but has larger constraints. It was a Sunday morning when t ...
- IP命令介绍
ip指令可以显示或操作路由.网络设备.设置路由策略和通道 1.语法 ip [选项] Object COMMAND [help] Object对象可以是: link 网络设备.addr 设备的协议 ...
- linux下java调用C
linux下java调用C 分类: linux2012-05-22 09:12 1529人阅读 评论(0) 收藏 举报 javalinuxmakefilegccclasscommand 下面是在ubu ...
- python学习笔记(23)-异常处理
#异常处理与调试 #异常:在运行代码过程中遇到的任何错误,带有error字样的都是异常 #异常处理,对代码中所有可能出现的异常进行的处理 #1.处理某个错误 2,处理某个类型的错误 3 有错就抓 一. ...
- java后台使用HttpServletRequest接收参数转换为model
当前端需要传图片时,后台用MultipartHttpServletRequest接收参数,request接收过来的参数有很多弊端,需要包装成自己的model就得做转化 弊端: 1.所接收的参数类型无法 ...
- BigDecimal不整除的一个异常java.lang.ArithmeticException
转载地址:http://blog.csdn.net/jobjava/article/details/6764623 金额的数据类型是BigDecimal 通过BigDecimal的divide方法进行 ...