实验4 现代C++标准库与类模板
实验任务5
TextCoder.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 class TextCoder { 9 private: 10 string text; 11 private: 12 void encoder(); 13 void decoder(); 14 public: 15 string get_ciphertext(); 16 string get_deciphertext(); 17 TextCoder(string &s); 18 }; 19 20 void TextCoder::encoder() { 21 for (auto &i : text){ 22 if (i >= 'a' && i <= 'z') 23 i = 'a' + ((i - 'a') + 7) % 26; 24 else if (i >= 'A' && i <= 'Z') 25 i = 'A' + ((i - 'A') + 7) % 26; 26 } 27 } 28 29 void TextCoder::decoder() { 30 for (auto &i : text){ 31 if (i >= 'a' && i <= 'z') 32 i = 'a' + ((i - 'a') + 26 - 7) % 26; 33 else if (i >= 'A' && i <= 'Z') 34 i = 'A' + ((i - 'A') + 26 - 7) % 26; 35 } 36 } 37 38 string TextCoder::get_ciphertext() { 39 encoder(); 40 return text; 41 } 42 43 string TextCoder::get_deciphertext() { 44 decoder(); 45 return text; 46 } 47 48 TextCoder::TextCoder(string &s): text{s} { 49 }
task5.cpp
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 }
运行测试结果
实验任务6
info.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 class Info { 9 private: 10 string nickname; 11 string contact; 12 string city; 13 int n; 14 public: 15 Info(const string& nickname, const string& contact, const string& city, const int& n); 16 void print() const; 17 }; 18 19 Info::Info(const string& nickname, const string& contact, const string& city, const int& n): nickname{nickname}, contact{contact}, city{city}, n{n} { 20 } 21 22 void Info::print() const { 23 cout << "昵称:\t\t" << nickname << endl; 24 cout << "联系方式:\t" << contact << endl; 25 cout << "所在城市:\t" << city << endl; 26 cout << "预定人数:\t" << n << endl; 27 }
task6.cpp
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include "info.hpp" 5 6 using namespace std; 7 8 int main() { 9 const int capacity = 100; 10 11 int reserved = 0; 12 string nickname, contact, city; 13 int n; 14 15 vector<Info> audience_info_list; 16 vector<Info> &v = audience_info_list; 17 18 cout << "录入信息:\n"; 19 cout << "昵称\t\t"; 20 cout << "联系方式(邮箱/手机号)\t\t"; 21 cout << "所在城市\t"; 22 cout << "预定人数" << endl; 23 24 while(cin >> nickname >> contact >> city >> n) 25 { 26 if(reserved + n <= capacity) 27 { 28 Info in(nickname, contact, city, n); 29 v.push_back(in); 30 reserved += n; 31 } 32 else 33 { 34 cout << "对不起,只剩" << capacity - reserved << "个位置.\n"; 35 cout << "1.输入u,更新(updata)预定信息" << endl; 36 cout << "2.输入q,退出预定" << endl; 37 cout << "你的选择:"; 38 39 char c; 40 cin >> c; 41 if(c == 'q') break; 42 else if(c == 'u') continue; 43 else break; 44 } 45 if (reserved == capacity) break; 46 } 47 cout << endl; 48 49 cout << "截至目前,一共有" << reserved << "位听众预定参加."; 50 if(reserved != 0) 51 { 52 cout << "预定听众信息如下:" << endl; 53 for (auto i = 0; i < v.size(); i++) 54 { 55 v[i].print(); 56 cout << endl; 57 } 58 } 59 }
运行测试结果