首页 > 编程语言 >c++指导书

c++指导书

时间:2023-04-13 16:23:15浏览次数:38  
标签:return Clock Fen c++ mu int operator 指导书

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

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

实验要求:

\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;
}

标签:return,Clock,Fen,c++,mu,int,operator,指导书
From: https://www.cnblogs.com/gyg1222/p/17315246.html

相关文章

  • C++从0到1 —— 跟着我一定能学会
    1.阅读须知以下所有的内容都属于我的个人总结与思考,但同时又想作为一种资源供大家学习、参考。文章的水平和质量可能不是很高,也有可能内容有些小错误,希望各位读者能够私信我指出错误。C++的部分分为三个版块:C++基础知识部分、STL部分、C++11特性以及其他。2.C++基础知识 ......
  • Understanding the different flavors of Clang C and C++ compilers in Windows
    https://blog.conan.io/2022/10/13/Different-flavors-Clang-compiler-Windows.htmlThisarticlewillexplainthedifferentflavorsofClangCandC++compileryoumightencounterinWindows,andgiveyousomesuggestionsaboutwhichonesmightberightforyo......
  • Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Bui
    https://zhuanlan.zhihu.com/p/471661231下载软件链接:https://pan.baidu.com/s/12hhCEKnjr2Qq-H3sHuQiXQ?pwd=6g0v提取码:6g0v安装退出虚拟环境,重新进入pip安装对应包......
  • C++性能优化——能用array就不要用unordered_map作为查询表
    unordered_map需要哈希值计算和表查询的开销,当key值为整数且连续,直接用数组作为查询表具有更高的效率。#include<iostream>#include<chrono>#include<unordered_map>usingnamespacestd;longlongcount=0;constexprintN=10;voidtimeMeasure(void(*f)()){a......
  • 《c++徒步》MFC篇——消息映射机制
    MFC消息映射机制什么是消息映射机制?MFC使用消息映射机制来处理消息,引入了消息映射表的概念,表中存消息和消息处理函数及二者对应关系。当鼠标点击事件发生时,会产生对应消息,然后去消息映射表中查找对应的消息处理函数并执行。什么是句柄?句柄相当于一个编号,Windows对于我们来说相......
  • 第十四届蓝桥杯省赛c/c++大学B组 试题A:日期统计(无深搜暴力求解)
    试题A:日期统计本题总分:5分【问题描述】小蓝现在有一个长度为100的数组,数组中的每个元素的值都在0到9的范围之内。数组中的元素从左至右如下所示:56869161249198236477595038758158618303792705885709919446......
  • C++文件处理
    ......
  • c++ new和malloc
    1、new/delete是C++关键字,需要编译器支持。malloc/free是库函数,需要头文件支持;2、使用new操作符申请内存分配时无须指定内存块的大小,编译器会根据类型信息自行计算。而malloc则需要显式地指出所需内存的尺寸。3、new操作符内存分配成功时,返回的是对象类型的指针,类型严格与对......
  • c++指针参数传递和引用参数传递的区别
    1) 指针参数传递本质上是值传递,它所传递的是一个地址值。值传递过程中,被调函数的形式参数作为被调函数的局部变量处理,会在栈中开辟内存空间以存放由主调函数传递进来的实参值,从而形成了实参的一个副本(替身)。值传递的特点是,被调函数对形式参数的任何操作都是作为局部变量进行的,......
  • MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe".
    完整报错信息:MSBUILD:errorMSB3428:CouldnotloadtheVisualC++component"VCBuild.exe".Tofixthis,1)installthe.NETFramework2.0SDK,2)installMicrosoftVisualStudio2005or3)addthelocationofthecomponenttothesystempathifit......