Test 5
主函数task5.cpp
#include<iostream> #include<string> #include<vector> #include"info.hpp" using namespace std; const int capacity = 100;//最大容纳人数为100 int main() { vector<info>audience_info_list{}; int shengyu = capacity;//剩余人数 cout << "录入信息:" << endl << endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预订参与人数" << endl; string a, b, c; int d; while (cin >> a >> b >> c >> d) { int t = 1;//控制是否跳出 info test(a, b, c, d); if (shengyu >= d) { audience_info_list.push_back(test); shengyu -= d; } else { cout << "对不起,只剩" << shengyu << "个位置." << endl; cout << "1.输入u,更新(update)预订信息" << endl; cout << "2.输入q,退出预订" << endl; cout << "你的选择:"; string e; cin >> e; if (e == "u") { continue; } else if (e == "q") { t = 0; } } if (t == 0) { break; } } cout << endl << "截至目前,一共有" << capacity - shengyu << "位听众预订参加。预订听众信息如下:" << endl; for (auto i : audience_info_list) { i.print(); } }
头文件info.hpp
#ifndef info_HPP #define info_HPP using namespace std; #include<iostream> #include<string> class info//info信息类 { public: info(const string& a, const string& b, const string& c, int d) :nickname(a), contact(b), city(c), n(d) {} //构造函数,对信息初始化 void print() { cout << "称呼: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预订人数: " << n << endl; }; private: string nickname;//昵称 string contact;//联系方式 string city;//所在城市 int n;//预订参与人数 }; #endif
以上为代码运行结果---
Test 6
主函数task6.cpp
#include "textcoder.hpp" #include <iostream> #include <string> int main() { using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本: "; while (getline(cin, text)) { encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象 cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象 cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } }
头文件textcoder.hpp
#ifndef textcoder_HPP #define textcoder_HPP using namespace std; #include <iostream> #include <string> class TextCoder { private: string text; public: TextCoder(string a = "") :text(a) {} string encoder(); string decoder(); }; string TextCoder::encoder() { string a; for (auto i : text) { if (i >= 'a' && i <= 'z') { a.push_back('a' + (i + 5 - 'a') % 26); } else if (i >= 'A' && i <= 'Z') { a.push_back('A' + (i + 5 - 'A') % 26); } else { a.push_back(i); } } return a; } string TextCoder::decoder() { string a; for (auto i : text) { if (i >= 'a' && i <= 'z') { a.push_back('z' - ('z' - i + 5) % 26); } else if (i >= 'A' && i <= 'Z') { a.push_back('Z' - ('Z' - i + 5) % 26); } else { a.push_back(i); } } return a; } #endif
以上为代码运行结果