任务5 源代码:
info.hpp
#ifndef INFO_HPP #define INFO_HPP #include<iostream> #include<string> #include<iomanip> using namespace std; class Info { public: Info(const string nickname0, const string contact0, const string city0, int n0); void print()const; private: string nickname; string contact; string city; int n; }; Info::Info(const string nickname0="", const string contact0 = "", const string city0 = "", int n0=0):nickname{nickname0},contact{contact0},city{city0},n{n0}{} void Info::print()const { cout.width(10); cout << setiosflags(ios::left) << "称呼:"; cout << this->nickname << endl; cout.width(10); cout << setiosflags(ios::left) << "联系方式:"; cout << this->contact << endl; cout.width(10); cout << setiosflags(ios::left) << "所在城市:"; cout << this->city<< endl; cout.width(10); cout << setiosflags(ios::left) << "预定人数:"; cout << this->n << endl; } #endif
task5.cpp
#include"Info.hpp" #include<iostream> #include<vector> #include<string> int main() { using namespace std; vector<Info>audience_info_list; const int capacity = 100; string nickname, contact, city; int n,total = 0; char choice; cout << "录入信息:" << endl<<endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; while (cin >> nickname) { cin >> contact; cin >> city; cin >> n; if (n + total <= capacity) { Info audience_information(nickname, contact, city, n); audience_info_list.push_back(audience_information); total += n; } else { cout << "对不起,只剩" << capacity - total << "个位置." << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; cin >> choice; if (choice == 'u') { cout << "请重新输入预定参加人数:" << endl; cin >> n; Info audience_information(nickname, contact, city, n); audience_info_list.push_back(audience_information); total += n; cout << "继续录入信息:" << endl << endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; } else break; } } cout << "截至目前,一共有" << total << "位听众预定参加。预定听众信息如下:"<<endl; for (auto Info : audience_info_list) { Info.print(); cout << endl; } return 0; }
任务6 源代码:
textcoder.hpp
#ifndef TEXTCORDER_HPP #define TEXTCORDER_HPP #include<iostream> #include<string> using namespace std; class TextCoder { public: TextCoder(string text0 = "") : text{ text0 } { } ~TextCoder() = default; string encoder(); string decoder(); private: string text; }; string TextCoder::encoder() { string res; for (int i = 0; i < this->text.size(); i++) { if (text[i] >= 'a' && text[i] <= 'u') res.push_back(text[i] += 5); else if (text[i] >= 'v' && text[i] <= 'z') res.push_back(text[i] -= 21); else if (text[i] >= 'A' && text[i] <= 'U') res.push_back(text[i] += 5); else if (text[i] >= 'V' && text[i] <= 'Z') res.push_back(text[i] -= 21); } return res; } string TextCoder::decoder() { string res; for (int i = 0; i < this->text.size(); i++) { if (text[i] >= 'f' && text[i] <= 'z') res.push_back(text[i] -= 5); else if (text[i] >= 'a' && text[i] <= 'e') res.push_back(text[i] += 21); else if (text[i] >= 'F' && text[i] <= 'Z') res.push_back(text[i] -= 5); else if (text[i] >= 'A' && text[i] <= 'E') res.push_back(text[i] += 21); } return res; } #endif
task6.cpp
#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输入英文文本: "; } }