第9周-任务3-分数类中运算符重载

简介: 【题目】接第8周任务3,定义分数类中<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。 【参考解答】 #include <iostream>using namespace std;class CFraction{private: int nume; // 分子 int deno; /

【题目】接第8周任务3,定义分数类中<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。


【参考解答】

#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();

	//输入输出的重载
	friend istream &operator>>(istream &in,CFraction &x);
	friend ostream &operator<<(ostream &out,CFraction x);
	
	CFraction operator+(const CFraction &c);  //两个分数相加,结果要化简
	CFraction operator-(const CFraction &c);  //两个分数相减,结果要化简
	CFraction operator*(const CFraction &c);  //两个分数相乘,结果要化简
	CFraction operator/(const CFraction &c);  //两个分数相除,结果要化简
	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的最大公约数
	{
		m=n;
		n=r;
	}
	deno/=n;     // 化简
	nume/=n;
	if (deno<0)  // 将分母转化为正数
	{
		deno=-deno;
		nume=-nume;
	}
}

// 重载输入运算符>>
istream &operator>>(istream &in,CFraction &x)
{
	char ch;
	while(1)
	{
		cin>>x.nume>>ch>>x.deno;
		if (x.deno==0) 
			cerr<<"分母为! 请重新输入\n";
		else if(ch!='/')
			cerr<<"格式错误(形如m/n)! 请重新输入\n";
		else
			break;
	}
	return cin;
}

// 重载输出运算符<<
ostream &operator<<(ostream &out,CFraction x)
{
	cout<<x.nume<<'/'<<x.deno;
	return cout;
}

// 分数相加
CFraction CFraction::operator+(const CFraction &c)
{
	CFraction t;
	t.nume=nume*c.deno+c.nume*deno;
	t.deno=deno*c.deno;
	t.simplify();
	return t;
}

// 分数相减
CFraction CFraction:: operator-(const CFraction &c)
{
	CFraction t;
	t.nume=nume*c.deno-c.nume*deno;
	t.deno=deno*c.deno;
	t.simplify();
	return t;
}

// 分数相乘
CFraction CFraction:: operator*(const CFraction &c)
{
	CFraction t;
	t.nume=nume*c.nume;
	t.deno=deno*c.deno;
	t.simplify();
	return t;
}

// 分数相除
CFraction CFraction:: operator/(const CFraction &c)
{
	CFraction t;
	if (!c.nume) return *this;   //除法无效(除数为)时,这种情况需要考虑,但这种处理仍不算合理
	t.nume=nume*c.deno;
	t.deno=deno*c.nume;
	t.simplify();
	return t;
}

// 分数取正号
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 this_nume,c_nume,common_deno;
	this_nume=nume*c.deno;        // 计算分数通分后的分子,同分母为deno*c.deno
	c_nume=c.nume*deno; 
	common_deno=deno*c.deno;
	if ((this_nume-c_nume)*common_deno>0) return true;
	return false;
}

// 分数比较大小
bool CFraction::operator<(const CFraction &c)
{
	int this_nume,c_nume,common_deno;
	this_nume=nume*c.deno;      
	c_nume=c.nume*deno;
	common_deno=deno*c.deno;
	if ((this_nume-c_nume)*common_deno<0) return true; 
	return false;
}

// 分数比较大小
bool CFraction::operator==(const CFraction &c)
{
	if (*this!=c) return false;
	return true;
}

// 分数比较大小
bool CFraction::operator!=(const CFraction &c)
{
	if (*this>c || *this<c) return true;
	return false;
}

// 分数比较大小
bool CFraction::operator>=(const CFraction &c)
{
	if (*this<c) return false;
	return true;
}

// 分数比较大小
bool CFraction::operator<=(const CFraction &c)
{
	if (*this>c) return false;
	return true;
}

int main()
{
	CFraction x,y,s;
	cout<<"输入x: ";
	cin>>x;
	cout<<"输入y: ";
	cin>>y;
	s=+x+y;
	cout<<"+x+y="<<s<<endl;
	cout<<"x-y="<<s<<endl;
	s=x*y;
	cout<<"x*y="<<s<<endl;
	s=x/y;
	cout<<"x/y="<<s<<endl;
	s=-x+y;
	cout<<"-x+y="<<s<<endl;
	
	cout<<x;
	if (x>y) cout<<"大于";
	if (x<y) cout<<"小于";
	if (x==y) cout<<"等于";
	cout<<y<<endl;
	system("pause");
	return 0;
}


目录
相关文章
|
C++ 机器学习/深度学习 移动开发
C++实践参考——分数类中的运算符重载
【项目1-分数类中的运算符重载】   (1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。 class CFraction { private: int nume; // 分子 int deno; // 分母 public: //构造函数及运算符重载的函数声明 }; //重载函数的实现及
1549 0
|
C++ 机器学习/深度学习
C++第8周(春)项目3 分数类中的运算符重载
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 【项目3-分数类中的运算符重载】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。可以以第5周项目2的代码为基础开始工作。 class CFraction { private: i
725 0
|
C++ 机器学习/深度学习 移动开发
C++第9周项目3 - 实现分数类中的运算符重载
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8841620 【项目3-分数类】接第8周项目3,定义分数类中&lt;&lt;和&gt;&gt;运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式
1036 0
|
C++ 机器学习/深度学习
C++第8周项目3 - 分数类中的运算符重载
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8806111 【项目3-分数类中的运算符重载】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。可以从
1101 0
|
C++
C++第9周项目4 - 一元一次方程类
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8841620 【项目4-一元一次方程类】设计一元一次方程类,求形如ax+b=0的方程的解。例如:输入3x-8=0时,输出的方程的解为x=2.66667;再如
1120 0
|
C++
C++第9周项目2 - 实现时间类中的运算符重载
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8841620 【项目2-Time类】接第8周项目2,定义Time类中的&lt;&lt;和&gt;&gt;运算符重载,实现时间的输入输出,改造原程序中对运算结
1064 0
|
C++
C++第8周项目2 -Time类中的运算符重载
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8806111 【项目2-Time类中的运算符重载】实现Time类中的运算符重载 #include &lt;iostream&gt; using namespa
918 0
|
测试技术 C++
C++程序设计-第8周 运算符的重载
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565 本周目标是:(1)学会定义类中双目和单目运算符的重载函数;(2)学会使用类的成员函数和友元函数实现运算符的重载 【项目1-实现复数类中的运算符重载】定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。(1)任务一:请用类的成员函数完成运算符的重载; cla
1193 0
|
C++
C++第7周项目2 - 成员函数、友元函数和一般函数之区别
课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8775137 【项目2-成员函数、友元函数和一般函数之区别】阅读程序,仔细阅读注释。然后模仿完成求点类中距离的任务。  你需要完成的任务是,利用成员函数、友
1360 0
|
人工智能 C++
第14周-任务1-数组类的构造
【关于题目】数组是几乎所支持的组织数据的方法。C和C++对数组类型提供了内置支持,使我们利用数组实现软件中需要的各种实用的功能。但是,这种支持仅限于用来读写单个元素的机制。C++不支持数组的抽象abstraction,也不支持对整个数组的操作。例如:把一个数组赋值给另外一个数组,对两个数组进行相等比较或者想知道数组的大小size,等等。对C++而言,数组是从C语言中继承来的,它反映了数据与
958 0