问题及代码:
/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:Project3.cpp
* 作 者:李楠
* 完成日期:2015年4月24日
* 版 本 号:v1.0
*
* 问题描述:(2)在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。
例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
* 程序输入:略
* 程序输出:略
*/
#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);
CFraction operator+(const int a);
CFraction operator-(const int a);
CFraction operator*(const int a);
CFraction operator/(const int a);
friend CFraction operator+(const int a,const CFraction &c2);
friend CFraction operator-(const int a,const CFraction &c2);
friend CFraction operator*(const int a,const CFraction &c2);
friend CFraction operator/(const int a,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/c.deno>0)
return true;
else
return false;
}
bool CFraction::operator < (CFraction &c1)
{
CFraction c;
c=*this-c1;
if(c.nume/c.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;
}
CFraction CFraction::operator+(const int a)
{
CFraction c;
c.nume=nume+deno*a;
c.deno=deno;
c.simplify();
return c;
}
CFraction CFraction::operator-(const int a)
{
CFraction c;
c.nume=nume-deno*a;
c.deno=deno;
c.simplify();
return c;
}
CFraction CFraction::operator*(const int a)
{
CFraction c;
c.nume=nume*a;
c.deno=deno;
c.simplify();
return c;
}
CFraction CFraction::operator/(const int a)
{
CFraction c;
c.nume=nume;
c.deno=deno*a;
c.simplify();
return c;
}
CFraction operator+(const int a,const CFraction &c2)
{
CFraction c;
c.nume=c2.nume+c2.deno*a;
c.deno=c2.deno;
c.simplify();
return c;
}
CFraction operator-(const int a,const CFraction &c2)
{
CFraction c;
c.nume=c2.deno*a-c2.nume;
c.deno=c2.deno;
c.simplify();
return c;
}
CFraction operator*(const int a,const CFraction &c2)
{
CFraction c;
c.nume=c2.nume*a;
c.deno=c2.deno;
c.simplify();
return c;
}
CFraction operator/(const int a,const CFraction &c2)
{
CFraction c;
c.nume=c2.deno*a;
c.deno=c2.nume;
c.simplify();
return c;
}
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();
p=x*3;
p=3*y;
p=3+x;
p=y+3;
p=10-x;
p=y-6;
p=y/10;
p=4/x;
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;
}
运行结果:
知识点总结:
有一部分没有用output输出,因为老贺说得好嘛~用单步执行测试更方便!而且涉及到二目的就要用友元函数了。
学习心得:
最开始写分数类雏形的时候就想着如果将分数加减乘除该怎么办呢,现在一点点地解决了~很嗨森~