任务5
#include <iostream> #include <string> class TextCoder { private: std::string text; void encoder() { for (char &c : text) { if (isalpha(c)) { char base = isupper(c) ? 'A' : 'a'; c = base + ((c - base + 7) % 26); } } } void decoder() { for (char &c : text) { if (isalpha(c)) { char base = isupper(c) ? 'A' : 'a'; c = base + ((c - base - 7 + 26) % 26); } } } public: TextCoder(const std::string& inputtext): text(inputtext) {} std::string get_ciphertext() { encoder(); return text; } std::string get_deciphertext() { decoder(); return text; } };
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
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 using namespace std; 5 6 class info{ 7 private: 8 string nickname; 9 string contact; 10 string city; 11 int n; 12 public: 13 info(string n1,string c,string city1,int n2){ 14 nickname=n1; 15 contact=c; 16 city=city1; 17 n=n2; 18 19 } 20 void print(){ 21 cout<<"昵称:\t\t"<<nickname<<endl; 22 cout<<"联系方式:\t"<<contact<<endl; 23 cout<<"所在城市:\t"<<city<<endl; 24 cout<<"预定人数:\t"<<n<<endl; 25 } 26 };
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 }