技术标签: output input c 测试 class 任务
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
* 作 者: 雷恒鑫
* 完成日期: 2012 年 04 月 14 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include<iostream.h>
#include"stdlib.h"
int gcd(int m, int n);
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
//构造函数及运算符重载的函数声明
CFraction(int nu=0,int de=1); //构造函数,初始化用
void Simplify(); //化简(使分子分母没有公因子)
void output(); //输出:以8/6为例,style为0时,输出8/6;
bool operator > (CFraction &t);
bool operator < (CFraction &t);
bool operator >= (CFraction &t);
bool operator <= (CFraction &t);
bool operator == (CFraction &t);
bool operator != (CFraction &t);
CFraction operator+(CFraction &c);
CFraction operator-(CFraction &c);
CFraction operator*(CFraction &c);
CFraction operator/(CFraction &c);
CFraction operator-();
friend ostream& operator << (ostream&,CFraction &);
friend istream& operator >> (istream&,CFraction &);
};
istream& operator >> (istream& input,CFraction & c)
{
input>>c.nume>>c.deno;
return input;
}
ostream& operator << (ostream& output,CFraction & c)
{
output<<c.nume<<'/'<<c.deno<<endl;
return output;
}
CFraction::CFraction(int nu,int de) //构造函数,初始化用
{
if (de!=0)
{
nume=nu;
deno=de;
}
else
{
cerr<<"初始化中发生错误,程序退出\n";
exit(0);
}
}
void CFraction::Simplify() //化简(使分子分母没有公因子)
{
int n;
if(nume < 0)
{
n = gcd(-nume, deno);
}
else
{
n = gcd(nume, deno);
}
nume = nume / n;
deno = deno / n;
}
// 求m,n的最大公约数
int gcd(int m, int n)
{
int r;
if (m<n){r=m;m=n;n=r;}
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
return n;
}
bool CFraction::operator > (CFraction &t)
{
CFraction c2,c3;
c2.nume =nume*t.deno ;
c3.nume =t.nume *deno;
if(c2.nume >c3.nume )
return true;
else
return false;
}
bool CFraction::operator < (CFraction &t)
{
CFraction c2,c3;
c2.nume =nume*t.deno ;
c3.nume =t.nume *deno;
if(c2.nume <c3.nume )
return true;
else
return false;
}
bool CFraction::operator >= (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1<t)
return false;
return true;
}
bool CFraction::operator <= (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1>t)
return false;
return true;
}
bool CFraction::operator == (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1<t)
return false;
if (c1>t)
return false;
return false;
}
bool CFraction::operator != (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1==t)
return false;
return true;
}
CFraction CFraction::operator+(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.deno ;
c3.nume =c.nume *deno;
c2.deno =deno*c.deno ;
c3.deno =c.deno *deno;
c4.nume=c2.nume +c3.nume ;
c4.deno =c2.deno ;
c4.Simplify ();
return c4;
}
CFraction CFraction::operator-(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.deno ;
c3.nume =c.nume *deno;
c2.deno =deno*c.deno ;
c3.deno =c.deno *deno;
c4.nume=c2.nume -c3.nume ;
c4.deno =c2.deno ;
c4.Simplify ();
return c4;
}
CFraction CFraction::operator*(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.nume ;
c2.deno =deno*c.deno ;
c2.Simplify ();
return c2;
}
CFraction CFraction::operator/(CFraction &c)
{
CFraction c2,c3;
c2.nume =c.deno ;
c2.deno =c.nume ;
c3.nume =nume*c2.nume ;
c3.deno =deno*c2.deno ;
c3.Simplify ();
return c3;
}
CFraction CFraction::operator-()
{
CFraction c2;
c2.nume =nume;
c2.deno =deno;
c2.Simplify ();
if(c2.nume<0 || c2.deno<0)
{
if(c2.nume <0)
{
c2.nume =-nume;
}
else
{
c2.deno =-deno;
}
}
else
{
c2.nume =-nume;
c2.deno =deno;
}
return c2;
}
//用于测试的main()函数
void main()
{
CFraction c1,c2,c;
cout<<"请您输入一个分数c1:(以a b的形式输入)";
cin>>c1;
cout<<"请您输入一个分数c1:(以a b的形式输入)";
cin>>c2;
cout<<"c1为:";
cout<<c1;
cout<<"c2为:";
cout<<c2;
cout<<"下面比较两个时间大小:\n";
if (c1>c2) cout<<"c1>c2"<<endl;
if (c1<c2) cout<<"c1<c2"<<endl;
if (c1==c2) cout<<"c1=c2"<<endl;
if (c1!=c2) cout<<"c1≠c2"<<endl;
if (c1>=c2) cout<<"c1≥c2"<<endl;
if (c1<=c2) cout<<"c1≤c2"<<endl;
cout<<endl;
cout<<"c1+c2的数值为:";
c=c1+c2;
cout<<c;
cout<<endl;
cout<<"c1-c2的数值为:";
c=c1-c2;
cout<<c;
cout<<endl;
cout<<"c1*c2的数值为:";
c=c1*c2;
cout<<c;
cout<<endl;
cout<<"c1/c2的数值为:";
c=c1/c2;
cout<<c;
cout<<endl;
cout<<"对c1取反的结果为:";
c=-c1;
cout<<c;
cout<<endl;
}
下面是改进后的程序:
#include<iostream.h>
#include"stdlib.h"
int gcd(int m, int n);
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
char unknown; // '/'的符号
public:
//构造函数及运算符重载的函数声明
CFraction(int nu=0,int de=1); //构造函数,初始化用
void Simplify(); //化简(使分子分母没有公因子)
void output(); //输出:以8/6为例,style为0时,输出8/6;
bool operator > (CFraction &t);
bool operator < (CFraction &t);
bool operator >= (CFraction &t);
bool operator <= (CFraction &t);
bool operator == (CFraction &t);
bool operator != (CFraction &t);
CFraction operator+(CFraction &c);
CFraction operator-(CFraction &c);
CFraction operator*(CFraction &c);
CFraction operator/(CFraction &c);
CFraction operator-();
friend ostream& operator << (ostream&,CFraction &);
friend istream& operator >> (istream&,CFraction &);
};
istream& operator >> (istream& input,CFraction & c)
{
input>>c.nume>>c.unknown>>c.deno;
return input;
}
ostream& operator << (ostream& output,CFraction & c)
{
output<<c.nume<<'/'<<c.deno<<endl;
return output;
}
CFraction::CFraction(int nu,int de) //构造函数,初始化用
{
if (de!=0)
{
nume=nu;
deno=de;
}
else
{
cerr<<"初始化中发生错误,程序退出\n";
exit(0);
}
}
void CFraction::Simplify() //化简(使分子分母没有公因子)
{
int n;
if(nume < 0)
{
n = gcd(-nume, deno);
}
else
{
n = gcd(nume, deno);
}
nume = nume / n;
deno = deno / n;
}
// 求m,n的最大公约数
int gcd(int m, int n)
{
int r;
if (m<n){r=m;m=n;n=r;}
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
return n;
}
bool CFraction::operator > (CFraction &t)
{
CFraction c2,c3;
c2.nume =nume*t.deno ;
c3.nume =t.nume *deno;
if(c2.nume >c3.nume )
return true;
else
return false;
}
bool CFraction::operator < (CFraction &t)
{
CFraction c2,c3;
c2.nume =nume*t.deno ;
c3.nume =t.nume *deno;
if(c2.nume <c3.nume )
return true;
else
return false;
}
bool CFraction::operator >= (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1<t)
return false;
return true;
}
bool CFraction::operator <= (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1>t)
return false;
return true;
}
bool CFraction::operator == (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1<t)
return false;
if (c1>t)
return false;
return false;
}
bool CFraction::operator != (CFraction &t)
{
CFraction c1;
c1.nume =nume;
c1.deno =deno;
if (c1==t)
return false;
return true;
}
CFraction CFraction::operator+(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.deno ;
c3.nume =c.nume *deno;
c2.deno =deno*c.deno ;
c3.deno =c.deno *deno;
c4.nume=c2.nume +c3.nume ;
c4.deno =c2.deno ;
c4.Simplify ();
return c4;
}
CFraction CFraction::operator-(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.deno ;
c3.nume =c.nume *deno;
c2.deno =deno*c.deno ;
c3.deno =c.deno *deno;
c4.nume=c2.nume -c3.nume ;
c4.deno =c2.deno ;
c4.Simplify ();
return c4;
}
CFraction CFraction::operator*(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.nume ;
c2.deno =deno*c.deno ;
c2.Simplify ();
return c2;
}
CFraction CFraction::operator/(CFraction &c)
{
CFraction c2,c3;
c2.nume =c.deno ;
c2.deno =c.nume ;
c3.nume =nume*c2.nume ;
c3.deno =deno*c2.deno ;
c3.Simplify ();
return c3;
}
CFraction CFraction::operator-()
{
CFraction c2;
c2.nume =nume;
c2.deno =deno;
c2.Simplify ();
if(c2.nume<0 || c2.deno<0)
{
if(c2.nume <0)
{
c2.nume =-nume;
}
else
{
c2.deno =-deno;
}
}
else
{
c2.nume =-nume;
c2.deno =deno;
}
return c2;
}
//用于测试的main()函数
void main()
{
CFraction c1,c2,c;
cout<<"请您输入一个分数c1:(以a/b的形式输入)";
cin>>c1;
cout<<"请您输入一个分数c1:(以a/b的形式输入)";
cin>>c2;
cout<<"c1为:";
cout<<c1;
cout<<"c2为:";
cout<<c2;
cout<<"下面比较两个时间大小:\n";
if (c1>c2) cout<<"c1>c2"<<endl;
if (c1<c2) cout<<"c1<c2"<<endl;
if (c1==c2) cout<<"c1=c2"<<endl;
if (c1!=c2) cout<<"c1≠c2"<<endl;
if (c1>=c2) cout<<"c1≥c2"<<endl;
if (c1<=c2) cout<<"c1≤c2"<<endl;
cout<<endl;
cout<<"c1+c2的数值为:";
c=c1+c2;
cout<<c;
cout<<endl;
cout<<"c1-c2的数值为:";
c=c1-c2;
cout<<c;
cout<<endl;
cout<<"c1*c2的数值为:";
c=c1*c2;
cout<<c;
cout<<endl;
cout<<"c1/c2的数值为:";
c=c1/c2;
cout<<c;
cout<<endl;
cout<<"对c1取反的结果为:";
c=-c1;
cout<<c;
cout<<endl;
}
文章浏览阅读408次。 在ubuntu 8.04下安装Oracle 11g2008年05月22日 星期四 11:02oracle 11g 数据库虽然提供了linux x86的版本,但是支持的linux版本只有Red Hat,Novell and Solaris 这几个,debian 和 ubuntu 不在支持之列,所以在ubuntu下安装就相对麻烦一些,请照着下文的方法一步一步的安装,不
文章浏览阅读166次。新东方在线中考网整理了《初一英语下册语法知识点全汇总》,供同学们参考。一. 情态动词can的用法can+动词原形,它不随主语的人称和数而变化。1. 含有can的肯定句:主语+can+谓语动词的原形+其他。2. 含有can的否定句:主语+can't+动词的原形+其他。3. 变一般疑问句时,把can提前:Can+主语+动词原形+其他? 肯定回答:Yes,主语+can。否定回答:No,主语+can't...._七年级下册计算机知识点
文章浏览阅读3k次。在平时开发中,可能会遇到UFUN函数没有的功能,比如创建PTP的加工程序(我目前没找到,哪位大神可以指点一下),可以使用Grip创建PTP,然后用UFUN函数UF_call_grip调用Grip程序。具体如下截图(左侧UFUN,右侧Grip程序):..._uf调用grip
文章浏览阅读156次。第一个:原生普通样式(随着主题不同,样式会变)第二个:原生普通样式-小icon第三个:自定义RatingBar 颜色第四个:自定义RatingBar DrawableRatingBar 各样式实现===============原生样式原生样式其实没什么好说的,使用系统提供的style 即可<RatingBarstyle="?android:attr/ratingBarStyleIndicator"android:layout_width=“wrap_cont.._ratingbar样式修改
文章浏览阅读4.6k次,点赞6次,收藏11次。安装vs2017:参考vs2017下载和安装。安装cmake3.12.3:cmake是一个工程文件生成工具。用户可以使用预定义好的cmake脚本,根据自己的选择(像是Visual Studio, Code::Blocks, Eclipse)生成不同IDE的工程文件。可以从它官方网站的下载页上获取。这里我选择的是Win32安装程序,如图所示:然后就是运行安装程序进行安装就行。配置glfw3...._vs2017的opengl环境搭建(完整篇)
文章浏览阅读976次。MLC NAND,UBIFS_ubifs warning
文章浏览阅读2.2k次。计算机系统的两种存储器形式介绍时间:2016-1-6计算机系统的存储器一般应包括两个部分;一个是包含在计算机主机中的主存储器,简称内存,它直接和运算器,控制器及输入输出设备联系,容量小,但存取速度快,一般只存放那些急需要处理的数据或正在运行的程序;另一个是包含在外设中的外存储器,简称外存,它间接和运算器,控制器联系,存取速度虽然慢,但存储容量大,是用来存放大量暂时还不用的数据和程序,一旦要用时,就..._计算机存储器系统采用的是主辅结构,主存速度快、容量相对较小,用于 1 分 程序,外
文章浏览阅读5.6k次。1. STEP 7(Simatic Manager):STEP 7或者Simatic Manager是西门子PLC编程最常用的软件开发环境。4. STEP 7 MicroWin:STEP 7 MicroWn是一款专门针对微型PLC(S7-200系列PLC)的编程软件,是Simatic Manager的简化版。如果需要与PLC系统配合使用,则需要与PLC编程工具进行配合使用。除了上述软件之外,西门子还提供了一些配套软件和工具,如PLC模拟器、硬件调试工具等,以帮助PLC编程人员快速地进行调试和测试。_西门子plc编程软件
文章浏览阅读36次。【代码】HashMap扩容。_hashma扩容
文章浏览阅读2.9k次。1mvn dependency:copy-dependencies2 项目右键 -> Maven -> Disable Maven Nature3 项目右键 -> Configure -> Convert to Maven Project_maven资源加载不全,怎么重新加载
文章浏览阅读527次。DMLDML的全称是Database management Language,数据库管理语言。主要包括以下操作:insert、delete、update、optimize。本篇对其逐一介绍INSERT数据库表插入数据的方式:1、insert的完整语法:(做项目的过程中将字段名全写上,这样比较容易看懂)单条记录插入语法:insert into table_name (column_name1,......_dml的全称是
文章浏览阅读136次。可以参考: http://git.oschina.net/jrain-group/ 组织下的Java Modbus支持库Modbus-系列文章1、虚拟成对串口(1)下载虚拟串口软件VSPD(可在百度中搜索)image.png(2)打开软件,添加虚拟串口。在设备管理中,看到如下表示添加成功。..._最好用的 modebus调试工具