深セン大学コンピュータソフトウェア「オブジェクト指向プログラミング」実験5のクラスとオブジェクト

A. Student クラスの定義 (クラスとオブジェクト)

トピックの説明

オブジェクト指向プログラミングの中心は、プログラムの世界で客観的なものをコードに抽象化することであり、キャンパスの主体は学生であり、一般学生には名前、学生番号、学部、専攻、性別、住所、連絡先番号。待ってください。名前の読み取り、名前の設定、学生番号の読み取り、学生番号の設定など、操作する必要のある属性とアクションがあります。待ってください。これらはクラスで説明した属性とメソッドです。属性とメソッドについては、パブリック、プライベート、保護などとしてマークされたアクセス制御制限があります。上記の情報に従って、完全な学生クラス定義を入力してください: Student 、このタイプの n 個のオブジェクトの属性値を出力するテストを行います。

入力

最初の行は、n 個のオブジェクトが入力されることを示します

後続の行には、さまざまなオブジェクトの各プロパティの値が入力されます (オブジェクトごとに 1 行)。

出力

さまざまなオブジェクトのそれぞれのプロパティを出力します

オブジェクトごとに 1 行

入力サンプル1

2
WangHai 2014150112 CSSE ComputerScience 男性 South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering 女性 South318 13677777777

出力サンプル1

WangHai 2014150112 CSSE ComputerScience 男性 South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering 女性 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. Passbook クラス定義(クラスとオブジェクト)

トピックの説明

通帳クラス CAccount を定義します。通帳クラスには、口座番号 (account、long)、名前 (name、char[10])、残高 (balance、float) などのデータ メンバーと、入金を実現できるその他のデータ メンバーが含まれます (deposit、操作が成功した場合は「保存 OK!」と表示されます))、出金 (出金、操作が成功した場合は「出金 OK!」)、残高確認 (チェック)、出金額は残高の範囲内である必要があります。それ以外の場合は、"申し訳ありません!制限を超えています!」と表示されます。

main関数を記述し、このクラスのオブジェクトを作成してテストし、口座番号、名前、残高を入力した後、クエリ残高、入金、クエリ残高、出金、クエリ残高の順にクラスメソッドを呼び出して出力します。

入力

1通目の通帳の口座番号、氏名、残高

入金額

出金額

2通目の通帳の口座番号、氏名、残高

入金額

出金額

出力

1通目の通帳の残高

入金操作結果

勘定残高

出金操作結果

勘定残高

2通目の通帳の残高

入金操作結果

勘定残高

出金操作結果

勘定残高

入力サンプル1

9111 トム 1000
500
1000
92220 ジョン 500
500
1500

出力サンプル1

トムの残高は1000
貯金OK!
トムの残高は1500
出金OK!
トムの残高は 500
ジョンの残高は 500
節約OK!
ジョンの残高は 1000 です、
ごめんなさい! 限度を超えて!
ジョンの残高は 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

名前は文字列で、製品の名前情報が格納されます。

家賃単価とは1日あたりの家賃のことです

ステータスは単一の数値で表され、0 はリースされていません、1 はリースされています

店舗では以下のような事業運営を行っております。

  1. 初期化(構築メソッドを使用)し、オーディオビジュアル製品の情報をキーボードから入力し、オブジェクトに設定します

  2. 印刷、出力オーディオおよびビデオ製品情報のクエリ

  3. レンタル料金を計算します。パラメーターはレンタル日数です。レンタル料金の合計を出力します。レンタルされていない場合はプロンプトが表示されます。具体的な出力情報については、デモを参照してください。

オーディオビジュアル製品クラスを定義し、対応するオブジェクトを作成して操作を完了してください。

質問に含まれる数値はすべて整数として扱われます

入力

最初の行に「n」と入力すると、オーディオビジュアル製品が n 個あることを意味します。

各オーディオおよびビデオ製品は 2 系統の入力に対応します

オーディオビジュアル製品の複数のパラメーター (具体的には、タイプ、名前、レンタル単価、ステータス) を 1 行に入力します。

操作コマンドを 1 行で入力します。入力が 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月3月

4月 April

5月 May

6月 June

7月

8月8月

9月 9月

10月 October

11月11月

12月 December

入力

n 個の日付オブジェクトがあることを示すには、最初の行に「n」と入力します。

次に、各行に日付の年、月、日を入力します。

n 行を順番に入力します

出力

各日付には 2 行の出力が含まれます

現在の日付の月の英語名と翌月の英語名を1行に出力します。

日付と年末の何日前を出力する 1 行

特定のコンテンツ形式は例を参照

入力サンプル1

3
1999 9 9
1996 2 11
2019 12 1

出力サンプル1

今月は9月で来月は10月です
年末まであと113日あります
今月は2月で来月は3月です
年末まであと324日あります
今月は12月で来月は1月です
30日あります年末まであと数日

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

テスト データのセットごとに 1 行、形式は次のとおりです。

データの数 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. 宝くじゲーム (クラスとオブジェクト)

トピックの説明

乱数演習の要件を参照してください。

福祉宝くじの販売でも、必要な数字は機械の選択によってランダムに生成されることがわかっています。福祉宝くじには合計 7 つの数字があり、それぞれの数字の間隔が 0 ~ 30 であるとします。次に、C++ 言語を使用して、次のことを行ってください。 7 つの福祉宝くじ番号を生成するプログラムを設計し、ユーザーが賭けを決定すると、ランダム マシンが 7 つの異なる番号を出力します。(ランダム関数 rand()、srand() を使用する必要があります。具体的な使用法については参考書を参照してください。)

これに基づいて、福利厚生の宝くじ番号のグループがすでに n 個あるとします (ランダムに生成する必要はありません)。宝くじが発行された後の番号のグループが 6、13、1、24、28、8、10 であると仮定します。購入した数字が順番に一致します。左から右まで、各位置の数字が 1 つずつ同じであれば、1 等を獲得したことを意味します。同じ数字が 5 つまたは 6 つあれば、それは 1 等です。 2等、残り2以上 上記と同じ数字が3等 ゲームのルールをプログラミングで実現!

入力

最初の行には、購入した宝くじのグループ数が入力されます。

2 行目には、宝くじを購入した顧客の名前を入力します。

福祉宝くじの各グループの枚数を3行目から各行に入力します。

最後の行に宝くじの当選番号を入力します。

出力

最初の行は顧客名を出力します。

2 行目は、宝くじ番号に従って顧客が獲得したボーナス レベルを出力します。例:

おめでとうございます。N 賞 (何回ベット) を獲得しました! または:

来て!続く!

入力サンプル1

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

出力サンプル1

トムが 1 ベットの 3 等賞を獲得したことをおめでとうございます!

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

Guess you like

Origin blog.csdn.net/weixin_46655675/article/details/129311404