task5
Info.h
1 #pragma once 2 #include <iostream> 3 #include <vector> 4 #include <string> 5 using namespace std; 6 7 class info { 8 public: 9 info(string name, string contact, string city, int total) :nickname{ name }, contact{ contact }, city{ city }, n{ total }{} 10 void print(); 11 12 private: 13 string nickname; 14 string contact; 15 string city; 16 int n; 17 18 }; 19 20 void info::print() { 21 cout << "昵称:\t \t" << nickname << "\n" << "联系方式:\t" << contact << "\n" << "所在城市:\t" << city << "\n" << "预定人数:\t" << n << endl; 22 }
task5.cpp
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include "Info.h" 5 6 void test() { 7 using namespace std; 8 9 const int capacity = 100; 10 string name, contast, city; 11 int n, count = 0; 12 vector<info> audience_info_list; 13 14 cout << "录入信息:" << endl; 15 cout << "昵称\t" << "联系方式(邮箱/手机号)\t\t" << "所在城市\t" << "预定参加人数\t" << endl; 16 17 while (cin >> name) { 18 cin >> contast >> city >> n; 19 count += n; 20 21 if (capacity - count >= 0) { 22 info p0 = info(name, contast, city, n); 23 audience_info_list.push_back(p0); 24 25 } 26 else { 27 string next; 28 count -= n; 29 cout << "对不起,只剩" << (capacity - count) << "个位置." << endl; 30 cout << "1.输入u,更新(update)预定信息" << endl; 31 cout << "2.输入q,退出预定" << endl; 32 cout << "你的选择:"; 33 cin >> next; 34 if (next == "q") 35 break; 36 else if (next == "u") { 37 cout << "录入信息:" << endl; 38 continue; 39 } 40 } 41 42 if (count == capacity) 43 break; 44 45 } 46 47 cout << "截至目前,一共有" << count << "位听众参加预定。预定信息如下:" << endl; 48 49 for (int i = 0; i < audience_info_list.size(); i++) { 50 audience_info_list.at(i).print(); 51 cout << endl; 52 } 53 54 } 55 56 int main() { 57 test(); 58 }
实验截图
task6
textcoder.hpp
1 #pragma once 2 #include<iostream> 3 #include <string> 4 using namespace std; 5 6 class TextCoder { 7 private: 8 string text; 9 void encoder(); 10 void decoder(); 11 public: 12 TextCoder(); 13 TextCoder(string t0) :text{t0}{} 14 string get_ciphertext(); 15 string get_deciphertext(); 16 }; 17 18 TextCoder::TextCoder(){} 19 20 void TextCoder::encoder(){ 21 22 int len = text.length(); 23 for (int i = 0; i < len; i++) { 24 if (text.at(i) >= 'a' && text.at(i) <= 'u') { 25 text.at(i) += 5; 26 } 27 else if (text.at(i) >= 'v' && text.at(i) <= 'z') { 28 text.at(i) -= 21; 29 } 30 else if (text.at(i) >= 'A' && text.at(i) <= 'U') { 31 text.at(i) += 5; 32 } 33 else if (text.at(i) >= 'V' && text.at(i) <= 'Z') { 34 text.at(i) -= 21; 35 } 36 } 37 38 } 39 40 void TextCoder::decoder() { 41 int len = text.length(); 42 for (int i = 0; i < len; i++) { 43 if (text.at(i) >= 'f' && text.at(i) <= 'z') { 44 text.at(i) -= 5; 45 } 46 else if (text.at(i) >= 'a' && text.at(i) <= 'e') { 47 text.at(i) += 21; 48 } 49 else if (text.at(i) >= 'F' && text.at(i) <= 'Z') { 50 text.at(i) -= 5; 51 } 52 else if (text.at(i) >= 'A' && text.at(i) <= 'E') { 53 text.at(i) += 21; 54 } 55 } 56 } 57 58 string TextCoder::get_ciphertext() { 59 encoder(); 60 return text; 61 } 62 63 string TextCoder::get_deciphertext() { 64 decoder(); 65 return text; 66 }
task6.cpp
1 #include<iostream> 2 #include "textcoder.hpp" 3 #include <string> 4 void test() { 5 using namespace std; 6 string text, encoded_text, decoded_text; 7 cout << "输入英文文本: "; 8 while (getline(cin, text)) { 9 encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时名对象 10 cout << "加密后英文文本:\t" << encoded_text << endl; 11 decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象 12 cout << "解密后英文文本:\t" << decoded_text << endl; 13 cout << "\n输入英文文本: "; 14 } 15 } 16 int main() { 17 test(); 18 }
实验截图
标签:info,city,string,text,实验,&&,include From: https://www.cnblogs.com/lee404error/p/16823421.html