实验3 数组、指针与现代C++标准库
task.5
某独立音乐人要举办一场免费小型liveshow。livehouse场地容量有限,最多容纳100位乐迷听众。现通 过某平台开通线上预约登记
1 #pragma once 2 #include<bits/stdc++.h> 3 using namespace std; 4 class Info 5 { 6 public: 7 Info(string nickname0 = 0, string contact0 = 0, string city0 = 0, int n0 = 0) :nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ n0 }{}; 8 //Info(Info& Info0) :nickname{ Info0.nickname }, contact{ Info0.contact }, city{ Info0.city }, n{ Info0.n }{}; 9 void print()const; 10 private: 11 string nickname; 12 string contact; 13 string city; 14 int n; 15 }; 16 void Info::print()const { 17 cout << left << setw(16) << "昵称:" << nickname << endl; 18 cout << left << setw(16) << "联系方式:" << contact << endl; 19 cout << left << setw(16) << "所在城市:" << city << endl; 20 cout << left << setw(16) << "预定人数:" << n << endl; 21 cout << endl; 22 }
1 #include "Info.hpp" 2 #include<bits/stdc++.h> 3 using namespace std; 4 const int capacity = 100; 5 vector<Info> audience_info_list; 6 int update(int size,int remain) { 7 cout << "录入信息:" << endl; 8 cout << endl; 9 cout << left<<setw(16) << "昵称" << setw(36) << "联系方式(邮箱/手机号)" << setw(16) << "所在城市" << setw(16) << "预定参加人数" << endl; 10 string temp_nickname; 11 string temp_contact; 12 string temp_city; 13 int temp_n; 14 do{ 15 remain = capacity - size; 16 cin >> temp_nickname >> temp_contact >> temp_city >> temp_n; 17 if (temp_n == -1) 18 break; 19 size += temp_n; 20 Info temp(temp_nickname, temp_contact, temp_city, temp_n); 21 audience_info_list.push_back(temp); 22 } while (temp_n <= remain); 23 if (temp_n > remain) { 24 audience_info_list.pop_back(); 25 size -= temp_n; 26 char choice; 27 cout << "对不起,只剩" << remain << "个位置." << endl; 28 cout << "1.输入u,更新(update)预定信息" << endl; 29 cout << "2.输入q,退出预定" << endl; 30 cout << "你的选择:" << endl; 31 cin >> choice; 32 if (choice == 'u') 33 size=update(size,remain); 34 if (choice == 'q') 35 return size; 36 } 37 return size; 38 } 39 void print() { 40 for (int i = 0; i < audience_info_list.size(); i++) 41 audience_info_list[i].print(); 42 } 43 int main() { 44 audience_info_list.clear(); 45 int size=update(0,capacity); 46 cout << "截至目前,一共有" << size << "位听众预定参加。预定信息如下:" << endl; 47 print(); 48 }


task6.
设计并实现一个类TextCoder,用于对英文文本字符串进行简单的加密和解密操作
1 #pragma once 2 #include<bits/stdc++.h> 3 using namespace std; 4 class TextCoder 5 { 6 public: 7 TextCoder(string text0 = 0) :text{ text0 } {}; 8 TextCoder(TextCoder& TextCoder0) :text{ TextCoder0.text } {}; 9 string get_ciphertext(); 10 string get_deciphertext(); 11 private: 12 string text; 13 void encoder(); 14 void decoder(); 15 }; 16 void TextCoder::encoder() { 17 int len = text.size(); 18 for (int i = 0; i < len; i++) { 19 if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z')) { 20 text[i] += 5; 21 if (text[i] > 'z' || (text[i] > 'Z' && text[i] < 'a')) 22 text[i] -= 26; 23 } 24 } 25 } 26 void TextCoder::decoder() { 27 int len = text.size(); 28 for (int i = 0; i < len; i++) { 29 if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z')) { 30 text[i] -= 5; 31 if (text[i] < 'A' || (text[i] > 'Z' && text[i] < 'a')) 32 text[i] += 26; 33 } 34 } 35 } 36 string TextCoder::get_ciphertext() { 37 encoder(); 38 return text; 39 } 40 string TextCoder::get_deciphertext() { 41 decoder(); 42 return text; 43 }

浙公网安备 33010602011771号