深圳大学计软《面向对象的程序设计》实验5 类和对象

A. 学生类定义(类和对象)

题目描述

面向对象程序设计的中心就是把客观事物抽象为程序世界里一段段代码,校园里的主体是学生,泛泛的学生包含很多属性,比如姓名、学号、所在学院、专业、性别、住址、联系电话。。。。。。等等,有这些属性,需要操纵它们的动作,比如读取姓名、设置姓名、读取学号、设置学号。。。。。。等等,这就是我们课堂说的属性和方法,对于属性和方法,我们又有访问控制方式限制,标示为public、private、protected等,根据以上的信息,请给出一个完整的学生类定义:Student,并测试输出n个该类对象的各项属性值。

输入

第一行表示要输入n个对象

后续各行输入不同对象的各属性值,每个对象一行。

输出

输出不同对象的各自属性

每个对象占一行

输入样例1

2
WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777

输出样例1

WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777

AC代码

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

class Student {
    
    
	string name, id, college, major, sex, address, phone;
public:
	Student() {
    
    }
	void setName(string name) {
    
     this->name = name; }
	void setId(string id) {
    
     this->id = id; }
	void setCollege(string college) {
    
     this->college = college; }
	void setMajor(string major) {
    
     this->major = major; }
	void setSex(string sex) {
    
     this->sex = sex; }
	void setAddress(string address) {
    
     this->address = address; }
	void setPhone(string phone) {
    
     this->phone = phone; }
	string getName() {
    
     return name; }
	string getId() {
    
     return id; }
	string getCollege() {
    
     return college; }
	string getMajor() {
    
     return major; }
	string getSex() {
    
     return sex; }
	string getAddress() {
    
     return address; }
	string getPhone() {
    
     return phone; }
};

int main() {
    
    
	int t;
	cin >> t;
	while (t--) {
    
    
		Student s;
		string name, id, college, major, sex, address, phone;
		cin >> name >> id >> college >> major >> sex >> address >> phone;
		s.setName(name);
		s.setId(id);
		s.setCollege(college);
		s.setMajor(major);
		s.setSex(sex);
		s.setAddress(address);
		s.setPhone(phone);
		cout << s.getName() << " " << s.getId() << " " << s.getCollege() 
		<< " " << s.getMajor() << " " << s.getSex() << " " << s.getAddress()
		 << " " << s.getPhone() << endl;
	}
}

B. 存折类定义(类与对象)

题目描述

定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。

编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。

输入

第一个存折的账号、姓名、余额

存款金额

取款金额

第二个存折的账号、姓名、余额

存款金额

取款金额

输出

第一个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

第二个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

输入样例1

9111 Tom 1000
500
1000
92220 John 500
500
1500

输出样例1

Tom’s balance is 1000
saving ok!
Tom’s balance is 1500
withdraw ok!
Tom’s balance is 500
John’s balance is 500
saving ok!
John’s balance is 1000
sorry! over limit!
John’s balance is 1000

AC代码

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

class CAccount {
    
    
	long account;
	char name[10];
	float balance;
public:
	CAccount() {
    
    
		cin >> account >> name >> balance;
	}

	void deposit(float data) {
    
    
		balance += data;
		cout << "saving ok!" << endl;
	}

	void withdraw(float data) {
    
    
		if (data > balance) {
    
    
			cout << "sorry! over limit!" << endl;
			return;
		}
		balance -= data;
		cout << "withdraw ok!" << endl;
	}

	void check() {
    
    
		cout << name << "'s balance is " << balance << endl;
	}


};

int main() {
    
    
	for (int i = 0; i < 2; i++) {
    
    
		CAccount account;
		account.check();
		int data;
		cin >> data;
		account.deposit(data);
		account.check();
		cin >> data;
		account.withdraw(data);
		account.check();
	}
	return 0;
}

C. 音像制品(类与对象)

题目描述

某商店出租音像制品,制品信息包括:类型、名称、租金单价、状态。

其中类型用单个数字表示,对应关系为:1-黑胶片,2-CD,3-VCD,4-DVD

名称是字符串,存储制品的名称信息

租金单价表示每天租金价格

状态用单个数字表示,0是未出租,1是已出租

商店提供业务操作包括

  1. 初始化(使用构造方法),从键盘输入音像制品的信息,并设置到对象中

  2. 查询Print,输出音像制品的信息

  3. 计算租金Fee,参数是租借的天数,输出租金总价,如果未出租则提示,具体输出信息看示范

请定义音像制品类,并创建相应的对象来完成操作

题目涉及的数值均用整数处理

输入

第一行输入n表示有n个音像制品

每个音像制品对应两行输入

一行输入一个音像制品的多个参数,具体为:类型、名称、租金单价、状态

一行输入操作命令,如果输入为0表示查询操作,非0则表示查询并且计算租金费用,租用天数就是这个非0值

依次输入2n行

输出

根据每个音像制品的操作命令,调用相应的操作,然后输出相关结果

输出样式看示范。

输入样例1

4
1 AAA 43 1
0
2 BBB 19 0
3
3 CCC 27 1
5
4 DDD 32 1
7

输出样例1

黑胶片[AAA]已出租
CD[BBB]未出租
未产生租金
VCD[CCC]已出租
当前租金为135
DVD[DDD]已出租
当前租金为224

AC代码

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

class AudiovisualProducts {
    
    
	int type;
	string name;
	int price;
	int state;
public:
	AudiovisualProducts() {
    
    
		cin >> type >> name >> price >> state;
	}

	void Print() {
    
    
		//1-黑胶片,2-CD,3-VCD,4-DVD
		string typeInfo[4] = {
    
     "黑胶片","CD","VCD","DVD" };
		cout << typeInfo[type - 1] << "[" << name << "]";
		if (state)
			cout << "已出租" << endl;
		else
			cout << "未出租" << endl;
	}

	void printFee(int day) {
    
    
		if (state) {
    
    
			cout << "当前租金为" << day * price << endl;
		}
		else cout << "未产生租金" << endl;
	}

};

int main() {
    
    
	int n;
	cin >> n;
	while (n--) {
    
    
		AudiovisualProducts test;
		int t;
		cin >> t;
		test.Print();
		if (t) {
    
    
			test.printFee(t);
		}
	}
}

D. 月份查询(类与对象+引用)

题目描述

定义一个日期类,包含私有属性年、月、日,添加属性的get方法,其他函数根据需要自行添加

定义一个函数,参数是日期对象引用,功能是输出该日期的月份英文名称,以及下一个月的名称,无返回值

定义一个函数,参数是日期对象引用,功能是输出该日期距离年底还有多少天,例如12月1日距离年底是30天

月份英文名称如下:

1月 January

2月 February

3月 March

4月 April

5月 May

6月 June

7月 July

8月 August

9月 September

10月 October

11月 November

12月 December

输入

第一行输入n表示有n个日期对象

接着每行输入一个日期的年、月、日

依次输入n行

输出

每个日期包含两行输出

一行输出当前日期的月份英文名称以及下一个月的英文名称

一行输出该日期距离年底还有多少天

具体内容格式看示范

输入样例1

3
1999 9 9
1996 2 11
2019 12 1

输出样例1

This month is September and next month is October
There are 113 days to the end of the year
This month is February and next month is March
There are 324 days to the end of the year
This month is December and next month is January
There are 30 days to the end of the year

AC代码

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

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; }
	int getYear() {
    
     return year; }
	int getMonth() {
    
     return month; }
	int getDay() {
    
     return day; }
	string getNameOfMonth() {
    
     return name[month - 1]; }
	string getNextMonthName() {
    
     return name[month % 12]; }
	bool isEndDay() {
    
     return month == 12 && day == 31; }

	bool isLeapYear() {
    
    
		if (year % 400 == 0)
			return true;
		if (year % 4 == 0 && year % 100 != 0)
			return true;
		return false;
	}


	Date getTomorrow() {
    
    
		int days[12] = {
    
     31,28,31,30,31,30,31,31,30,31,30,31 };
		Date nextDay = *this;
		if (nextDay.isLeapYear())
			days[1] = 29;

		nextDay.day++;

		if (nextDay.day > days[nextDay.month - 1]) {
    
    
			nextDay.month++;
			nextDay.day = 1;
		}

		if (nextDay.month > 12) {
    
    
			nextDay.year++;
			nextDay.month = 1;
		}

		return nextDay;
	}
};

void printMonth(Date& date) {
    
    
	cout << "This month is " << date.getNameOfMonth() << " and next month is " << date.getNextMonthName() << endl;
}

void printRestDay(Date& date) {
    
    
	int cnt = 0;
	Date today = date;
	while (!today.isEndDay()) {
    
    
		today = today.getTomorrow();
		cnt++;
	}

	printf("There are %d days to the end of the year\n", cnt);
}

int main() {
    
    
	int n;
	cin >> n;
	while (n--) {
    
    
		Date test;
		printMonth(test);
		printRestDay(test);
	}
	return 0;
}

E. 链表原地反转(链表)

题目描述

按数字输入顺序创建单链表。不可借助数组、容器,不可开辟新结点空间。编写函数,在原链表上实现单链表的反转。例如单链表10->20->30,反转后变为单链表30->20->10。

注:不符合题目要求,使用上题逆序输出不计分。

输入

测试次数t

每组测试数据一行,格式如下:

数据个数n,后跟n个整数

输出

对每组测试数据,输出反转后的单链表。

输入样例1

2
10 1 2 3 4 5 6 7 8 9 10
4 19 20 15 -10

输出样例1

10 9 8 7 6 5 4 3 2 1
-10 15 20 19

AC代码

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

struct Node
{
    
    
	int value;
	Node* next;
	Node(int value = 0, Node* next = NULL) {
    
    
		this->value = value;
		this->next = next;
	}

};


class List {
    
    
	Node* head;
public:
	List() {
    
    
		int len;
		cin >> len;
		head = new Node(len);
		Node* p = head;
		for (int i = 0; i < len; i++) {
    
    
			int t;
			cin >> t;
			p->next = new Node(t);
			p = p->next;
		}
	}


	~List() {
    
    
		Node* p = head;
		while (head) {
    
    
			p = head->next;
			delete head;
			head = p;
		}
	}

	void print() {
    
    
		Node* p = head->next;
		while (p) {
    
    
			cout << p->value << " ";
			p = p->next;
		}
		cout << endl;
	}

	void reverse() {
    
    
		if (head->value <= 1)
			return;
		Node* next_node = NULL;
		Node* cur_node = head->next;
		while (cur_node) {
    
    
			Node* prior_node = cur_node->next;
			cur_node->next = next_node;
			head->next = prior_node;
			next_node = cur_node;
			cur_node = prior_node;
		}
		head->next = next_node;
	}

};

int main() {
    
    
	int t;
	cin >> t;
	while (t--) {
    
    
		List l;
		l.reverse();
		l.print();
	}
	return 0;
}

F. 买彩游戏(类和对象)

题目描述

参见随机数练习要求:

我们知道福利彩票销售中也有通过机选随机生成你需要的号码,现假设福利彩票共有七个号码,每个号码的区间为0-30,现在请你利用C++语言设计一个生成七个福利彩票号码的随机机器,当用户确定下注后,输出七个不同的号码。(需要利用随机函数rand()、srand(),具体用法参见参考书)。

在此基础上,你已有了一个n组的福利彩票号码(无需随机生成),现假定给出摇奖后的一组号码为:6,13,1,24,28,8,10,把你所买的每组号码依次匹配,当从左至右各位置上的号码一一相同,则表示你中了一等奖,如果有5、6个相同则为二等奖,余下大于两个以上相同的号码均为三等奖,编程实现该游戏规则!

输入

第一行输入所买彩票的组数;

第二行输入买入彩票的客户名称;

第三行开始每行输入每组福利彩票的号码;

最后行输入摇奖中奖号码。

输出

第一行输出客户名称;

第二行根据摇奖号码输出客户所中的奖金等级,比如:

恭喜你 中了(多少注)N等奖!或者:

加油!继续!

输入样例1

2
Tom
2 5 3 24 10 8 9
20 23 30 1 5 9 2
6 13 1 24 28 8 10

输出样例1

恭喜Tom中了1注三等奖!

AC代码

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


class Ticket {
    
    
	vector<vector<int>>group;
	int answer[7] = {
    
     6,13,1,24,28,8,10 };
	int firstCnt, secontCnt, thirdCnt;
	string name;
public:
	Ticket() {
    
    
		int n;
		cin >> n >> name;
		group.resize(n);
		for (int i = 0; i < n; i++) {
    
    
			group[i].resize(7);
			for (int j = 0; j < 7; j++)
				cin >> group[i][j];
		}
		for (int i = 0; i < 7; i++)
			cin >> answer[i];
		firstCnt = secontCnt = thirdCnt = 0;
	}

	void compare() {
    
    
		for (int i = 0; i < group.size(); i++) {
    
    
			int temp_cnt = 0;

			for (int j = 0; j < 7; j++)
				if (group[i][j] == answer[j])
					temp_cnt++;

			//一等奖?
			if (temp_cnt == 7) {
    
    
				firstCnt++;
				continue;
			}


			//二等奖?
			if (group[i][4] == answer[4] && group[i][5] == answer[5]) {
    
    
				secontCnt++;
				continue;
			}

			//三等奖
			if (temp_cnt >= 2)
				thirdCnt++;
		}
	}

	void display() {
    
    
		if (firstCnt)
			cout << "恭喜" << name << "中了" << firstCnt << "注一等奖!" << endl;
		if (secontCnt)
			cout << "恭喜" << name << "中了" << secontCnt << "注二等奖!" << endl;
		if (thirdCnt)
			cout << "恭喜" << name << "中了" << thirdCnt << "注三等奖!" << endl;
		if (!firstCnt && !secontCnt && !thirdCnt)
			cout << "加油!继续!" << endl;
	}

};

int main() {
    
    
	Ticket t;
	t.compare();
	t.display();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46655675/article/details/129311404