实验4

发布时间 2023-11-28 17:08:11作者: 汤谷雨
 1 #include <iostream>
 2 #include <string>
 3 
 4 class TextCoder {
 5 private:
 6     std::string text;
 7 
 8     void encoder() {
 9         for (auto& c : text) {
10             if (c >= 'a' && c <= 'z') {
11                 c = (c - 'a' + 7) % 26 + 'a';
12             } else if (c >= 'A' && c <= 'Z') {
13                 c = (c - 'A' + 7) % 26 + 'A';
14             }
15         }
16     }
17 
18     void decoder() {
19         for (auto& c : text) {
20             if (c >= 'a' && c <= 'z') {
21                 c = (c - 'a' + 19) % 26 + 'a';
22             } else if (c >= 'A' && c <= 'Z') {
23                 c = (c - 'A' + 19) % 26 + 'A';
24             }
25         }
26     }
27 
28 public:
29     TextCoder() {}
30     TextCoder(std::string str) : text(str) {}
31 
32     std::string get_ciphertext() {
33         encoder();
34         return text;
35     }
36 
37     std::string get_deciphertext() {
38         decoder();
39         return text;
40     }
41 };
textcoder.hpp
 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 void test() {
 5 using namespace std;
 6 string text, encoded_text, decoded_text;
 7 cout << "输入英文文本: ";
 8 while (getline(cin, text)) {
 9 encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象
10 cout << "加密后英文文本:\t" << encoded_text << endl;
11 decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
12 cout << "解密后英文文本:\t" << decoded_text << endl;
13 cout << "\n输入英文文本: ";
14 }
15 }
16 int main() {
17     test();
18 }
task5.cpp

任务5

 

 

 

 

 1 #ifndef INFO_HPP
 2 #define INFO_HPP
 3 
 4 #include <iostream>
 5 #include <string>
 6 
 7 class Info {
 8 private:
 9     std::string nickname;
10     std::string contact;
11     std::string city;
12     int n;
13 
14 public:
15     Info() {}
16     Info(std::string nickname, std::string contact, std::string city, int n)
17         : nickname(nickname), contact(contact), city(city), n(n) {}
18 
19     void print() const {
20         std::cout << "昵称: " << nickname << std::endl;
21         std::cout << "联系方式: " << contact << std::endl;
22         std::cout << "所在城市: " << city << std::endl;
23         std::cout << "预定参加人数: " << n << std::endl;
24     }
25 };
26 
27 #endif
info.hpp
 1 #include <iostream>
 2 #include <vector>
 3 #include "info.hpp"
 4 
 5 const int capacity = 100;
 6 
 7 int main() {
 8     std::vector<Info> audience_info_list;
 9     std::string nickname, contact, city;
10     int n;
11 
12     while (audience_info_list.size() < capacity) {
13         std::cout << "请输入昵称:";
14         std::getline(std::cin, nickname);
15 
16         std::cout << "请输入联系方式:";
17         std::getline(std::cin, contact);
18 
19         std::cout << "请输入所在城市:";
20         std::getline(std::cin, city);
21 
22         std::cout << "请输入预定参加人数:";
23         std::cin >> n;
24         std::cin.ignore(); // 忽略输入缓冲区中的换行符
25 
26         if (n > capacity - audience_info_list.size()) {
27             std::string choice;
28             std::cout << "预定参加人数超过场地容量剩余限制,输入 q 退出预定,输入 u 更新预定信息:";
29             std::getline(std::cin, choice);
30 
31             if (choice == "q") {
32                 break;
33             } else if (choice == "u") {
34                 continue;
35             }
36         }
37 
38         Info info(nickname, contact, city, n);
39         audience_info_list.push_back(info);
40     }
41 
42     for (const auto& info : audience_info_list) {
43         info.print();
44         std::cout << std::endl;
45     }
46 
47     return 0;
48 }
task6.cpp

任务6