实验三
#pragma once #include <iostream> #include<vector> #include <string> #include<iomanip> using namespace std; class Info { public: Info() {} Info(string a, string b, string c, int d) :nickname{ a }, contact{ b }, city{ c }, n{ d }{}; void print() { cout <<left<< setw(20) << "昵称:" << nickname << endl; cout <<left<< setw(20) << "联系方式:" << contact << endl; cout <<left<< setw(20) << "所在城市:" << city << endl; cout <<left<< setw(20) << "预定人数" << n << endl; } private: string nickname, contact, city; int n; };
#include<Info.hpp> #include <iostream> #include<vector> #include <string> #include<iomanip> using namespace std; vector<Info> audience_info_list; int main() { cout << "录入信息:" << endl; cout << "昵称 联系方式(邮箱/手机号) 所在城市 预定参加人数" << endl; const int capacity = 100; int sum = 0; string s1, s2, s3; int num; int count = 0; while (cin >> s1 >> s2 >> s3 >> num) { sum += num; if (sum > capacity) { sum -= num; cout << "对不起,只剩" << capacity - sum << "个位置." << endl; cout << "1.输入u,更新(update)预定信息" << endl << "2.输入q, 退出预定" << endl << "你的选择: "; string f; cin >> f; if (f == "u") { cin >> s1 >> s2 >> s3 >> num; Info f(s1, s2, s3, num); audience_info_list.push_back(f); sum += num; } else break; } else audience_info_list.push_back(Info(s1, s2, s3, num)); } cout << endl << "截至目前,一共有" << sum << "位听众预定参加。信息如下:" << endl;; for (int i = 0; i < audience_info_list.size(); i++) { audience_info_list.at(i).print(); cout << endl; } return 0; }
#pragma once #include <iostream> #include<string> using namespace std; class TextCoder { public: TextCoder() {} TextCoder(string text0) :text{ text0 } {} string get_ciphertext() { encoder(); return text; } string get_deciphertext() { decoder(); return text; } private: string text; void encoder(); void decoder(); }; void TextCoder::encoder() { int i; for (i = 0; i < text.length(); i++) { if ((text[i] >= 86 && text[i] <= 90) || (text[i] >= 118 && text[i] <= 122)) { text[i] = text[i] - 21; } if ((text[i] < 86 && text[i] >= 65) || (text[i] >= 97 && text[i] < 118)) { text[i] = text[i] + 5; } } } void TextCoder::decoder() { int i; for (i = 0; i < text.length(); i++) { if ((text[i] >= 65 && text[i] <= 69) || (text[i] >= 97 && text[i] <= 101)) { text[i] = text[i] + 21; } if ((text[i] > 69 && text[i] <= 90) || (text[i] > 101 && text[i] <= 122)) { text[i] = text[i] - 5; } } }
#include"textcorder.hpp" #include<iostream> #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(); }