任务五源码:
#ifndef INFO_HPP #define INFO_HPP #include<iostream> #include<iomanip> #include <string> using namespace std; class Info { public: Info(string n, string co, string c, string nn); void print(); static int s; string nickname, contact, city, n; }; int Info::s = 0; Info::Info(string na, string co, string c, string nn) :nickname{ na }, contact{ co }, city{ c }, n{ nn }{s += stoi(nn); }; void Info::print() { cout << setw(13) <<left << nickname << setw(20) << contact << setw(13) << city << setw(13) << n <<endl; } #endif
#include"info.hpp" #include <iostream> #include<vector> #include<limits> #define MAX 100 int main() { vector<Info> audience_info_list; const int capacity = MAX; string s1, s2, s3, s4; std::cout << "please input nickname、contact、city and num:"; while (1) { if (Info::s > capacity) { std::cout << "录入信息:" << endl << "称呼/昵称 联系方式(邮箱/手机号) 所在城市 预定参加人数" << endl; for (auto i = audience_info_list.begin(); i != audience_info_list.end(); i++) i->print(); cout << endl << endl; std::cout << "对不起,只剩 " << capacity - (Info::s-stoi(s4) )<< "个位置"<<endl << "1、输入u更新预订信息"<<endl <<"2、输入q退出预订"<<endl <<"你的选择:"; char c = getchar(); auto it = audience_info_list.end() - 1; if (c == 'q') { it=audience_info_list.erase(it); Info::s -= stoi(s4); break; } else if (c == 'u') { it= audience_info_list.erase(it); Info::s -= stoi(s4); cout << "please input nickname、contact、city and num:"; c = getchar(); getline(cin, s1, ' '); } } else if (!getline(cin, s1, ' ')|| Info::s == capacity) { cout << "录入信息:" << endl << "称呼/昵称 联系方式(邮箱/手机号) 所在城市 预定参加人数" << endl; for (auto i = audience_info_list.begin(); i != audience_info_list.end(); i++) i->print(); cout <<endl << endl; break; } getline(cin, s2, ' '); getline(cin, s3, ' '); getline(cin, s4); Info i(s1, s2, s3, s4); audience_info_list.push_back(i); } cout << "截至目前,一共有" << Info::s << "位听众预定参加,听众信息如下:" << endl; for (auto i = audience_info_list.begin(); i != audience_info_list.end(); i++) cout << setw(15)<< "称呼:" <<"\t"<< i->nickname << endl << "联系方式:" << "\t"<< i->contact << endl << "所在城市:" << "\t" << i->city << endl << "预定人数:" << "\t" << i->n << endl; return 0; }
任务六源码:
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include <iostream> #include <string> using namespace std; class TextCoder { public: TextCoder(string s = " ") :text{ s } {}; string encoder(); string decoder(); private: string text; }; string TextCoder::encoder() { for (auto& i : text) if (i >= 'A' && i <= 'U') i = i + 5; else if (i > 'U' && i <= 'Z') i = (i + 5 )%'Z' + 'A'-1; else if (i >= 'a' && i <= 'u') i = i + 5; else if (i > 'u' && i <= 'z') i =( i + 5 )% 'z' + 'a'-1; else; return text; }; string TextCoder::decoder() { for (auto& i : text) if (i >= 'A' && i <= 'E') i = i - 5 + 26; else if (i > 'E' && i <= 'Z') i = i - 5; else if (i >= 'a' && i <= 'e') i = i - 5+26; else if (i > 'e' && i <= 'z') i = i - 5; else; return text; } #endif
#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输入英文文本: "; } }