您的位置:首页 > 其它

第八周任务四(分数类中的对象可以和整型数进行四则运算)

2012-04-16 18:42 375 查看
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:分数类
* 作    者:冯珍珍
* 完成日期:  2012   年   4   月    16 日
* 版 本 号:略
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述: 在任务3的基础上拓展。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;

class CFraction
{
private:
int nume;  // 分子
int deno;  // 分母
public:
CFraction(int nu=0,int de=1):nume(nu),deno(de){}
void simplify();
void display();
//以下为用友员函数实现分数与整数的四则运算的函数声明部分
friend CFraction operator+(int i, const CFraction &c);  //新加方法
friend CFraction operator-(int i, const CFraction &c);  //新加方法
friend CFraction operator*(int i, const CFraction &c);  //新加方法
friend CFraction operator/(int i, const CFraction &c);  //新加方法
friend CFraction operator+(const CFraction &c, int i);  //新加方法
friend CFraction operator-(const CFraction &c, int i);  //新加方法
friend CFraction operator*(const CFraction &c, int i);  //新加方法
friend CFraction operator/(const CFraction &c, int i);  //新加方法

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;
m=abs(deno);
n=abs(nume);
while(r=m%n)
{
m=n;
n=r;
}
deno /= n;
nume /= n;
if (deno < 0)
{
deno =- deno;
nume =- nume;
}
}

void CFraction::display()
{
cout << "(" << nume << "/" << deno << ")" << endl;
}

CFraction CFraction:: operator+()
{
return *this;
}

CFraction CFraction:: operator-()
{
CFraction x;

x.nume = -nume;

x.deno = deno;

return x;
}

bool CFraction::operator>(const CFraction &c)
{
int n, a, d;

n = nume * c.deno;

a = c.nume * deno;

d = deno * c.deno;

if (n > a && d > 0 || n < a && d < 0)
{
return true;
}

return false;
}

bool CFraction::operator<(const CFraction &c)
{
int n, a, d;

n = nume * c.deno;

a = c.nume * deno;

d = deno * c.deno;

if ((n - a) * d < 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;
}
}

//新加方法:
CFraction operator+ (const CFraction &c, int i)
{
CFraction t;

t.nume = c.nume  + c.deno * i;

t.deno = c.deno;

t.simplify();

return t;
}

CFraction operator- (const CFraction &c, int i)
{
CFraction t;

t.nume = c.nume - i * c.deno;

t.deno = c.deno;

t.simplify();

return t;
}

CFraction operator* (const CFraction &c, int i)
{
CFraction t;

t.nume = c.nume * i;

t.deno = c.deno;

t.simplify();

return t;
}

CFraction operator/(const CFraction &c, int i)
{
CFraction t;

t.deno *= i;

t.nume = c.nume;

t.simplify();

return t;
}

CFraction operator+ (int i, const CFraction &c)
{
CFraction t;

t.nume = c.nume  + c.deno * i;

t.deno = c.deno;

t.simplify();

return t;
}

CFraction operator- (int i, const CFraction &c)
{
CFraction t;

t.nume = c.nume - i * c.deno;

t.deno = c.deno;

t.simplify();

return t;
}

CFraction operator* (int i, const CFraction &c)
{
CFraction t;

t.nume = c.nume * i;

t.deno = c.deno;

t.simplify();

return t;
}

CFraction operator/(int i, const CFraction &c)
{
CFraction t;

t.deno *= i;

t.nume = c.nume;

t.simplify();

return t;
}

int main()
{
CFraction x(1,3),y(-5,10),s;

x.display();
y.display();
if (x>y) cout<<"x大于y"<<endl;
if (x<y) cout<<"x小于y"<<endl;
if (x==y) cout<<"x等于y"<<endl;

cout<<endl;

cout << "分数与整数相加的实现:" << endl;
cout << "x + 5 = ";
s = x + 5;
s.display();
cout << "5 + x = " ;
s = 5 + x;
s.display();
cout << "x - 5 = " ;
s = x - 5;
s.display();
cout << "5 - x = " ;
s = 5 - x;
s.display();
cout << "x / 5 = " ;
s = x / 5;
s.display();
cout << "5 / x = " ;
s = 5 / x;
s.display();
cout << "x * 5 = " ;
s = x * 5;
s.display();
cout << "5 * x = " ;
s = 5 * x;
s.display();

system("pause");
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 c system
相关文章推荐