/*
* Copyright (c) 2011, 烟台大学计算机学院
* All rights reserved.
* 作 者:刘明亮
* 完成日期:2013 年 4 月 24 日
* 版 本 号:v1.0
* 输入描述:
* 问题描述:分数类中的对象可以和整型数进行四则运算,且运算符合交换律。
* 例如:CFraction a(1,3),b;int i=2; 可以完成b=a+i;。
* 同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
* 程序输出:
* 问题分析:
* 算法设计:略
*/
#include<iostream>
#include<cmath>
using namespace std;
class CFraction
{private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //置值,改变值时用
CFraction simplify(); //化简(使分子分母没有子)
//比较运算符(二目)的重载
bool operator > (CFraction &t);
bool operator < (CFraction &t);
bool operator == (CFraction &t);
//二目运算符的重载
CFraction operator+(CFraction &n);
CFraction operator-(CFraction &n);
CFraction operator*(CFraction &n);
CFraction operator/(CFraction &n);
CFraction operator+(int i);
CFraction operator-(int i);
CFraction operator*(int i);
CFraction operator/(int i);
void display();
};
CFraction::CFraction(int nu,int de) //置值
{
nume=nu;
deno=de;
}
CFraction CFraction::simplify() //化简(使分子分母没有公因子)
{
CFraction n;
n.nume=nume;n.deno=deno;
int i;
if(nume%deno==0){
n.deno=1;
n.nume=nume/deno;
}else{
if(nume>deno){
for(i=2;i<=n.deno;i++){
if(nume%i==0&&deno%i==0){
n.nume=n.nume/i;
n.deno=n.deno/i;
}
}
}else{
for(i=2;i<=n.nume;i++){
if(n.nume%i==0&&n.deno%i==0){
n.nume=n.nume/i;
n.deno=n.deno/i;
}
}
}
}
return n;
}
bool CFraction::operator > (CFraction &t)
{
if(nume>t.nume||(nume==t.nume&&deno>t.deno)){
return true;
}else{
return false;
}
}
bool CFraction::operator < (CFraction &t)
{
if(nume<t.nume||(nume==t.nume&&deno<t.deno)){
return true;
}else{
return false;
}
}
bool CFraction::operator == (CFraction &t)
{
if(nume==t.nume&&deno==t.deno){
return true;
}else{
return false;
}
}
//二目运算符的重载
CFraction CFraction::operator+(CFraction &n)
{
CFraction t;
t.deno=deno*n.deno;
t.nume=(n.nume)*deno+nume*(n.deno);
return t.simplify();
}
CFraction CFraction::operator-(CFraction &n)
{
CFraction t;
t.deno=deno*n.deno;
t.nume=nume*n.deno-n.nume*deno;
return t.simplify();
}
CFraction CFraction::operator*(CFraction &n)
{
CFraction t;
t.deno=deno*n.deno;
t.nume=nume*n.nume;
return t.simplify();
}
CFraction CFraction::operator/(CFraction &n)
{
CFraction t;
t.deno=deno*n.nume;
t.nume=nume*n.deno;
return t.simplify();
}
CFraction CFraction::operator+(int i)
{
CFraction t;
t.deno=deno;
t.nume=nume+i*deno;
return t.simplify();
}
CFraction CFraction::operator-(int i)
{
CFraction t;
t.deno=deno;
t.nume=nume-i*deno;
return t.simplify();
}
CFraction CFraction::operator*(int i)
{
CFraction t;
t.deno=deno;
t.nume=nume*i;
return t.simplify();
}
CFraction CFraction::operator/(int i)
{
CFraction t;
t.deno=deno*i;
t.nume=nume;
return t.simplify();
}
void CFraction::display()
{
if(deno==1){
cout<<nume<<endl;
}else{
cout<<nume<<"/"<<deno<<endl;
}
}
int main()
{
CFraction n1(3,2),n2(1,2),n;//构造函数,初始化用
cout<<"n1为:";
n1.display();
cout<<"n2为:";
n2.display();
cout<<"下面比较两个时间大小:\n";
if (n1>n2) cout<<"n1>n2"<<endl;
if (n1<n2) cout<<"n1<n2"<<endl;
if (n1==n2) cout<<"n1=n2"<<endl;
cout<<endl;
//下面自行设计对其他运算符的重载的测试
cout<<"n1+n2=";
n=n1+n2;
n.display();
cout<<"n1-n2=";
n=n1-n2;
n.display();
cout<<"n1*n2=";
n=n1*n2;
n.display();
cout<<"n1/n2=";
n=n1/n2;
n.display();
cout<<"n1+3=";
n=n1.operator+(3);
n.display();
cout<<"n1-3=";
n=n1.operator-(3);
n.display();
cout<<"n1*3=";
n=n1.operator*(3);
n.display();
cout<<"n1/3=";
n=n1.operator/(3);
n.display();
return 0;
}
“+ - * /”重载
最新推荐文章于 2022-08-06 10:13:56 发布