实验任务5: Info.hpp文件源码:
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 class info { 6 private: 7 string nickname; 8 string contact; 9 string city; 10 int n; 11 public: 12 info(string na = "zero", string co = "0", string ci = "Nanjing", int nu = 0) :nickname(na), contact(co), city(ci), n(nu) {} 13 void print() ; 14 }; 15 void info::print() { 16 cout << "昵称: " << nickname << endl 17 << "联系方式: " << contact << endl 18 << "所在城市: " << city << endl 19 << "预订人数: " << n << endl; 20 };task5.cpp源码:
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include"info.hpp" 5 int main() 6 { 7 int num, sum = 0; 8 string nam, con, cit; 9 const int capacity = 100; 10 vector<info>audience_info_list; 11 cout << "录入信息:" << endl << endl 12 << "昵称 " << "联系方式(邮箱/手机号码) " << "所在城市 " << "预定参加人数" << endl; 13 while (cin >> nam) { 14 cin >> con >> cit >> num; 15 sum += num; 16 if (capacity - sum >= 0) { 17 info a0 = info(nam, con, cit, num); 18 audience_info_list.push_back(a0); 19 } 20 else { 21 sum -= num; 22 cout << "对不起,只剩" << (capacity - sum) << "个位置。" << endl 23 << "1.输入u,更新(update)预订信息" << endl 24 << "2.输入q,退出预定" << endl 25 << "你的选择:"; 26 string choice; 27 cin >> choice; 28 if (choice == "q") break; 29 if (choice == "u") { 30 cout << "录入信息:" << endl << endl 31 << "昵称 " << "联系方式(邮箱/手机号码) " << "所在城市 " << "预定参加人数" << endl; 32 continue; 33 } 34 } 35 } 36 cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl; 37 for (int i = 0; i < audience_info_list.size(); i++) { 38 audience_info_list[i].print(); 39 cout << endl; 40 } 41 }运行测试结果截图:
更换数据后运行截图:
实验任务6:
TextCoder.hpp文件源码:1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class TextCoder { 5 private: 6 string text; 7 void encoder(); 8 void decoder(); 9 public: 10 TextCoder(string t) :text(t) {} 11 TextCoder(TextCoder& t) :text(t.text) {} 12 string get_ciphertext(); 13 string get_deciphertext(); 14 }; 15 void TextCoder::encoder() { 16 for (int i = 0; i < text.size(); i++) { 17 if (text.at(i) >= 'a' && text.at(i) <= 'z') { 18 text.at(i) = (text.at(i) + 5 - 97) % 26 + 97; 19 } 20 if (text.at(i) >= 'A' && text.at(i) <= 'Z') { 21 text.at(i) = (text.at(i) + 5 - 65) % 26 + 65; 22 } 23 } 24 } 25 void TextCoder::decoder() { 26 for (int i = 0; i < text.size(); i++) { 27 if (text.at(i) >= 'a' && text.at(i) <= 'z') { 28 text.at(i) = (text.at(i) - 5 - 97 + 26) % 26 + 97; 29 } 30 if (text.at(i) >= 'A' && text.at(i) <= 'Z') { 31 text.at(i) = (text.at(i) - 5 - 65 + 26) % 26 + 65; 32 } 33 } 34 } 35 string TextCoder::get_ciphertext() { 36 encoder(); 37 return text; 38 } 39 string TextCoder::get_deciphertext() { 40 decoder(); 41 return text; 42 }task6.cpp源码:
1 #include "textcoder.hpp" 2 #include <iostream> 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,string,text,void,实验,include,TextCoder From: https://www.cnblogs.com/dyb20030328/p/16807375.html