- 实验任务五
info.hpp:
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 using namespace std; 5 class info { 6 string nickname; 7 string contact; 8 string city; 9 int n;//预定参加人数 10 public: 11 info(){} 12 info(string name0,string city0,string contact0,int n0):nickname(name0),contact(contact0),city(city0),n(n0){} 13 ~info(){} 14 void set_name(string x) { nickname = x; } 15 void set_city(string x) { city = x; } 16 void set_contact(string x) { contact = x; } 17 void set_num(int x) { n = x; } 18 void print(); 19 }; 20 void info::print() { 21 cout << setiosflags(ios::left); 22 cout << setw(20) << "昵称:" << nickname << endl;; 23 cout << setw(20) << "联系方式:" << contact<<endl; 24 cout << setw(20) << "所在城市:" << city << endl; 25 cout << setw(20) << "预定人数:" << n<<endl; 26 }
liveshow.cpp:
1 #include"info.hpp" 2 #include<iostream> 3 #include<string> 4 #include<vector> 5 #include<iomanip> 6 using namespace std; 7 int main() { 8 const int capacity = 100; 9 vector<info> audience_info_list(100); 10 string t1, t2, t3, choose; 11 int t4,count=0,i=0,flag=1; 12 cout << "录入信息"<<endl<<endl; 13 cout << setiosflags(ios::left); 14 cout <<setw(20)<< "昵称" << setw(40) << "联系方式(邮箱/手机号)" << setw(20) << "所在城市" 15 << "预定参加人数" << endl; 16 while (cin>>t1>>t2>>t3>>t4) 17 { 18 if (t4 <= capacity - count) { 19 audience_info_list[i].set_name(t1); 20 audience_info_list[i].set_contact(t2); 21 audience_info_list[i].set_city(t3); 22 audience_info_list[i].set_num(t4); 23 i++; 24 count += t4; 25 }//if 26 else { 27 cout << "对不起,只剩" << capacity - count << "个位置" << endl; 28 cout << "1.输入u,更新(update),预定信息" << endl; 29 cout << "2.输入q,退出预定" << endl; 30 cout << "你的选择: "; 31 while (cin >> choose) { 32 if (choose == "q") { 33 flag = 0; 34 break; 35 } 36 else if (choose == "u") { 37 cin >> t1 >> t2 >> t3 >> t4; 38 if (t4 > capacity - count) { 39 cout << "对不起,只剩" << capacity - count << "个位置" << endl; 40 cout << "1.输入u,更新(update),预定信息" << endl; 41 cout << "2.输入q,退出预定" << endl; 42 cout << "你的选择"; 43 continue; 44 } 45 audience_info_list[i].set_name(t1); 46 audience_info_list[i].set_contact(t2); 47 audience_info_list[i].set_city(t3); 48 audience_info_list[i].set_num(t4); 49 i++; 50 count += t4; 51 break; 52 } 53 } 54 }//else 55 if (flag == 0||count==capacity) 56 break; 57 }//while 58 cout << "截至目前,一共有" << count << "位听众预定参加,预定听众信息如下:" << endl; 59 for (int j = 0; j < i; j++) 60 { 61 audience_info_list[j].print(); 62 cout << endl; 63 } 64 }
运行截图:
- 实验任务六
Textcoder.hpp:
1 #pragma once 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 class TextCoder { 6 string text; 7 void encoder(); 8 void decoder(); 9 public: 10 TextCoder(){} 11 TextCoder(string text1):text{text1}{} 12 ~TextCoder(){} 13 string get_ciphertext() { encoder(); return text; } 14 string get_deciphertext() { decoder(); return text; } 15 }; 16 void TextCoder::encoder() { 17 for (auto& obj : text) { 18 if ((obj >= 'a' && obj <= 'u') || (obj >= 'A' && obj <= 'U')) 19 obj += 5; 20 else if ((obj > 'u' && obj <= 'z') || (obj > 'U' && obj <= 'Z')) 21 obj = obj - 21; 22 } 23 } 24 void TextCoder::decoder() { 25 for (auto& obj : text) { 26 if ((obj >= 'f' && obj <= 'z') || (obj >= 'F' && obj <= 'Z')) 27 obj = obj - 5; 28 else if ((obj > 'a' && obj <= 'f') || (obj > 'A' && obj <= 'f')) 29 obj += 21; 30 } 31 }
task6.cpp:
1 #include "textcoder.hpp" 2 #include <iostream> 3 #include <string> 4 5 void test() { 6 using namespace std; 7 8 string text, encoded_text, decoded_text; 9 10 cout << "输入英文文本: "; 11 while (getline(cin, text)) { 12 encoded_text = TextCoder(text).get_ciphertext(); 13 cout << "加密后英文文本:\t" << encoded_text << endl; 14 15 decoded_text = TextCoder(encoded_text).get_deciphertext(); 16 cout << "解密后英文文本:\t" << decoded_text << endl; 17 cout << "\n输入英文文本: "; 18 } 19 } 20 21 int main() { 22 test(); 23 }
运行截图:
标签:obj,string,text,void,实验,&&,include From: https://www.cnblogs.com/zzzys/p/16816300.html