实验三
实验任务5:
task5.hpp:
#pragma once #include <iostream> #include <vector> #include <string> using namespace std; class Info{ public: Info(string name, string con, string city, int people) :nickname{ name }, contact{ con }, city{ city }, n{ people }{} void print(); private: string nickname; string contact; string city; int n; }; void Info::print() { cout << "昵称:\t \t"<< nickname << endl; cout << "联系方式:\t" << contact << endl; cout << "所在城市:\t" << city << endl; cout << "预定人数:\t" << n << endl; }
task5.cpp:
#include <iostream> #include <vector> #include <string> #include "Info.h" void test() { using namespace std; const int capacity = 100; string name, con, city; int n,count= 0; vector<Info> audience_info_list; cout << "录入信息:" << endl; cout << "昵称\t" << "联系方式(邮箱/手机号)\t\t" << "所在城市\t" << "预定参加人数\t" << endl; while (cin>>name) { cin >> con >> city >> n; count += n; if (capacity - count >= 0) { Info p0 = Info(name, con, city, n); audience_info_list.push_back(p0); } else{ string next; count -= n; cout << "对不起,只剩" << (capacity - count) << "个位置." << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; cin >> next; if (next == "q") break; else if (next == "u") { cout << "录入信息:" << endl; continue; } } if (count == capacity) break; } cout << "截至目前,一共有" << count << "位听众参加预定。预定信息如下:" << endl; for (int i = 0; i < audience_info_list.size(); i++) { audience_info_list.at(i).print(); cout << endl; } } int main() { test(); }
运行测试截图:
修改测试数据后 测试结果:
实验任务6:
task6.hpp:
#pragma once #include<iostream> #include <string> using namespace std; class TextCoder { private: string text; void encoder(); void decoder(); public: TextCoder(); TextCoder(string t0) :text{t0}{} string get_ciphertext(); string get_deciphertext(); }; TextCoder::TextCoder(){} void TextCoder::encoder(){ int len = text.length(); for (int i = 0; i < len; i++) { if (text.at(i) >= 'a' && text.at(i) <= 'u') { text.at(i) += 5; } else if (text.at(i) >= 'v' && text.at(i) <= 'z') { text.at(i) -= 21; } else if (text.at(i) >= 'A' && text.at(i) <= 'U') { text.at(i) += 5; } else if (text.at(i) >= 'V' && text.at(i) <= 'Z') { text.at(i) -= 21; } } } void TextCoder::decoder() { int len = text.length(); for (int i = 0; i < len; i++) { if (text.at(i) >= 'f' && text.at(i) <= 'z') { text.at(i) -= 5; } else if (text.at(i) >= 'a' && text.at(i) <= 'e') { text.at(i) += 21; } else if (text.at(i) >= 'F' && text.at(i) <= 'Z') { text.at(i) -= 5; } else if (text.at(i) >= 'A' && text.at(i) <= 'E') { text.at(i) += 21; } } } string TextCoder::get_ciphertext() { encoder(); return text; } string TextCoder::get_deciphertext() { decoder(); return text; }
task6.cpp:
#include<iostream> #include "TextCoder.h" #include <string> void test() { using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本: "; while (getline(cin, text)) { encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时名对象 cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象 cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } } int main() { test(); }
运行测试截图:
修改测试数据后 测试结果: