/*
*Copyright (c)2013,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:孙玲倩
*完成日期:2014年4月22日
*版本号:v1.0
*问题描述:
问题分析:
*/
#include <iostream>
#include <cstdlib>
using namespace std;
class CFraction
{
private:
double nume; // 分子
double deno; // 分母
public:
//构造函数及运算符重载的函数声明
CFraction(double nu=1,double de=1); //构造函数,初始化用
void set(double nu,double de); //置值,改变值时用
void simplify(); //化简(使分子分母没有公因子)
void display();
//二目的比较运算符重载
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 &t);
CFraction operator-(CFraction &t);//对照+理解
CFraction operator*(CFraction &t);
CFraction operator/(CFraction &t);//对照+理解
//一目运算符
CFraction operator+();
CFraction operator-();
CFraction operator~();
friend ostream&operator<<(ostream&,CFraction&);
friend istream&operator>>(istream&,CFraction&);
};
int gcd(int x,int y);
CFraction::CFraction(double nu,double de) //构造函数,初始化用
{
if(de!=0)
{
nume=nu;
deno=de;
}
else
{
cout<<"初始化有误,退出程序"<<endl;
exit(0);
}
}
void CFraction::set(double nu,double de) //置值,改变值时用
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::display()
{
cout<<"="<<nume<<"/"<<deno<<endl;
}
bool CFraction::operator > (CFraction &t)
{
double m1=nume/deno;
double m2=t.nume/t.deno;
if (m1>m2) return true;
return false;
}
bool CFraction::operator < (CFraction &t)
{
double m1=nume/deno;
double m2=t.nume/t.deno;
if (m1<m2) return true;
return false;
}
bool CFraction::operator == (CFraction &t)
{
if (*this <t||*this>t) return false;
return true;
}
bool CFraction::operator != (CFraction &t)
{
if (*this == t) return false;//可以直接使用已经重载了的运算实现新运算
return true;
}
bool CFraction::operator >= (CFraction &t)
{
if (*this < t) return false;
return true;
}
bool CFraction::operator <= (CFraction &t)
{
if (*this > t) return false;
return true;
}
CFraction CFraction::operator+(CFraction &c)
{
CFraction t;
t.nume=nume*c.deno+c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator-(CFraction &c)//对照+理解
{
CFraction t;
t.nume=nume*c.deno-c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator*(CFraction &c)
{
CFraction t;
t.nume=nume*c.nume;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator/(CFraction &c)//对照+理解
{
CFraction t;
if (!c.nume) return *this; //除法无效
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.simplify();
return t;
}
void CFraction::simplify() //化简(使分子分母没有公因子)
{
int m=gcd(nume,deno);
nume=nume/m;
deno/=m;
}
int gcd(int x,int y)//求两数最大公约数
{
int r;
while(y>0)
{
r=x%y;
x=y;
y=r;
}
return x;
}
CFraction CFraction::operator+()
{
CFraction t;
t.nume=nume;
t.deno=deno;
t.simplify();
return t;
}
CFraction CFraction::operator-()
{
CFraction t;
t.nume=-nume;
t.deno=deno;
t.simplify();
return t;
}
CFraction CFraction::operator~()
{
CFraction t;
t.nume=deno;
t.deno=nume;
t.simplify();
return t;
}
ostream & operator<<(ostream &output,CFraction &c)
{
output<<"="<<c.nume<<"/"<<c.deno<<endl;
return output;
}
istream & operator>>(istream &input,CFraction &c)
{
cout<<"input nume and deno";
input>>c.nume>>c.deno;
return input;
}
int main()
{
CFraction t1,t2,t;
cin>>t1>>t2;
cout<<"t1="<<t1;
cout<<"t2="<<t2;
cout<<"比较分数的大小:\n";
if (t1>t2) cout<<"t1>t2"<<endl;
if (t1<t2) cout<<"t1<t2"<<endl;
if (t1==t2) cout<<"t1=t2"<<endl;
if (t1!=t2) cout<<"t1≠t2"<<endl;
if (t1>=t2) cout<<"t1≥t2"<<endl;
if (t1<=t2) cout<<"t1≤t2"<<endl;
cout<<"分数取相反数:"<<endl;
t=-t1;
cout<<"-t1"<<t;
t=-t2;
cout<<"-t2"<<t;
cout<<"分数取倒数:"<<endl;
t=~t1;
cout<<"~t1"<<t;
t=~t2;
cout<<"~t2"<<t;
cout<<"t1与t2的计算 \n";
t=t1+t2;
cout<<"t1+t2="<<t;
t=t1-t2;
cout<<"t=t1-t2="<<t;
t=t1*t2;
cout<<"t=t1*t2="<<t;
t=t1/t2;
cout<<"t=t1/t2="<<t;
return 0;
}
运行结果:
第九周项目三
最新推荐文章于 2023-04-20 10:22:56 发布