定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
2016-05-28 14:56
639 查看
问题及代码:
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作 者:李磊涛
*完成时间:2016年5月28日
*版 本 号:v1.0
*
*问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
*输入描述:无。
*程序输出:分数的各种形式。
*/
#include<iostream>
using namespace std;
class CFraction
{
private:
int nume;//fenzi
int deno;//fenmu
public:
CFraction(int a=0,int b=0);
void show();
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(double c);
CFraction operator-(double c);
CFraction operator*(double c);
CFraction operator/(double c);
bool operator>(CFraction &c);
bool operator<(CFraction &c);
bool operator==(CFraction &c);
bool operator>=(CFraction &c);
bool operator<=(CFraction &c);
bool operator!=(CFraction &c);
CFraction operator+();
CFraction operator-();
CFraction operator~();
};
istream &operator>>(istream &in,CFraction &x)
{
char c;
for(;;)
{
cin>>x.nume>>c>>x.deno;
if (x.deno==0)
cerr<<"分母为0, 请重新输入\n";
else if(c!='/')
cerr<<"格式错误(形如m/n)! 请重新输入\n";
else
break;
}
return cin;
}
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
}
CFraction CFraction::operator~()
{
int a=nume,b=deno,t;
t=a;
a=b;
b=t;
CFraction c(a,b);
return c;
}
CFraction CFraction::operator+()
{
return *this;
}
CFraction CFraction::operator-()
{
int mu,zi;
mu=deno;
zi=-nume;
CFraction t(zi,mu);
return t;
}
bool CFraction::operator>(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1>zi2)
return true;
else
return false;
}
bool CFraction::operator<(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1< zi2)
return true;
else
return false;
}
bool CFraction::operator==(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1==zi2)
return true;
else
return false;
}
bool CFraction::operator<=(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1<=zi2)
return true;
else
return false;
}
bool CFraction::operator>=(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1>=zi2)
return true;
else
return false;
}
bool CFraction::operator!=(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1!=zi2)
return true;
else
return false;
}
CFraction::CFraction(int a,int b)
{
nume=a;
deno=b;
}
CFraction CFraction::operator+(double c)
{
int zi;
zi=nume+c*deno;
CFraction t(zi,deno);
return t;
}
CFraction CFraction::operator-(double c)
{
int zi;
zi=nume-c*deno;
CFraction t(zi,deno);
return t;
}
CFraction CFraction::operator*(double c)
{
int zi;
zi=nume*c;
CFraction t(zi,deno);
return t;
}
CFraction CFraction::operator/(double c)
{
int mu;
mu=deno*c;
CFraction t(nume,mu);
return t;
}
void CFraction::show()
{int t,m,r,n;
m=deno;
n=nume;
if(deno<nume)
{
t=m;
m=n;
n=t;
}
while(r=m%n)
{
m=n;
n=r;
}
deno=deno/n;
nume=nume/n;
if(deno==1)
cout<<nume<<endl;
else
cout<<nume<<"/"<<deno<<endl;
}
int main()
{
CFraction c1(1,2),c2(2,3),c3;
c3=c1+2;
c3.show();
c3=c1-2;
c3.show();
c3=c1*2;
c3.show();
c3=c1/2;
c3.show();
if(c1>c2)
cout<<"c1>c2"<<endl;
if(c1<c2)
cout<<"c1<c2"<<endl;
if(c1==c2)
cout<<"c1==c2"<<endl;
if(c1>=c2)
cout<<"c1>=c2"<<endl;
if(c1<=c2)
cout<<"c1<=c2"<<endl;
if(c1!=c2)
cout<<"c1!=c2"<<endl;
cout<<"-c1="<<-c1<<endl;
cout<<"+c1="<<+c1<<endl;
cout<<"c1的倒数: "<<~c1<<endl;
return 0;
}
运行结果:
知识点总结:
通过该程序,强化了我对<<和>>运算符重载认识。
学习心得:
<<和>>运算符重载用起来确实方便不少虽然还不是很熟悉。
/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作 者:李磊涛
*完成时间:2016年5月28日
*版 本 号:v1.0
*
*问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
*输入描述:无。
*程序输出:分数的各种形式。
*/
#include<iostream>
using namespace std;
class CFraction
{
private:
int nume;//fenzi
int deno;//fenmu
public:
CFraction(int a=0,int b=0);
void show();
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(double c);
CFraction operator-(double c);
CFraction operator*(double c);
CFraction operator/(double c);
bool operator>(CFraction &c);
bool operator<(CFraction &c);
bool operator==(CFraction &c);
bool operator>=(CFraction &c);
bool operator<=(CFraction &c);
bool operator!=(CFraction &c);
CFraction operator+();
CFraction operator-();
CFraction operator~();
};
istream &operator>>(istream &in,CFraction &x)
{
char c;
for(;;)
{
cin>>x.nume>>c>>x.deno;
if (x.deno==0)
cerr<<"分母为0, 请重新输入\n";
else if(c!='/')
cerr<<"格式错误(形如m/n)! 请重新输入\n";
else
break;
}
return cin;
}
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
}
CFraction CFraction::operator~()
{
int a=nume,b=deno,t;
t=a;
a=b;
b=t;
CFraction c(a,b);
return c;
}
CFraction CFraction::operator+()
{
return *this;
}
CFraction CFraction::operator-()
{
int mu,zi;
mu=deno;
zi=-nume;
CFraction t(zi,mu);
return t;
}
bool CFraction::operator>(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1>zi2)
return true;
else
return false;
}
bool CFraction::operator<(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1< zi2)
return true;
else
return false;
}
bool CFraction::operator==(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1==zi2)
return true;
else
return false;
}
bool CFraction::operator<=(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1<=zi2)
return true;
else
return false;
}
bool CFraction::operator>=(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1>=zi2)
return true;
else
return false;
}
bool CFraction::operator!=(CFraction &c)
{
int mu,zi1,zi2;
mu=deno*c.deno;
zi1=nume*c.deno;
zi2=c.nume*deno;
if(zi1!=zi2)
return true;
else
return false;
}
CFraction::CFraction(int a,int b)
{
nume=a;
deno=b;
}
CFraction CFraction::operator+(double c)
{
int zi;
zi=nume+c*deno;
CFraction t(zi,deno);
return t;
}
CFraction CFraction::operator-(double c)
{
int zi;
zi=nume-c*deno;
CFraction t(zi,deno);
return t;
}
CFraction CFraction::operator*(double c)
{
int zi;
zi=nume*c;
CFraction t(zi,deno);
return t;
}
CFraction CFraction::operator/(double c)
{
int mu;
mu=deno*c;
CFraction t(nume,mu);
return t;
}
void CFraction::show()
{int t,m,r,n;
m=deno;
n=nume;
if(deno<nume)
{
t=m;
m=n;
n=t;
}
while(r=m%n)
{
m=n;
n=r;
}
deno=deno/n;
nume=nume/n;
if(deno==1)
cout<<nume<<endl;
else
cout<<nume<<"/"<<deno<<endl;
}
int main()
{
CFraction c1(1,2),c2(2,3),c3;
c3=c1+2;
c3.show();
c3=c1-2;
c3.show();
c3=c1*2;
c3.show();
c3=c1/2;
c3.show();
if(c1>c2)
cout<<"c1>c2"<<endl;
if(c1<c2)
cout<<"c1<c2"<<endl;
if(c1==c2)
cout<<"c1==c2"<<endl;
if(c1>=c2)
cout<<"c1>=c2"<<endl;
if(c1<=c2)
cout<<"c1<=c2"<<endl;
if(c1!=c2)
cout<<"c1!=c2"<<endl;
cout<<"-c1="<<-c1<<endl;
cout<<"+c1="<<+c1<<endl;
cout<<"c1的倒数: "<<~c1<<endl;
return 0;
}
运行结果:
知识点总结:
通过该程序,强化了我对<<和>>运算符重载认识。
学习心得:
<<和>>运算符重载用起来确实方便不少虽然还不是很熟悉。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- 使用 Libki 来管理公共用户访问计算机
- c++ primer 第五版 笔记前言
- 微型计算机的始祖:Altair 8800
- share_ptr的几个注意点
- 通过手机、电脑远程开关机,Windows和linux机手机,电脑相互控制
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例