深圳大学计软《面向对象的程序设计》实验8 静态与友元

A. 旅馆旅客管理(静态成员)

题目描述

编写程序,实现某旅馆的客人住宿记录功能。

定义一个Customer类,要求输入客人的姓名,创建一个Customer对象。类声明如下:
在这里插入图片描述

调用类的Display函数输出客人ID(输出顺序号占4位,如第1位为0001,第2位为0002,依此类推)、姓名、总人数。总人数和客人租金用静态成员,其他属性采用普通的数据成员。

输入

输入测试次数t

对于每次测试,首先输入当前年份,

接下来依次输入顾客姓名,0表示输入结束。

输出

每行依次输出顾客信息和旅馆信息。包括顾客姓名,顾客编号,旅馆入住总人数,旅馆当前总收入。

输入样例1

2
2014
张三 李四 王五 0
2015
Cindy David 0

输出样例1

张三 20140001 1 150
李四 20140002 2 300
王五 20140003 3 450
Cindy 20150004 4 600
David 20150005 5 750

AC代码

#include<bits/stdc++.h>
using namespace std;

class Customer {
	static int TotalCustNum;
	static int Rent;
	static int Year;
	int CustID;
	string name;
public:
	Customer(string name) :name(name) {
		Rent += 150;
		TotalCustNum++;
		CustID = TotalCustNum;
	}

	~Customer() { name.clear(); }

	static void changeYear(int r) {
		Year = r;
	}

	void Display() {
		cout << name << " " << Year;
		cout << setfill('0') << setw(4) << CustID << " " << CustID << " " << Rent << endl;
	}

	static void init() {
		TotalCustNum = 0;
		Rent = 0;
	}
};

int Customer::TotalCustNum = 0;
int Customer::Rent = 0;
int Customer::Year = 0;


int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		//Customer::init();
		int year;
		cin >> year;
		string name;
		cin >> name;
		int j = 1;
		while (name != "0") {
			Customer test(name);
			Customer::changeYear(year);
			test.Display();
			cin >> name;
		}
	}
	return 0;
}

B. 距离计算(友元函数)

题目描述

Point类的基本形式如下:

在这里插入图片描述

请完成如下要求:

  1. 实现Point类;
  2. 为Point类增加一个友元函数double Distance(Point &a, Point &b),用于计算两点之间的距离。直接访问Point对象的私有数据进行计算。
  3. 编写main函数,输入两点坐标值,计算两点之间的距离。

输入

第1行:输入需计算距离的点对的数目

第2行开始,每行依次输入两个点的x和y坐标

输出

每行依次输出一组点对之间的距离(结果直接取整数部分,不四舍五入)

输入样例1

2
1 0 2 1
2 3 2 4

输出样例1

1
1

AC代码

#include<bits/stdc++.h>
using namespace std;

class Point {
	double x, y;
public:
	Point(double xx, double yy) {
		x = xx;
		y = yy;
	}

	friend double Distance(Point& a, Point& b) {
		return sqrt(pow(b.x - a.x, 2) + pow(a.y - b.y, 2));
	}
};

int main() {
	int t;
	cin >> t;
	while (t--){
		double a, b, c, d;
		cin >> a >> b >> c >> d;
		Point p1(a, b), p2(c, d);
		cout << int(Distance(p1, p2)) << endl;
	}
	return 0;
}

C. 日期时间合并输出(友元函数)

题目描述

已知日期类Date,有属性:年、月、日,其他成员函数根据需要自行编写,注意该类没有输出的成员函数

已知时间类Time,有属性:时、分、秒,其他成员函数根据需要自行编写,注意该类没有输出的成员函数

现在编写一个全局函数把时间和日期的对象合并起来一起输出,

函数原型为:void Display(Date &, Time &)

函数输出要求为:

1、时分秒输出长度固定2位,不足2位补0

2、年份输出长度固定为4位,月和日的输出长度固定2位,不足2位补0

例如2017年3月3日19时5分18秒

则输出为:2017-03-03 19:05:18

程序要求

1、把函数Display作为时间类、日期类的友元

2、分别创建一个日期对象和时间对象,保存日期的输入和时间的输入

3、调用Display函数实现日期和时间的合并输出

输入

第一行输入t表示有t组示例

接着一行输入三个整数,表示年月日

再接着一行输入三个整数,表示时分秒

依次输入t组示例

输出

每行输出一个日期和时间合并输出结果

输出t行

输入样例1

2
2017 3 3
19 5 18
1988 12 8
5 16 4

输出样例1

2017-03-03 19:05:18
1988-12-08 05:16:04

AC代码

#include<bits/stdc++.h>
using namespace std;

class Time;
class Date {
	int year, month, day;
	string name[12] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
public:
	Date() { cin >> year >> month >> day; }
	friend void Display(Date& date, Time& time);
};

class Time {
	int year, minute, second;
public:
	Time() { cin >> year >> minute >> second; }
	friend void Display(Date& date, Time& time);
};

void Display(Date& date, Time& time) {
	cout <<setfill('0')<<setw(4)<< date.year << "-";
	cout <<setfill('0') << setw(2) << date.month << "-";
	cout << setfill('0') << setw(2) << date.day << " ";
	cout << setfill('0') << setw(2) << time.year << ":";
	cout << setfill('0') << setw(2) << time.minute << ":";
	cout << setfill('0') << setw(2) << time.second << endl;
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		Date d;
		Time t;
		Display(d, t);
	}
	return 0;
}

D. 判断矩形是否重叠(复合类+友元)

题目描述

用CPoint表示点,用两个CPoint对象表示矩形类CRect的对角线两点。分别实现CPoint类和CRect类,并在主函数用输入的坐标定义4个CPoint类对象,每2个CPoint对象再构造1个CRect对象,然后写个友元函数,判断2个矩形是否重叠。

输入

判断次数

矩形1的对角线顶点坐标x1, y1, x2, y2

矩形2的对角线顶点坐标x1, y1, x2, y2

输出

是否重叠

输入样例1

3
1 5 2 9
1 3 2 4
5 6 7 8
5 7 7 7
2 5 1 0
9 4 2 9

输出样例1

not overlapped
overlapped
overlapped

AC代码

#include<bits/stdc++.h>
using namespace std;

class CRect;

class CPoint {
	double x, y;
public:
	CPoint() { cin >> x >> y; }
	friend bool isOverlap(CRect& c1, CRect& c2);
};

class CRect {
	CPoint p1, p2;
public:
	CRect(CPoint& p1, CPoint& p2):p1(p1),p2(p2){}

	friend bool isOverlap(CRect& c1, CRect& c2);
};

bool isOverlap(CRect& c1, CRect& c2) {
	int c1up = max(c1.p1.y, c1.p2.y);
	int c1down = min(c1.p1.y, c1.p2.y);
	int c1left = min(c1.p1.x, c1.p2.x);
	int c1right = max(c1.p1.x, c1.p2.x);

	int c2up = max(c2.p1.y, c2.p2.y);
	int c2down = min(c2.p1.y, c2.p2.y);
	int c2left = min(c2.p1.x, c2.p2.x);
	int c2right = max(c2.p1.x, c2.p2.x);

	if (c1up < c2down)
		return false;
	if (c1down > c2up)
		return false;
	if (c1left > c2right)
		return false;
	if (c2left > c1right)
		return false;
	return true;

}

int main() {
	int t;
	cin >> t;
	while (t--) {
		CPoint p1, p2, p3, p4;
		CRect c1(p1, p2), c2(p3, p4);
		if (isOverlap(c1, c2))
			cout << "overlapped" << endl;
		else
			cout << "not overlapped" << endl;
	}
	return 0;
}

E. 银行账户(静态成员与友元函数)

题目描述

银行账户类的基本描述如下:

在这里插入图片描述

要求如下:

实现该银行账户类

为账户类Account增加一个友元函数,实现账户结息,要求输出结息后的余额(结息余额=账户余额+账户余额*利率)。友元函数声明形式为 friend void Update(Account& a);

在main函数中,定义一个Account类型的指针数组,让每个指针指向动态分配的Account对象,并调用成员函数测试存款、取款、显示等函数,再调用友元函数测试进行结息。

大家可以根据实际需求在类内添加其他方法,也可以修改成员函数的参数设置

输入

第1行:利率
第2行:账户数目n
第3行开始,每行依次输入一个账户的“账号”、“姓名”、“余额”、存款数,取款数。

输出

第1行开始,每行输出一个账户的相关信息,包括账号、姓名、存款后的余额、存款后结息余额、取款后余额。

最后一行输出所有账户的余额。

输入样例1

0.01
3
201501 张三 10000 1000 2000
201502 李四 20000 2000 4000
201503 王二 80000 4000 6000

输出样例1

201501 张三 11000 11110 9110
201502 李四 22000 22220 18220
201503 王二 84000 84840 78840
106170

AC代码

#include<bits/stdc++.h>
using namespace std;

class Account {
	static float TotalBalance;
	static int count;
	static float InterestRate;
	string _accno, _accname;
	float _banlance;
	float save, withdraw;
public:
	Account() {
		cin >> _accno >> _accname >> _banlance >> save >> withdraw;
	}

	static float getTotalBalance() {
		return TotalBalance;
	}

	friend void Update(Account& a) {
		a._banlance += a.save;
		a._banlance *= (1 + a.InterestRate);
		cout << a._banlance << " ";
		a._banlance -= a.withdraw;
		cout << a._banlance << endl;
		a.TotalBalance += a._banlance;
	}

	~Account()
	{
	}

	void Show() {
		cout << _accno  << " " << _accname << " " << _banlance +save << " ";
	}

	static int GetCount() { return count; }
	static float GetInterestRate() { return InterestRate; }

	static void changeRate() {
		cin >> InterestRate;
	}

};

int Account::count = 0;
float Account::InterestRate = 0;
float Account::TotalBalance = 0;

int main() {
	Account::changeRate();
	int n;
	cin >> n;
	Account* account = new Account[n];
	for (int i = 0; i < n; i++) {

		(account + i)->Show();
		Update(*(account + i));
	}
	cout << Account::getTotalBalance() << endl;
	delete[]account;

	return 0;
}

F. 电视机与遥控器(友元类)

题目描述

有如下的电视类和遥控器类,遥控器在电视开机的情况下可以控制电视。

在这里插入图片描述

在这里插入图片描述

要求如下:

  1. 实现并完善Tv类;其中构造函数需修改和完善。另:最大频道为100;

  2. 将Remote设为Tv的友元类,以支持在Remote类中对Tv方法的调用。

  3. 在main函数中,通过Remote实例对TV实例进行操作。

输入

第一行,电视初始状态,依次为state,volume,channel,mode,input的初始值。

第二行,利用遥控器对上述状态的操作指令,用对应的函数名表示,如增加音量为volup

输出

第一行,执行遥控器操作后的状态。

输入样例1

off 10 19 Cable VCR
onoff volup chanup set_mode set_input

输出样例1

on 11 20 Antenna TV

AC代码

#include<bits/stdc++.h>
using namespace std;


class TV {
	int state, volume, maxchannel, channel, mode, input;
	friend class Remote;
public:
	TV() {
		string _state, _mode, _input;
		cin >> _state >> volume >> channel >> _mode >> _input;
		_state == "on" ? state = 1 : state = 0;
		_mode == "Cable" ? mode = 0 : mode = 1;
		_input == "TV" ? input = 1 : input = 0;
	}

	void onoff() {
		state = !state;
	}

	bool isOn()const {
		return state;
	}

	bool volup() {
		if (!isOn())
			return false;

		volume++;
		if (volume > 20)
			return false;
		return true;
	}

	bool voldown() {
		if (!isOn())
			return false;

		volume--;
		if (volume < 0)
			return false;
		volume = 0;
	}

	void chanup() {
		if (!isOn())
			return;

		channel++;
		if (channel > 100)
			channel = 100;
	}

	void chandown() {
		if (!isOn())
			return;

		channel--;
		if (channel < 0)
			channel = 0;
	}

	void set_chan() {
		if (!isOn())
			return;

		channel = !channel;
	}

	void set_mode() {
		if (!isOn())
			return;

		mode = !mode;
	}

	void set_input() {
		if (!isOn())
			return;

		input = !input;
	}

	void settings() {
		if (state)
			cout << "on ";
		else
			cout << "off ";

		cout << volume << " " << channel << " ";

		if (!mode)
			cout << "Cable ";
		else
			cout << "Antenna ";

		if (!input)
			cout << "VCR" << endl;
		else
			cout << "TV" << endl;
	}

	int getMode() { return mode; }

};


class Remote {
	int mode;
public:
	Remote(int m) :mode(m) {}
	bool volup(TV& t) { return t.volup(); }
	bool voldown(TV& t) { return t.voldown(); }
	void onoff(TV& t) { t.onoff(); }
	void chanup(TV& t) { t.chanup(); }
	void chandown(TV& t) { t.chandown(); }
	void set_chan(TV& t, int c) { t.channel = c; }
	void set_mode(TV& t) { t.set_mode();}
	void set_input(TV& t) { t.set_input(); }
};

int main() {
	TV t;
	Remote r(t.getMode());
	string s;
	while(cin >> s) {
		if (s == "onoff")
			r.onoff(t);
		else if (s == "volup")
			r.volup(t);
		else if (s == "voldown")
			r.voldown(t);
		else if (s == "set_mode")
			r.set_mode(t);
		else if (s == "set_input")
			r.set_input(t);
		else if (s == "chanup")
			r.chanup(t);
		else if (s == "chandown")
			r.chandown(t);
	} 

	t.settings();
	return 0;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曹无悔

请支持我的梦想!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值