实验3
task5.cpp
#include<iostream> #include<string> #include<vector> #include"info.hpp" using namespace std; int main() { const int capacity = 100; vector<info> audience_info_list; string name, cont, city; int rn,count=0; string s = "昵称 联系方式(邮箱/手机号) 所在城市 预定参加人数"; cout << "录入信息:" << endl << endl; cout << s << endl; while (cin>>name) { cin >> cont >> city >> rn; info inf(name,cont,city,rn); audience_info_list.push_back(inf); count += inf.get_n(); if (count > capacity) { audience_info_list.pop_back(); count -= inf.get_n(); cout << "对不起,只剩" << capacity-count << "个位置" << endl; cout << "1.输入u,更新(update)预定信息"<<endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; string choice; cin >> choice; if (choice == "u") continue; else break; } } cout << "截至目前,一共有" << count << "位听众预定参加。预定听众信息如下:" << endl; for (auto i = 0; i < audience_info_list.size(); i++) { audience_info_list[i].print(); cout << endl; } }
info.hpp
#pragma once #include<iostream> #include<string> using namespace std; class info { public: info(string name, string cont, string ct, int rn) :nickname(name), contact(cont), city(ct), n(rn) {} void print()const ; int get_n() { return n; } private: string nickname, contact, city; int n; }; void info::print()const { cout << "昵称: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预定人数: " << n << endl; }


task6.cpp
#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(); }
textcoder.hpp
#pragma once #include<iostream> using namespace std; class TextCoder { public: TextCoder(string t):text(t){} string get_ciphertext(); string get_deciphertext(); private: string text; void encoder(); void decoder(); }; string TextCoder:: get_ciphertext() { encoder(); return text; } string TextCoder:: get_deciphertext() { decoder(); return text; } void TextCoder::encoder() { for (auto i = 0; i < text.length(); i++) { if (text[i] <= 'z' && text[i] >= 'a' || text[i] <= 'Z' && text[i] >= 'A') { text[i] += 5; if (!text[i] <= 'z' && !text[i] >= 'a' || !text[i] <= 'Z' && !text[i] >= 'A') text[i] -= 26; } } } void TextCoder::decoder() { for (auto i = 0; i < text.length(); i++) { if (text[i] <= 'z' && text[i] >= 'a' || text[i] <= 'Z' && text[i] >= 'A') { text[i] -= 5; if (!text[i] <= 'z' && !text[i] >= 'a' || !text[i] <= 'Z' && !text[i] >= 'A') text[i] += 26; } } }


浙公网安备 33010602011771号