实验结论

实验任务5

某独立音乐人要举办一场免费小型liveshow。livehouse场地容量有限,最多容纳100位乐迷听众。现通
过某平台开通线上预约登记。线上预约登记信息类Info如下:

 

Info.hpp

 

实验二_ios实验二_#include_02
 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 class Info {
 9 private:
10     string nickname;
11     string contact;
12     string city;
13     int n;
14     static int total_number;
15 public:
16     Info(string _nickname, string _contact, string _city, int _n);
17     void print();
18     static int get_total_number();
19 };
20 
21 int Info::total_number = 0;
22 
23 Info::Info(string _nickname, string _contact, string _city, int _n) : nickname(_nickname), contact(_contact), city(_city), n(_n) {
24     total_number += n;
25 }
26 
27 int Info::get_total_number() {
28     return total_number;
29 }
30 void Info::print() {
31     cout << setiosflags(ios::left);
32     cout << setw(10) << "称呼:" << nickname << endl;
33     cout << setw(10) << "联系方式:" << contact << endl;
34     cout << setw(10) << "所在城市:" << city << endl;
35     cout << setw(10) << "预定人数:" << n << endl;
36 
37 }
View Code

 

main.cpp

 

实验二_ios实验二_#include_02
 1 #include "info.hpp"
 2 #include <vector>
 3 
 4 void say_book();
 5 void show_select();
 6 
 7 int main() {
 8     vector<Info> audience_info_list;
 9     const int capacity = 100;
10     string id, contract, city;
11     int attend_number;
12 
13     // 录入信息开始
14     cout << "录入信息:\n" << endl;
15     say_book();
16     
17     while (getline(cin, id, ' ') && getline(cin, contract, ' ') && getline(cin, city, ' ')) {
18         cin >> attend_number;
19         // 判断是否将订单放入数组
20         if (Info::get_total_number() + attend_number <= capacity) {
21             Info order(id, contract, city, attend_number);
22             audience_info_list.push_back(order);
23         }
24         else {
25             cout << "对不起,只剩" << capacity - Info::get_total_number() << "个位置" << endl;
26             show_select();
27             char ch;
28             cin >> ch;
29             while (ch != 'q' && ch != 'u') {
30                 cout << "输入错误,请重试" << endl;
31                 show_select();
32                 cin >> ch;
33             }
34             if (ch == 'q') {
35                 break;
36             }
37             else {
38                 cout << "继续预定" << endl;
39                 say_book();
40             }
41         }
42     }
43     cout << "截至目前,一共有" << Info::get_total_number() << "位观众预订参加。预订听众信息如下:" << endl;
44 
45     for (int i = 0; i < audience_info_list.size(); i++) {
46         audience_info_list[i].print();
47     }
48 }
49 
50 void say_book() {
51     cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
52 }
53 
54 void show_select() {
55     cout << "1.输入u,更新(update)预定信息" << endl;
56     cout << "2.输入q,退出预订" << endl;
57     cout << "你的选择:" << endl;
58 }
View Code

 

运行效果图如下:

实验二_#include_05

 

 

实验任务6

设计并实现一个类TextCoder,用于对英文文本字符串进行简单的加密和解密操作

 

TextCoder.hpp

 

实验二_ios实验二_#include_02
 1 #pragma once
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 
 6 class TextCoder {
 7 private :
 8     string text;
 9 public:
10     TextCoder(string _text);
11     string encoder();
12     string decoder();
13 };
14 
15 TextCoder::TextCoder(string _text) : text(_text) {
16 
17 }
18 
19 string TextCoder::encoder() {
20     string answer;
21     
22     for (auto ch : text) {
23         if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
24             ch += 5;
25             if ((ch < 'a' && ch > 'U') || ch > 'z') {
26                 ch -= 26;
27             }
28         }
29         answer += ch;
30     }
31     return answer;
32 }
33 
34 string TextCoder::decoder() {
35     string answer;
36 
37     for (auto ch : text) {
38         if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
39             ch -= 5;
40             if (ch < 'A' || (ch > 'Z' && ch < 'a')) {
41                 ch += 26;
42             }
43         }
44         answer += ch;
45     }
46     return answer;
47 }
View Code

 

main.hpp

 

实验二_ios实验二_#include_02
 1 #include "Textcoder.hpp"
 2 
 3 int main()
 4 {
 5     using namespace std;
 6     string text, encoded_text, decoded_text;
 7     cout << "输入英文文本: ";
 8     while (getline(cin, text))
 9     {
10         encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象
11         cout << "加密后英文文本:\t" << encoded_text << endl;
12         decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
13             cout << "解密后英文文本:\t" << decoded_text << endl;
14         cout << "\n输入英文文本: ";
15     }
16 }
View Code

 

运行效果图如下:

 

实验二_用户名_10

 

实验总结

实验5

vector数组的方法还需要熟悉熟悉才能熟练使用

我的代码在实现了老师的运行效果图情况下加了一个u,q的安全保证,提高程序的稳定性

针对^Z的退出问题:

可以使用getline()方法,同时,由于用户可能中途反悔,需要考虑输入一个用户名以后按^Z这种不讲武德的行为,可以把输入都放在while循环中

具体实现可以阅读我的代码,谢谢您


实验六

这个实验相对于实验五比较简单,只需要会连接字符串即可。

得益于c++的运算符重载,可以直接使用“+”运算符进行连接