Shenzhen University Jiruan "Object-Oriented Programming" Experiment 8 Static and Friend

A. Hotel Guest Management (static member)

topic description

Write a program to realize the guest accommodation record function of a hotel.

Define a Customer class, require the input of the guest's name, and create a Customer object. The class declaration is as follows:
insert image description here

Call the Display function of the class to output the guest ID (the output sequence number occupies 4 digits, such as the first digit is 0001, the second digit is 0002, and so on), name, and total number of people. Static members are used for the total number of people and guest rent, and common data members are used for other attributes.

enter

Enter the number of tests t

For each test, first enter the current year,

Next, enter the customer's name in turn, and 0 means the end of the input.

output

Each row outputs customer information and hotel information in turn. Including customer name, customer number, total number of hotel occupants, hotel's current total revenue.

Input sample 1

2
2014
Zhang San Li Si Wang Wu 0
2015
Cindy David 0

output sample 1

Zhang San 20140001 1 150
Li Si 20140002 2 300 Wang Wu 20140003
3 450
Cindy 20150004 4 600
David 20150005 5 750

AC code

#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. Distance calculation (friend function)

topic description

The basic form of the Point class is as follows:

insert image description here

Please complete the following requirements:

  1. Implement the Point class;
  2. Add a friend function double Distance(Point &a, Point &b) to the Point class to calculate the distance between two points. Directly access the private data of the Point object for calculation.
  3. Write the main function, input the coordinates of two points, and calculate the distance between the two points.

enter

Line 1: Enter the number of point pairs whose distance is to be calculated

Starting from line 2, enter the x and y coordinates of two points in each line

output

Each line sequentially outputs the distance between a set of point pairs (the result is directly taken as an integer part, without rounding)

Input sample 1

2
1 0 2 1
2 3 2 4

output sample 1

1
1

AC code

#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. Combine date and time output (friend function)

topic description

The known date class Date has attributes: year, month, day, and other member functions can be written according to the needs. Note that this class has no output member functions

The known time class Time has attributes: hours, minutes, seconds, and other member functions can be written according to the needs. Note that this class has no output member functions

Now write a global function to combine the time and date objects and output them together,

The function prototype is: void Display(Date &, Time &)

The function output requirements are:

1. The output length of hours, minutes, and seconds is fixed at 2 digits, and 0 is added for less than 2 digits

2. The output length of the year is fixed at 4 digits, the output length of the month and day is fixed at 2 digits, and 0 is added for less than 2 digits

For example, March 3, 2017 at 19:05:18

Then the output is: 2017-03-03 19:05:18

program requirements

1. Use the function Display as a friend of time and date

2. Create a date object and a time object respectively, and save the date input and time input

3. Call the Display function to realize the combined output of date and time

enter

Enter t in the first line to indicate that there are t groups of examples

Then enter three integers in one line, indicating the year, month and day

Enter three integers on the next line, indicating hours, minutes and seconds

Enter t group examples in sequence

output

Output a date and time merged output result per line

output t lines

Input sample 1

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

output sample 1

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

AC code

#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. Determine whether the rectangles overlap (composite class + friend)

topic description

Use CPoint to represent the point, and use two CPoint objects to represent the two points on the diagonal of the rectangle class CRect. Implement the CPoint class and the CRect class respectively, and define 4 CPoint class objects with the input coordinates in the main function, construct a CRect object for every 2 CPoint objects, and then write a friend function to judge whether the two rectangles overlap.

enter

Judgment times

Diagonal vertex coordinates x1, y1, x2, y2 of rectangle 1

Diagonal vertex coordinates x1, y1, x2, y2 of rectangle 2

output

Does it overlap

Input sample 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

output sample 1

not overlapped
overlapped
overlapped

AC code

#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. Bank account (static members and friend functions)

topic description

The basic description of the bank account class is as follows:

insert image description here

Requirements are as follows:

Implement the bank account class

Add a friend function to the account class Account to realize account interest settlement, and require output of the balance after interest settlement (interest settlement balance = account balance + account balance * interest rate). The declaration form of friend function is friend void Update(Account& a);

In the main function, define a pointer array of Account type, let each pointer point to a dynamically allocated Account object, and call member functions to test deposit, withdrawal, display and other functions, and then call friend functions to test for interest settlement.

You can add other methods in the class according to actual needs, and you can also modify the parameter settings of member functions

enter

Line 1: Interest rate
Line 2: Number of accounts n
Starting from line 3, each line enters the "account number", "name", "balance", deposit amount, and withdrawal amount of an account in sequence.

output

Starting from line 1, each line outputs information about an account, including account number, name, balance after deposit, interest settlement balance after deposit, and balance after withdrawal.

The last line outputs the balance of all accounts.

Input sample 1

0.01
3
201501 Zhang San 10000 1000 2000
201502 Li Si 20000 2000 4000
201503 Wang Er 80000 4000 6000

output sample 1

201501 Zhang San 11000 11110 9110
201502 Li Si 22000 22220 18220
201503 Wang Er 84000 84840 78840
106170

AC code

#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. TV and remote control (friend class)

topic description

There are the following TV types and remote control types, and the remote control can control the TV when the TV is turned on.

insert image description here

insert image description here

Requirements are as follows:

  1. Realize and perfect the Tv class; the constructor needs to be modified and perfected. Another: the maximum channel is 100;

  2. Set Remote as a friend class of Tv to support calling the Tv method in the Remote class.

  3. In the main function, operate the TV instance through the Remote instance.

enter

The first line, the initial state of the TV, is the initial value of state, volume, channel, mode, and input in turn.

In the second line, use the remote control to operate the above state, and use the corresponding function name to represent it, such as increasing the volume as volup

output

The first line is the state after performing the remote control operation.

Input sample 1

off 10 19 Cable VCR
onoff volup chanup set_mode set_input

output sample 1

on 11 20 Antenna TV

AC code

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

Supongo que te gusta

Origin blog.csdn.net/weixin_46655675/article/details/129323724
Recomendado
Clasificación