您的位置:首页 > 其它

第八周 项目三(1):分数类中的运算符重载

2015-04-24 13:06 567 查看
问题及代码:

/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:Project3.cpp
* 作    者:李楠
* 完成日期:2015年4月24日
* 版 本 号:v1.0
*
* 问题描述:(1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、
比较(6种关系)的运算。可以在第4周分数类代码的基础上开始工作。
* 程序输入:略
* 程序输出:略
*/
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int gcd(int m,int n);
class CFraction
{
private:
int nume;  // 分子
int deno;  // 分母
public:
CFraction(int nu=0,int de=1);   //构造函数,初始化用
void set(int nu=0,int de=1);    //置值,改变值时用
void input();               //按照"nu/de"的格式,如"5/2"的形式输入
void simplify();            //化简(使分子分母没有公因子)
void amplify(int n);        //放大n倍,如2/3放大5倍为10/3
void output(int style=0);   //输出:以8/6为例,style为0时,原样输出8/6;
//style为1时,输出化简后形式4/3;
//style为2时,输出1(1/3)形式,表示一又三分之一;
//style为3时,用小数形式输出,如1.3333;
//默认方式0
CFraction operator+(const CFraction &c2);
CFraction operator-(const CFraction &c2);
CFraction operator*(const CFraction &c2);
CFraction operator/(const CFraction &c2);
bool operator > (CFraction &c1);
bool operator < (CFraction &c1);
bool operator >= (CFraction &c1);
bool operator <= (CFraction &c1);
bool operator == (CFraction &c1);
bool operator != (CFraction &c1);
};
CFraction::CFraction(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
else
{
cout<<"分母不能为0,初始化失败!"<<endl;
exit(0);
}
}

void CFraction::set(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
else
{
cout<<"分母不能置为0,更改失败!"<<endl;
exit(0);
}
}

void CFraction::input()
{
char p;
cin>>nume>>p>>deno;
if(p!='/')
{
cout<<"输入不符合格式!"<<endl;
exit(0);
}
else if(deno==0)
{
cout<<"分母不能为0,输入失败!"<<endl;
exit(0);
}
}

int gcd(int m,int n)
{
int g;
if(n==0)
g=m;
else
g=gcd(n,m%n);
return g;
}
void CFraction::simplify()
{
int l;
l=gcd(nume,deno);
nume=nume/l;
deno=deno/l;
}

void CFraction::amplify(int n)
{
nume=nume*n;
}

void CFraction::output(int style)
{
int a;
float b;
if(style==0)
cout<<nume<<'/'<<deno<<endl;
else if(style==1)
{
simplify();
cout<<nume<<'/'<<deno<<endl;
}
else if(style==2)
{
a=nume/deno;
cout<<a<<'('<<nume%deno<<'/'<<deno<<')'<<endl;
}
else if(style==3)
{
b=float(nume)/float(deno);
cout<<b<<endl;
}
else
cout<<"没有此种形式!"<<endl;
}
CFraction CFraction::operator+(const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.deno+c2.nume*deno;
c.deno=deno*c2.deno;
c.simplify();
return c;
}
CFraction CFraction::operator-(const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.deno-c2.nume*deno;
c.deno=deno*c2.deno;
c.simplify();
return c;
}
CFraction CFraction::operator*(const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.nume;
c.deno=deno*c2.deno;
c.simplify();
return c;
}
CFraction CFraction::operator/(const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.deno;
c.deno=deno*c2.nume;
c.simplify();
return c;
}
bool CFraction::operator > (CFraction &c1)
{
CFraction c;
c=*this-c1;
if(c.nume/deno>0)
return true;
else
return false;
}
bool CFraction::operator < (CFraction &c1)
{
CFraction c;
c=*this-c1;
if(c.nume/deno<0)
return true;
else
return false;
}
bool CFraction::operator >= (CFraction &c1)
{
return !(*this<c1);
}
bool CFraction::operator <= (CFraction &c1)
{
return !(*this>c1);
}
bool CFraction::operator == (CFraction &c1)
{
if (*this!=c1)
return false;
else
return true;
}
bool CFraction::operator != (CFraction &c1)
{
if (*this>c1||*this<c1)
return true;
else
return false;
}
int main()
{
CFraction cf;
cout<<"默认值输出:";
cf.output();
cout<<"改变值后输出:";
cf.set(6,4);
cf.output(1);
cout<<"输入:";
cf.input();
cout<<"原样输出:";
cf.output(0);
cout<<"放大3倍:";
cf.amplify(3);
cf.output(0);
cout<<"化简形式:";
cf.output(1);
cout<<"真分数形式:";
cf.output(2);
cout<<"小数形式:";
cf.output(3);
CFraction x(4,5),y(-2,6);
CFraction p;
cout<<"分数:";
cout<<"x=";
x.output();
cout<<"     y=";
y.output();
p=x+y;
cout<<"x+y=";
p.output();
p=x-y;
cout<<"x-y=";
p.output();
p=x*y;
cout<<"x*y=";
p.output();
p=x/y;
cout<<"x/y=";
p.output();
if (x>y) cout<<"x大于y"<<endl;
if (x<y) cout<<"x小于y"<<endl;
if (x==y) cout<<"x等于y"<<endl;
if (x>=y) cout<<"x大于等于y"<<endl;
if (x<=y) cout<<"x小于等于y"<<endl;
if (x!=y) cout<<"x不等于y"<<endl;
cout<<endl;
return 0;
}


运行结果:



知识点总结:

在第四周分数类雏形的基础上又添加了加减乘除以及比较大小的函数,函数中要注意的是比较两数大小的时候,不能直接写*this>或者<形参,而是应该判断分子分母是否大于或小于零。而且定义大于等于,小于等于,等于和不等于函数的时候都可以根据已定义的函数来写~

学习心得:

写除法那个函数的时候不知道如果除以的是0该怎么处理啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: