实验4
任务5
Textcoder
#include<iostream> #include<string> using namespace std; class Textcoder { public: Textcoder(string t); string get_ciphertext(); string get_deciphertext(); private: string text; void encoder(); void decoder(); }; Textcoder::Textcoder(string t) { text = t; } string Textcoder::get_ciphertext() { encoder(); return text; } string Textcoder::get_deciphertext() { decoder(); return text; } void Textcoder::encoder() { for (auto& t : text) { if (t >= 'a' && t <= 'z') { t = (t - 'a' + 7) % 26 + 'a'; } if (t >= 'A' && t <= 'Z') { t = (t - 'A' + 7) % 26 + 'A'; } } } void Textcoder::decoder() { for (auto& m : text) { if (m >= 'a' && m <= 'z') { m = 'z' - ('z' - m + 7) % 26; } if (m >= 'A' && m <= 'Z') { m = 'Z'- ('Z' - m + 7) % 26; } } }
test
#include "Textcoder.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(); }

任务6
test
#include <iostream> #include <string> #include <vector> #include"info.hpp" using namespace std; int main(){ const int capacity=100; vector<info> audience_info_list; string nickname, contact, city; int n; vector<info> &i = audience_info_list; int sum=0; cout << "录入信息:\n\n"; cout << "昵称\t\t"; cout << "联系方式(邮箱/手机号)\t\t"; cout << "所在城市\t"; cout << "预定人数" << endl; while(cin>>nickname >> contact >> city >> n){ if(sum+n<=capacity){ info a(nickname,contact,city,n); i.push_back(a); sum +=n; } else{cout << "对不起,只剩" << capacity - sum << "个位置。\n"; cout << "1.输入u,更新(updata)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; char c; cin >> c; if (c == 'q') break; else if (c == 'u') continue; else break; } if (sum == capacity) break; } cout << endl; cout << "截至目前,一共有" << sum << "位听众预定参加。"; if (sum != 0) { cout << "预定听众信息如下:" << endl; for (int k= 0; k< i.size(); k++) { i[k].print(); cout << endl; } } }
info
#include<iostream> #include<iostream> #include<string> #include<vector> using namespace std; class info { private: string nickname; string contact; string city; int n; public: info(string n1, string c, string city1, int n2) { nickname = n1; contact = c; city = city1; n = n2; } void print() { cout << "昵称:\t\t" << nickname << endl; cout << "联系方式:\t" << contact << endl; cout << "所在城市:\t" << city << endl; cout << "预定人数:\t" << n << endl; } };
test
#include <iostream> #include <string> #include <vector> #include"info.hpp" using namespace std; int main() { const int capacity = 100; vector<info> audience_info_list; string nickname, contact, city; int n; vector<info>& i = audience_info_list; int sum = 0; cout << "录入信息:\n\n"; cout << "昵称\t\t"; cout << "联系方式(邮箱/手机号)\t\t"; cout << "所在城市\t"; cout << "预定人数" << endl; while (cin >> nickname >> contact >> city >> n) { if (sum + n <= capacity) { info a(nickname, contact, city, n); i.push_back(a); sum += n; } else { cout << "对不起,只剩" << capacity - sum << "个位置。\n"; cout << "1.输入u,更新(updata)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; char c; cin >> c; if (c == 'q') break; else if (c == 'u') continue; else break; } if (sum == capacity) break; } cout << endl; cout << "截至目前,一共有" << sum << "位听众预定参加。"; if (sum != 0) { cout << "预定听众信息如下:" << endl; for (int k = 0; k < i.size(); k++) { i[k].print(); cout << endl; } } }

浙公网安备 33010602011771号