[实验任务四]:****电子钟

设计一款电子钟类,用于显示时、分、秒

实验要求:

\1. 含有形参有默认值的默认构造函数;

\2. 重载 前缀++ 和 后缀—用于调整时间,每次调整均对秒进行调整,若秒满60,则分加1,若分满60则时加1,时满24,则清零重新开始;

\3. 重载插入运算符 >> 用于输入(设定)时间;

\4. 重载插入运算符 << 用于输出时间。

#include<iostream>
using namespace std;
class Clock
{
	friend ostream& operator<<(ostream& out, Clock &c);
	friend istream& operator>>(istream& in, Clock& c);
public:
	Clock(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
	Clock& operator++();
	Clock& operator--(int);
	
private:
	int hour, minute, second;
};
Clock& Clock::operator++()
{
	second++;
	return *this;
}
Clock& Clock::operator--(int)
{
	Clock temp = (*this);
	second--;
	return temp;
}
ostream& operator<<(ostream& out, Clock &c)
{
	if (c.second >= 60)
	{
		++c.minute;
		c.second -= 60;
	}
	if (c.minute >= 60)
	{
		++c.hour;
		c.minute -= 60;
	}
	if (c.hour == 24)
	{
		c.hour = 0;
	}
	if (c.second < 0)
	{
		--c.minute;
		c.second += 60;
	}
	if (c.minute < 0)
	{
		--c.hour;
		c.minute += 60;
	}
	if (c.hour < 0)
	{
		c.hour += 24;
	}

	out << c.hour << " 时 " << c.minute << " 分 " << c.second << " 秒 " << endl;
	return out;
}
istream& operator>>(istream& in, Clock& c)
{
	cout << "请输入时间:";
	in >> c.hour >> c.minute >> c.second;
	return in;
}
int main()
{
	Clock c1;
	cin >> c1;
	cout << c1;
	++c1;
	cout << c1;
	c1--;
	cout << c1;
}

[实验任务五]:****分数类

定义一个分数类,包含分子、分母

实验要求:

\1. 含有无参的默认构造函数,并进行构造函数的重载;

\2. 重载分数的加法+、减法-、数乘*这三运算符;

\3. 重载分数的输入和输出运算符;

\4. 重载分数的关系运算符==,!=,>=,<=;

\5. 定义约简函数,使分子分母没有公因子。

#include<iostream>
using namespace std;
int big(int a, int b)
{
	int t = (a > b) ? a : b;
	while (1)
	{
		
		if ((t % a == 0) && (t % b == 0))
		{
			return t;
		}
		t++;
	}
}
class Fen
{
	friend Fen& operator+(Fen& f1, Fen& f2);
	friend Fen& operator-(Fen& f1, Fen& f2);
	friend Fen& operator*(Fen& f1, Fen& f2);
	friend ostream& operator<<(ostream& out, Fen& f);
	friend istream& operator>>(istream& in, Fen& f);
public:
	Fen(){}
	Fen(int m,int z):mu(m),zi(z){}
	Fen& operator*(Fen& f)
	{
		zi *= f.zi;
		mu *= f.mu;
		
		return (*this);
	}
	bool operator==(Fen& f)
	{
		if (mu == f.mu && zi == f.zi)
		{
			
			return true;
		}
		else
		{
			
			return false;
		}
	}
	bool operator!=(Fen& f)
	{
		if (mu != f.mu && zi != f.zi)
		{
			
			return true;
		}
		else
		{
			
			return false;
		}
	}
	bool operator>=(Fen& f)
	{
		int temp = big(mu, f.mu);
		if ((temp / mu) * zi >= (temp / f.mu) * f.zi)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool operator<=(Fen& f)
	{
		int temp = big(mu, f.mu);
		if ((temp / mu) * zi <= (temp / f.mu) * f.zi)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
private:
	int mu, zi;
};
Fen& operator+(Fen& f1,Fen &f2)
{
	Fen temp;
	if (f1.mu == f2.mu)
	{
		temp.zi = f1.zi + f2.zi;
		temp.mu = f1.mu;
	}
	else
	{
		int temp1 = big(f1.mu, f2.mu);
		temp.zi = (temp1 / f1.mu) * f1.zi + (temp1 / f2.mu) * f2.zi;
		temp.mu = temp1;
	}
	return temp;
}
Fen& operator-(Fen& f1, Fen& f2)
{
	Fen temp;
	if (f1.mu == f2.mu)
	{
		temp.zi = f1.zi - f2.zi;
		temp.mu = f1.mu;
	}
	else
	{
		int temp1 = big(f1.mu, f2.mu);
		temp.zi = (temp1 / f1.mu) * f1.zi - (temp1 / f2.mu) * f2.zi;
		temp.mu = temp1;
	}

	return temp;
}
Fen& operator*(Fen& f1, Fen& f2)
{
	Fen temp;
	temp.mu = f1.mu * f2.mu;
	temp.zi = f1.zi * f2.zi;
	return temp;
}



ostream& operator<<(ostream& out, Fen& f)
{
	out << f.zi << "/" << f.mu << "  ";
	return out;
}
istream& operator>>(istream& in, Fen& f)
{
	cout << "请依次输入分子分母: ";
	in >> f.zi >> f.mu;
	return in;
}
int main()
{
	Fen f1, f2;
	cin >> f1 >> f2;
	if (f1 >= f2)
	{
		cout << "大于" << endl;
	}
	if (f1 <= f2)
	{
		cout << "小于" << endl;
	}
	if (f1 == f2)
	{
		cout << "相等" << endl;
	}
	if (f1 != f2)
	{
		cout << "不相等" << endl;
	}
	Fen f3 = f1 + f2;
	Fen f4 = f1 - f2;
	cout << f1;
	cout << f2;
	cout << f3;
	cout << f4;
}
posted on 2023-04-13 16:19  许七安gyg  阅读(16)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });