实验4
实验任务5
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class TextCoder { 6 private: 7 string text; 8 void encoder(); 9 void decoder(); 10 public: 11 TextCoder(); 12 TextCoder(string str); 13 string get_ciphertext(); 14 string get_deciphertext(); 15 }; 16 17 TextCoder::TextCoder() { 18 text = ""; 19 } 20 TextCoder::TextCoder(string str) { 21 text = str; 22 } 23 24 void TextCoder::encoder() { 25 for (int i = 0; i < text.length(); i++) { 26 if (isalpha(text[i])) { 27 if (islower(text[i])) { 28 text[i] = (text[i] - 'a' + 7) % 26 + 'a'; 29 } 30 else { 31 text[i] = (text[i] - 'A' + 7) % 26 + 'A'; 32 } 33 } 34 } 35 } 36 37 void TextCoder::decoder() { 38 for (int i = 0; i < text.length(); i++) { 39 if (isalpha(text[i])) { 40 if (islower(text[i])) { 41 text[i] = (text[i] - 'a' + 19) % 26 + 'a'; 42 } 43 else { 44 text[i] = (text[i] - 'A' + 19) % 26 + 'A'; 45 } 46 } 47 } 48 } 49 50 string TextCoder::get_ciphertext() { 51 encoder(); 52 return text; 53 } 54 55 string TextCoder::get_deciphertext() { 56 decoder(); 57 return text; 58 } 59 60 int main() { 61 string str; 62 while (getline(cin, str)) { 63 TextCoder coder(str); 64 string ciphertext = coder.get_ciphertext(); 65 string deciphertext = coder.get_deciphertext(); 66 cout << "加密后的文本:" << ciphertext << endl; 67 cout << "解密后的文本:" << deciphertext << endl; 68 } 69 return 0; 70 }
实验任务6
1 #ifndef INFO_HPP 2 #define INFO_HPP 3 4 #include <iostream> 5 #include <string> 6 7 class Info { 8 public: 9 Info(const std::string& nickname, const std::string& contact, const std::string& city, int n) 10 : nickname_(nickname), contact_(contact), city_(city), n_(n) {} 11 12 void print() const { 13 std::cout << "昵称:" << nickname_ << std::endl; 14 std::cout << "联系方式:" << contact_ << std::endl; 15 std::cout << "所在城市:" << city_ << std::endl; 16 std::cout << "预定参加人数:" << n_ << std::endl; 17 } 18 19 private: 20 std::string nickname_; 21 std::string contact_; 22 std::string city_; 23 int n_; 24 }; 25 26 #endif
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include "info.hpp" 5 6 const int capacity = 100; 7 8 int main() { 9 std::vector<Info> audience_info_list; 10 int total_num = 0; 11 std::string nickname, contact, city; 12 int n; 13 while (total_num < capacity) { 14 std::cout << "请输入昵称:"; 15 if (!(std::cin >> nickname)) { 16 break; 17 } 18 std::cout << "请输入联系方式:"; 19 if (!(std::cin >> contact)) { 20 break; 21 } 22 std::cout << "请输入所在城市:"; 23 if (!(std::cin >> city)) { 24 break; 25 } 26 std::cout << "请输入预定参加人数:"; 27 if (!(std::cin >> n)) { 28 break; 29 } 30 if (total_num + n > capacity) { 31 std::cout << "预定参加人数超过场地剩余容量,输入q退出预定,输入u更新预定信息:"; 32 std::string option; 33 if (!(std::cin >> option)) { 34 break; 35 } 36 if (option == "q") { 37 break; 38 } 39 else if (option == "u") { 40 continue; 41 } 42 } 43 audience_info_list.emplace_back(nickname, contact, city, n); 44 total_num += n; 45 } 46 std::cout << "预约参加livehouse的听众信息:" << std::endl; 47 for (const auto& info : audience_info_list) { 48 info.print(); 49 std::cout << std::endl; 50 } 51 return 0; 52 }