task5
//Info.hpp
#include<iostream> #include<math.h> #include<string> using namespace std; class Info{ public: Info (string a, string b, string c, int nn) : nickname{a}, contact{b}, city{c}, n{nn} { cnt += nn; } void print(){ cout << "昵称: " << nickname << endl << "联系方式: " << contact << endl << "所在城市: " << city << endl << "预定人数: " << n << endl << endl; } static int getcnt() { return cnt; } static const int capacity; private: string nickname, contact, city; int n; static int cnt; }; int Info::cnt = 0; const int Info::capacity = 100;
//task5.cpp
#include<iostream> #include<vector> #include<iomanip> #include"Info.hpp" using namespace std; vector<Info> livehouse; int main(){ cout << "录入信息:" << endl; cout << setw(15) << left << "昵称"; cout << setw(15) << left << "联系方式(邮箱/手机号)"; cout << setw(15) << left << "所在城市"; cout << setw(15) << left << "预定参加人数"; cout << endl; string nickname, contact, city; int n; while(cin >> nickname){ cin >> contact >> city >> n; bool in = true; while(n + Info::getcnt() > Info::capacity) { cout << "对不起,只剩" << Info::capacity - Info::getcnt() << "个位置" << endl; cout << "1.输入u,更新(updata)预订信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; char op; cin >> op; if(op == 'q'){ in = false; break; } else{ cout << "更新预定人数:" ; cin >> n; } } if(in){ livehouse.push_back(Info(nickname, contact, city, n)); cout << "预定成功 ~ " << endl; } } cout << "截止目前,一共有" << Info::getcnt() << "位听众预定参加。预定听众信息如下:" << endl; for(auto &now : livehouse) now.print(); return 0; }
测试结果1:
测试结果2:
更换测试数据的结果:
task6
//TextCoder.hpp
#include<bits/stdc++.h> using namespace std; class TextCoder{ public: TextCoder(string a = "") : text{a} {} string get_ciphertext(){ encoder(); return text; } string get_deciphertext(){ decoder(); return text; } private: string text; void encoder(int w = 5){ for(auto &ch : text){ char Aa; if('a' <= ch && ch <= 'z') Aa = 'a'; else if('A' <= ch && ch <= 'Z') Aa = 'A'; else continue; ch = Aa + (ch - Aa + w + 52) % 26; } } void decoder(){ encoder(-5); } };
//task6.cpp
#include<iostream> #include<string> #include"TextCoder.hpp" void test(){ using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本:"; while(getline(cin, text)){ encoded_text = TextCoder(text).get_ciphertext(); cout << "加密后的英文文本:" << encoded_text << endl; decoded_text = TextCoder(encoded_text).get_deciphertext(); cout << "解密后的英文文本:" << decoded_text << endl << endl; cout << "输入英文文本:"; } } int main(){ test(); }
测试结果:
跟换测试数据的结果:
标签:Info,string,text,hpp,cout,C++,数组,include,指针 From: https://www.cnblogs.com/lyhy/p/16808084.html