实验4 现代C++标准库与类模板

发布时间 2023-11-30 20:36:11作者: 袁德聪

任务5

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class TextCoder {
 6 public:
 7     TextCoder() = default;
 8     TextCoder(string str);
 9     string get_ciphertext();
10     string get_deciphertext();
11     ~TextCoder() = default;
12 
13 private:
14     string text;
15     void encoder();
16     void decoder();
17 };
18 
19 TextCoder::TextCoder(string str) {
20     text = str;
21 }
22 
23 string TextCoder::get_ciphertext() {
24     encoder();
25     return text;
26 }
27 
28 string TextCoder::get_deciphertext() {
29     decoder();
30     return text;
31 }
32 
33 void TextCoder::encoder() {
34     for (int i = 0; i < text.length(); i++) {
35         if (islower(text[i])) {
36             text[i] = (text[i] - 'a' + 7) % 26 + 'a';
37         }
38         else if (isupper(text[i])) {
39             text[i] = (text[i] - 'A' + 7) % 26 + 'A';
40         }
41     }
42 }
43 
44 void TextCoder::decoder() {
45     for (int i = 0; i < text.length(); i++) {
46         if (islower(text[i])) {
47             text[i] = (text[i] - 'a' + 26 - 7) % 26 + 'a';
48         }
49         else if (isupper(text[i])) {
50             text[i] = (text[i] - 'A' + 26 - 7) % 26 + 'A';
51         }
52     }
53 }
textcoder.hpp
 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     string text, encoded_text, decoded_text;
 9 
10     cout << "输入英文文本: ";
11     while (getline(cin, text)) {
12         encoded_text = TextCoder(text).get_ciphertext();
13         cout << "加密后英文文本:\t" << encoded_text << endl;
14 
15         decoded_text = TextCoder(encoded_text).get_deciphertext();
16         cout << "解密后英文文本:\t" << decoded_text << endl;
17         cout << "\n输入英文文本: ";
18     }
19 }
20 
21 int main() {
22     test();
23 }
task5.cpp

 

任务6

 1 #include "Info.hpp"
 2 #include <iostream>
 3 #include <vector>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 const int capacity = 100;
 9 static int total = 0;
10 
11 int main() {
12     vector<Info> audience_info_list;
13     string nickname_, contact_, city_;
14     int n_;
15     cout << "录入信息:" << endl << "\n";
16     cout << "昵称   联系方式(邮箱/手机号)   所在城市   预定参加人数" << endl;
17     while (cin >> nickname_)
18     {
19         cin >> contact_ >> city_ >> n_;
20         if (total + n_ <= capacity) {
21             Info tmp(nickname_, contact_, city_, n_);
22             audience_info_list.push_back(tmp);//将新的听众信息存入list 
23             total += n_;
24             if (total == capacity)
25                 break;
26         }
27         else
28         {
29             char option;
30             cout << "对不起,只剩" << capacity - total << "个位置."
31                 << "\n1. 输入u,更新(update)预定信息"
32                 << "\n2. 输入q,退出预定"
33                 << "\n你的选择:";
34             cin >> option;
35             if (option == 'u')
36             {
37                 cout << "请重新输入:" << endl;
38             }
39             if (option == 'q')
40             {
41                 cout << endl;
42                 break;
43             }
44         }
45     }
46     cout << "截至目前,一共有" << total << "位听众预定参加。预定听众信息如下:" << endl;
47     for (auto& list : audience_info_list)
48         list.print();
49     return 0;
50 }
task6.cpp
 1 #include <iostream>
 2 #include <string>
 3 #include <iomanip>
 4 using namespace std;
 5 
 6 class Info {
 7 public:
 8     Info(const string nickname_, const string contact_, const string city_, int n_);
 9     ~Info() = default;
10     void print() const;
11 
12 private:
13     string nickname;
14     string contact;
15     string city;
16     int n;
17 };
18 
19 Info::Info(const string nickname_, const string contact_, const string city_, int n_)
20     : nickname(nickname_), contact(contact_), city(city_), n(n_) {}
21 
22 void Info::print() const {
23     cout << left << endl
24         << "昵称:" << setw(15) << nickname << endl
25         << "联系方式:" << setw(15) << contact << endl
26         << "所在城市:" << setw(15) << city << endl
27         << "预定人数:" << setw(15) << n << endl;
28 }
Info.hpp