task5源代码:
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class TextCoder { 9 public: 10 TextCoder() = default; 11 TextCoder(string str); 12 string get_ciphertext(); 13 string get_deciphertext(); 14 ~TextCoder() = default; 15 16 private: 17 string text; 18 void encoder(); 19 void decoder(); 20 }; 21 22 TextCoder::TextCoder(string str) { 23 text = str; 24 } 25 26 string TextCoder::get_ciphertext() { 27 encoder(); 28 return text; 29 } 30 31 string TextCoder::get_deciphertext() { 32 decoder(); 33 return text; 34 } 35 36 void TextCoder::encoder() { 37 for(int i = 0;i<text.length();i++) { 38 if(islower(text[i])) { 39 text.replace(i, 1, 1, (text[i] - 'a' + 7) % 26 + 'a'); 40 } 41 else if(isupper(text[i])) { 42 text.replace(i, 1, 1, (text[i] - 'A' + 7) % 26 + 'A'); 43 } 44 } 45 } 46 47 void TextCoder::decoder() { 48 for(int i = 0;i<text.length();i++) { 49 if(islower(text[i])) { 50 text.replace(i, 1, 1, (text[i] - 'a' + 26 - 7) % 26 + 'a'); 51 } 52 else if(isupper(text[i])) { 53 text.replace(i, 1, 1, (text[i] - 'A' + 26 - 7) % 26 + 'A'); 54 } 55 } 56 }TextCoder.hpp
1 #include<iostream> 2 #include<string> 3 #include"TextCoder.hpp" 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 decoded_text = TextCoder(encoded_text).get_deciphertext(); //这里使用的是临时无名对象 15 cout << "解密后英文文本:\t" << decoded_text << endl; 16 cout << "\n输入英文文本:"; 17 } 18 } 19 20 int main() { 21 test(); 22 }task5.cpp
task5运行结果:
task6源代码:
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class info { 9 public: 10 info(string n1, string c, string city1, int n2); 11 void print() { 12 cout << "昵称:\t\t" << nickname << endl; 13 cout << "联系方式:\t" << contact << endl; 14 cout << "所在城市:\t" << city << endl; 15 cout << "预定人数:\t" << n << endl; 16 }; 17 18 private: 19 string nickname; 20 string contact; 21 string city; 22 int n; 23 }; 24 25 info::info(string n1, string c, string city1, int n2): nickname{n1}, contact{c}, city{city1}, n{n2} {}info.hpp
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include"info.hpp" 5 6 using namespace std; 7 8 void test() { 9 const int type = 100; 10 vector<info> audience_info_list; 11 vector<info> &v = audience_info_list; 12 int sum = 0; 13 14 cout << "录入信息:\n\n"; 15 cout << "昵称\t\t"; 16 cout << "联系方式(邮箱/手机号)\t\t"; 17 cout << "所在城市\t"; 18 cout << "预定人数" << endl; 19 20 string nickname, contact, city; 21 int n; 22 23 while (cin >> nickname >> contact >> city >> n) { 24 if (sum + n <= type) { 25 info a(nickname, contact, city, n); 26 v.push_back(a); 27 sum += n; 28 } 29 else { 30 cout << "对不起,只剩" << type - sum << "个位置。\n"; 31 cout << "1.输入u,更新(update)预定信息" << endl; 32 cout << "2.输入q,退出预定" << endl; 33 cout << "你的选择:"; 34 35 char c; 36 cin >> c; 37 if (c == 'q') break; 38 else if (c == 'u') continue; 39 else break; 40 } 41 if (sum == type) break; 42 } 43 cout << endl; 44 45 cout << "截至目前,一共有" << sum << "位听众预定参加。"; 46 if (sum != 0) { 47 cout << "预定听众信息如下:" << endl; 48 for (int i = 0; i < v.size(); i++) { 49 v[i].print(); 50 cout << endl; 51 } 52 } 53 } 54 55 int main() { 56 test(); 57 }task6.cpp
task6运行结果:
标签:info,string,text,void,实验,include,TextCoder From: https://www.cnblogs.com/atry/p/17899585.html