C++程序设计实验二 数组、指针与C++标准库
Info.hpp文件源码
#include<iostream> #include<string> #include<iomanip> using namespace std; class Info { public: Info(){ } Info(string name, string number, string w, int num) : nickname{ name }, contact{ number }, city{ w }, n{ num } { } void print() const { cout << left << setw(10) << "称呼:" << nickname << endl; cout << setw(10) << "联系方式:" << contact << endl; cout << setw(10) << "所在城市:" << city << endl; cout << setw(10) << "预定人数:" << n << endl; } private: string nickname; string contact; string city; int n; };
task5.cpp源码
#include"Info.hpp" #include<iostream> #include<vector> #include<string> #include<limits> using namespace std; static int num = 0; int main() { vector<Info> audience_info_list; string n_nickname, n_contact, n_city; int n = 0; const int capacity = 100; cout << "录入信息:\n" << endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; while (cin >> n_nickname) { cin >> n_contact >> n_city >> n; if (n + num <= capacity) { Info audience(n_nickname, n_contact, n_city, n); audience_info_list.push_back(audience); num += n; } else { char input; cout << "对不起,只剩" << capacity - num << "个位置。" << endl; cout << "1.输出u,更新(update)预定信息" << endl; cout << "2.输出q,退出预定" << endl; cout << "你的选择: "; cin >> input; if (input == 'q') { break; } } } cout << "截至目前,一共有" << num << "位听众预定参加。预定听众信息如下:" << endl; for (auto item : audience_info_list) item.print(); }
运行测试结果截图
TextCoder.hpp文件源码
#include <iostream> #include <string> using namespace std; class TextCoder { public: TextCoder(string tex = NULL) : text{ tex } { } string encoder() { for (auto& item : text) { if (item >= 'a' && item <= 'z') item = 'a' + (item - 'a' + 5) % 26; else if (item >= 'A' && item <= 'Z') item = 'A' + (item - 'A' + 5) % 26; } return text; } string decoder() { for (auto& item : text) { if (item >= 'a' && item <= 'z') item = 'a' + (item - 'a' + (26 - 5)) % 26; else if (item >= 'A' && item <= 'Z') item = 'A' + (item - 'A' + (26 - 5)) % 26; } return text; } private: string text; };
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输入英文文本: "; } }
运行测试结果截图