实验三
实验任务5
.hpp
#pragma once #include<iostream> #include<string> #include<iomanip> using std::string; using std::cout; using std::endl; class Info { public: Info(string nickname0,string contact0,string city0,int n0); void print() const; ~Info() = default; private: string nickname; string contact; string city; int n; }; Info::Info(string nickname0, string contact0, string city0, int n0) { nickname = nickname0; contact = contact0; city = city0; n = n0; } void Info::print() const { cout << "昵称: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预定人数: " << n << endl << endl; }
.cpp
#include"Info.hpp" #include<string> #include<iostream> #include<vector> #include<iomanip> using namespace std; int main() { const int capac = 100; vector<Info> audience_info_list; cout << "录入信息:" << endl; cout << "昵称\t" << "联系方式(邮箱/手机号)\t" << "所在城市\t" << "预定参加人数\t" << endl; string yourname, yourcontact, yourcity; int yn = 0; char choice = 'u'; int sum = 0; while (choice == 'u' && sum < capac && cin >> yourname) { cin >> yourcontact >> yourcity >> yn; Info a(yourname, yourcontact, yourcity, yn); audience_info_list.push_back(a); sum += yn; if (sum > capac) { sum -= yn; audience_info_list.pop_back(); cout << "对不起,只剩" << capac - sum << "个位置。" << endl; cout << "1.输入u,更新(update)预定信息" << endl << "2.输入q,退出预定" << endl << "你的选择:"; cin >> choice; } } cout << endl << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl; for (auto &Info:audience_info_list) Info.print(); }
实验任务6
.hpp
#pragma once #include<iostream> using namespace std; class Textcoder { public: Textcoder(string _text); string get_citext(); string get_detext(); private: string text; void encoder(); void decoder(); }; Textcoder::Textcoder(string _text) :text{ _text }{} string Textcoder::get_citext() { encoder(); return text; } string Textcoder::get_detext() { decoder(); return text; } void Textcoder::encoder() { for (int i = 0; i < text.size(); i++) { if (text[i] >= 'a' && text[i] <= 'u') { text[i] = text[i] + 5; } else if (text[i] >= 'v' && text[i] <= 'z') { text[i] = text[i] - 21; } else if (text[i] >= 'A' && text[i] <= 'U') { text[i] = text[i] + 5; } else if (text[i] >= 'V' && text[i] <= 'Z') { text[i] = text[i] - 21; } } } void Textcoder::decoder() { for (int i = 0; i < text.size(); i++) { if (text[i] >= 'f' && text[i] <= 'z') { text[i] = text[i] - 5; } else if (text[i] >= 'a' && text[i] <= 'e') { text[i] = text[i] + 21; } else if (text[i] >= 'F' && text[i] <= 'Z') { text[i] = text[i] - 5; } else if (text[i] >= 'A' && text[i] <= 'E') { text[i] = text[i] + 21; } } }
.cpp
#include "Textcoder.hpp" #include <iostream> #include <string> using namespace std; void test() { 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 << "输入英文文本: "; } } int main() { test(); }