问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:Project.cpp
*作 者:chenqin.
*完成日期:2015年5月20日
*版 本 号:v1.0
*
*问题描述:(1)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。
(2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
*程序输入:略
*程序输出:略
*/
#include <iostream>
#include <cmath>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1):nume(nu),deno(de){} //构造函数,初始化用
void simplify(); //化简(使分子分母没有公因子)
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(const CFraction &c);
CFraction operator-(const CFraction &c);
CFraction operator*(const CFraction &c);
CFraction operator/(const CFraction &c);
CFraction operator-();//反一目运算符
CFraction operator+();//正一目运算符
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);
};
void CFraction::simplify() //分数化简
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
while(r=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<<"分母不能为零,请重新输入:";
else if(ch!='/')
cerr<<"格式错误,请重新输入:";
else
break;
}
return cin;
}
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
}
//分数的加减乘除运算
CFraction CFraction::operator+(const CFraction&c)
{
CFraction t;
t.nume=nume*c.deno+c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator-(const CFraction&c)
{
CFraction t;
t.nume=nume*c.deno-c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator*(const CFraction&c)
{
CFraction t;
t.nume=nume*c.nume;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator/(const CFraction&c)
{
CFraction t;
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.simplify();
return t;
}
//一目运算符
CFraction CFraction::operator+()
{
return *this;
}
CFraction CFraction::operator-()
{
CFraction x;
x.nume=-nume;
x.deno=deno;
return x;
}
CFraction CFraction::operator~()
{
CFraction x;
x.nume=deno;
x.deno=nume;
if(x.deno==0)
cout<<"分母不能为零"<<endl;
if(x.deno<0)
{
x.deno=-x.deno;
x.nume=-x.nume;
}
return x;
}
//六种关系的比较运算
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)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=deno*c.nume;
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)
return false;
else
return
true;
}
bool CFraction::operator!=(const CFraction &c)
{
if(*this>c||*this<c)
return true;
else
return false;
}
bool CFraction::operator>=(const CFraction &c)
{
if(*this<c)
return false;
else
return true;
}
bool CFraction::operator<=(const CFraction &c)
{
if(*this>c)
return false;
else
return true;
}
int main()
{
CFraction x,y,z;
cout<<"请输入x:";
cin>>x;
cout<<"请输入y:";
cin>>y;
z=x+y;
cout<<"x+y="<<z<<endl;
z=x-y;
cout<<"x-y="<<z<<endl;
z=x*y;
cout<<"x*y="<<z<<endl;
z=x/y;
cout<<"x/y="<<z<<endl;
cout<<"-x="<<-x<<endl;
cout<<"+x="<<+x<<endl;
cout<<"x的倒数="<<~x<<endl;
cout<<x;
if(x>y)
cout<<"大于"<<endl;
if(x<y)
cout<<"小于"<<endl;
if(x==y)
cout<<"等于"<<endl;
cout<<y;
cout<<endl;
return 0;
}
运行结果:
学习心得:
注意friend ostream &operator<<(ostream &out,CFraction x)这一行,若在x前加上引用符号则会出现大量编译错误,导致我整个人无语了好大半天。
文章浏览阅读624次。前言平时接触过多线程开发的童鞋应该都或多或少了解过线程池,之前发布的《阿里巴巴 Java 手册》里也有一条: 可见线程池的重要性。简单来说使用线程池有以下几个目的:线程是稀缺资源,不能频繁的创建。 解耦作用;线程的创建于执行完全分开,方便维护。 应当将其放入一个池子中,可以给其他任务进行复用。线程池原理谈到线程池就会想到池化技术,其中最核心的思想就是把..._说下你对线程池的理解,如何创建一个线程池与使用
文章浏览阅读3.1k次,点赞3次,收藏5次。NCBI下载的cram文件无法直接使用,需要先转成bam/sam文件,根据官网说明下载了cramtools,发现早已没有维护,报错如下:$ java -jar cramtools-3.0.jarError: Invalid or corrupt jarfile cramtools-3.0.jar所以就直接用samtools来转换,但是直接转换会报错:$ samtools view -b NA12878.final.cram > NA12878.bam &Failed to popu_cram转成bam
文章浏览阅读1.4w次,点赞33次,收藏222次。INS/GNSS组合导航,松耦合、紧耦合、深度耦合异同对比_gnss/ins
文章浏览阅读288次。SudokuTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 17997 Accepted: 8714 Special JudgeDescriptionSudoku is a very simple task. A square table with_数独 17997
文章浏览阅读486次。1.( )就是合适的时间、合适的地点、做合适的事A.职业习惯B.职业意识C.职业化D.职业道德错误正确答案:左边查询学生答案:未作答2.曾经的一项调查发现一些企业业绩不好,客户流失的最重要的原因是( )。A.企业员工素养达不到要求B.企业管理问题C.企业产品价格不好D.企业产品质量问题错误正确答案:左边查询学生答案:未作答3.用冰山模型说明职业素养构成时,我们把浮在水面上面的知识、技能等部分称为( )的职业素养。A.显性B.专业C.获得D.隐性错_a 职业习惯 b 职业道德 c 职业意识 d 职业化
文章浏览阅读7.9k次。NVivo是一款支持定性研究方法和混合研究方法的软件。它的编码比较查询是比较两个用户完成的编码,以衡量“评估者之间的可靠性”或用户之间编码的同意程度。它使您能够比较由两个用户或两组用户完成的编码。通过计算百分比一致性和“ Kappa系数”,这是提供了测量“评估者间可靠性”或用户之间一致性程度的方法。协议百分比是协议单位数除以数据项内的总度量单位,以百分比显示。 Kappa系数是一种统计量度,其中考虑了可能通过偶然性达成的协议量。创建编码比较查询在“浏览”选项卡上的“查询”组中,单击“ 编码比较”_nvivo kappa系数
文章浏览阅读382次。public class MainActivity extends AppCompatActivity { private TextView tvShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt_httppost hp = new httppost
文章浏览阅读2.5k次,点赞5次,收藏21次。最近在学习 c++, 在编译与链接过程中遇到了一些定义与声明的问题, 经过多处查阅资料, 基本解惑. 现记录与此, 希望让后面人少走些弯路.C++ 的头文件应该用什么扩展名?目前业界的常用格式如下:implementation file*.cpp*.cc*.cc*.cheader file*.hpp*.h++*.hh*.hxx*.h一句话: 建议 源文件使用 .cpp, 头文件使用 .hpp关于 implementation file 并没有什么说的, 使用._c++跨平台头文件声明
文章浏览阅读9.4k次。高版本AGP集成Tinker踩坑记录,加固包补丁如何生成,TInker补丁的管理规范。_android tinker
文章浏览阅读942次。Problem DescriptionGiven a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1],_problem description given a specified total t and a list of n integers, find
文章浏览阅读227次。问题:java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-java-8.0.23.jar:8.0.23] at com.mysql.cj.jdbc.exceptions.SQLError.crea..._mybatis client_plugin_auth is required
文章浏览阅读2.1k次。篇Log用于记录如何在Deepin 15.11下运行Final Fantasy XIV。(WeGame版本没有测试)结论:通过测试,这个游戏可以在Linux下和Android下完整的玩(硬件配置必须OK)。问题分析: 运行网游,需要知道的是有几个要点: 1.需要安装DirectX 2.需要中文支持 3.需要能打开登录界面。 踩的坑: FF14的首次..._deepin ff14