# ifndef INFO_H # define INFO_H # include <iostream> # include <string> using namespace std; class info { public: info(string a = " ", string b = " ", string c = " ", int d = 0) : nickname(a), contact(b), city(c), n(d) {} void print()const { cout << "称呼: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预订人数: " << n << endl; } void print() { cout << "称呼: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预订人数: " << n << endl; } private: string nickname; string contact; string city; int n; }; # endif
# include "info.hpp" # include <iostream> # include <vector> int main() { using namespace std; vector<info>audience_info_list; const int capacity = 100; string a, b, c; int d; //i = 0; int k = capacity; cout << "录入信息:" << endl << endl; cout << "称呼/昵称,联系方式(邮箱/手机号码),所在城市,预定参加人数" << endl; while (cin >> a >> b >> c >> d) { int y = k; y -= d; string x; if (y <0) { cout << "对不起,只剩下" << capacity << "个位置." << endl; cout << "1. 输入u,更新(update)预订信息" << endl; cout << "2. 输入q,退出预订" << endl; cin >> x; } else { audience_info_list.push_back(info(a, b, c, d)); k -= d; continue; } if (x == "q") { break; } else { continue; } } int n; n = 100 - k; cout << "截至目前,一共有" << n << "位听众参加,预订听众信息如下:" << endl; for (auto const& a : audience_info_list) a.print(); cout << endl; }
#ifndef TEXTCODER_H #define TEXTCODER_H # include <iostream> # include <string> # include <vector> using namespace std; class TextCoder { public: TextCoder(string s="******"): text(s){} string encoder(); string decoder(); private: string text; }; string TextCoder::encoder() { for (auto &a:text) { if (a >= 'a' && a <= 'z') { if (a >= 'a' && a <= 'u') a += 5; else a -= 21; } else if (a >= 'A' && a <= 'Z') { if (a >= 'A' && a <= 'U') a += 5; else a -= 21; } } return text; } string TextCoder::decoder() { for (auto& b:text) { if (b >= 'a' && b <= 'z') { if (b >= 'a' && b <= 'e') b += 21; else b -= 5; } else if (b >= 'A' && b <= 'Z') { if (b >= 'A' && b <= 'E') b += 21; else b -= 5; } } return text; } #endif
#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输入英文文本: "; } }