实验任务5:
TextCoder.hpp文件源码:
1 #ifndef TEXTCODER_HPP 2 #define TEXTCODER_HPP 3 4 #include <string> 5 6 class TextCoder { 7 private: 8 std::string text; 9 10 void encoder(); 11 void decoder(); 12 13 public: 14 TextCoder(std::string input_text); 15 std::string get_ciphertext(); 16 std::string get_deciphertext(); 17 }; 18 19 #endif
1 // textcoder.cpp 2 #include "textcoder.hpp" 3 4 TextCoder::TextCoder(std::string input_text) { 5 text = input_text; 6 } 7 8 void TextCoder::encoder() { 9 for (char& c : text) { 10 if (isalpha(c)) { 11 if (islower(c)) { 12 c = 'a' + (c - 'a' + 7) % 26; 13 } else { 14 c = 'A' + (c - 'A' + 7) % 26; 15 } 16 } 17 } 18 } 19 20 void TextCoder::decoder() { 21 for (char& c : text) { 22 if (isalpha(c)) { 23 if (islower(c)) { 24 c = 'a' + (c - 'a' + 19) % 26; 25 } else { 26 c = 'A' + (c - 'A' + 19) % 26; 27 } 28 } 29 } 30 } 31 32 std::string TextCoder::get_ciphertext() { 33 encoder(); 34 return text; 35 } 36 37 std::string TextCoder::get_deciphertext() { 38 decoder(); 39 return text; 40 } 41 42 // main.cpp 43 #include "textcoder.hpp" 44 #include <iostream> 45 #include <string> 46 47 void test() { 48 using namespace std; 49 string text, encoded_text, decoded_text; 50 cout << "输入英文文本: "; 51 while (getline(cin, text)) { 52 encoded_text = TextCoder(text).get_ciphertext(); 53 cout << "加密后英文文本:\t" << encoded_text << endl; 54 decoded_text = TextCoder(encoded_text).get_deciphertext(); 55 cout << "解密后英文文本:\t" << decoded_text << endl; 56 cout << "\n输入英文文本: "; 57 } 58 } 59 60 int main() { 61 test(); 62 return 0; 63 }
运行测试结果截图 :
实验任务6
1 #ifndef INFO_HPP 2 #define INFO_HPP 3 4 #include <string> 5 #include <iostream> 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(std::string _nickname, std::string _contact, std::string _city, int _n) : nickname(_nickname), contact(_contact), city(_city), n(_n) {} 16 17 void print() { 18 std::cout << "昵称: " << nickname << ", 联系方式: " << contact << ", 城市: " << city << ", 预定参加人数: " << n << std::endl; 19 } 20 }; 21 22 #endif
1 #include "info.hpp" 2 #include <iostream> 3 #include <vector> 4 5 int main() { 6 const int capacity = 100; 7 std::vector<Info> audience_info_list; 8 int total_attendees = 0; 9 10 std::cout << "开始录入预约登记信息:" << std::endl; 11 while (total_attendees < capacity) { 12 std::string nickname, contact, city; 13 int n; 14 15 std::cout << "请输入昵称: "; 16 if (!std::getline(std::cin, nickname)) { 17 break; 18 } 19 20 std::cout << "请输入联系方式: "; 21 std::getline(std::cin, contact); 22 23 std::cout << "请输入所在城市: "; 24 std::getline(std::cin, city); 25 26 std::cout << "请输入预定参加人数: "; 27 std::cin >> n; 28 std::cin.ignore(); // Ignore the newline character 29 30 if (total_attendees + n > capacity) { 31 std::cout << "预定参加人数超过livehouse场地剩余容量,输入 'q' 退出预定,或输入 'u' 更新预定信息: "; 32 std::string choice; 33 std::cin >> choice; 34 if (choice == "q") { 35 break; 36 } else if (choice == "u") { 37 continue; 38 } 39 } 40 41 total_attendees += n; 42 Info new_info(nickname, contact, city, n); 43 audience_info_list.push_back(new_info); 44 } 45 46 std::cout << "\n预约参加livehouse的听众信息:" << std::endl; 47 for (const Info& info : audience_info_list) { 48 info.print(); 49 } 50 51 return 0; 52 }
运行测试结果截图 :