实验4 现代C++标准库与类模板

发布时间 2023-11-27 22:58:11作者: 懿里
实验任务5:
1.代码:
textcoder.hpp:
 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <vector>
 5 #include <array>
 6 #include <string>
 7 using namespace std;
 8 
 9 class TextCoder
10 {
11     private:
12         string text;
13         void encoder()
14         {
15             for(auto i=0; i<text.size(); i++) {  
16             if(text.at(i) >= 'a' && text.at(i) <= 'z') {  
17              text.at(i) = 'a' + (text.at(i) - 'a' + 7) % 26;   
18         } 
19             else if(text.at(i) >= 'A' && text.at(i) <= 'Z') {  
20             text.at(i) = 'A' + (text.at(i) - 'A' + 7) % 26; 
21         }  
22     }  
23         }
24         void decoder()
25         { 
26             for(auto i=0; i<text.size(); i++) {  
27              if(text.at(i) >= 'a' && text.at(i) <= 'z') {  
28             text.at(i) = 'a' + (text.at(i)-'a' +26- 7) % 26; 
29         } 
30             else if(text.at(i) >= 'A' && text.at(i) <= 'Z') {  
31             text.at(i) = 'A' + (text.at(i) -'A'+26 - 7) % 26; 
32         }  
33     }  
34         }
35     public:
36         string get_ciphertext()
37         {
38             encoder();
39             return text;
40         }
41         string get_deciphertext()
42         {
43             decoder();
44             return text;
45         }
46         TextCoder(string a):text{a}{}
47         TextCoder(const TextCoder& b):text{b.text}{}
48         
49         
50     
51     
52 };
View Code

textcoder.cpp:

 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 void test() {
 5 using namespace std;
 6 string text, encoded_text, decoded_text;
 7 cout << "输入英文文本: ";
 8 while (getline(cin, text)) {
 9 encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象
10 
11 cout << "加密后英文文本:\t" << encoded_text << endl;
12 decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
13 
14 cout << "解密后英文文本:\t" << decoded_text << endl;
15 cout << "\n输入英文文本: ";
16 }
17 }
18 int main() {
19 test();
20 }
View Code

2.图片:

 

实验任务6:
1.代码:
Info.hpp:

 1 #pragma once
 2 #include<iostream>
 3 #include<vector>
 4 #include<string>
 5 using namespace std;
 6 class Info{
 7     private:
 8         
 9         string nickname;
10         string contact;
11         string city;
12         int n;
13     public:
14         Info(string &a,string &b,string &c,int &d):nickname{a},contact{b},city{c},n{d}{}
15         void print(){
16             cout<<"昵称:\t\t"<<nickname<<std::endl;
17             cout<<"联系方式:\t"<<contact<<std::endl;
18             cout<<"所在城市:\t"<<city<<std::endl;
19             cout<<"预定人数:\t"<<n<<std::endl;
20         }
21     
22 }; 
View Code

task6.cpp:

 1 #include"Info.hpp"
 2 #include<iostream>
 3 #include<vector>
 4 #include<string>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     string nickname, contact, city;
11     int n;
12     char choose;
13     const int capacity = 100;
14     vector<Info> audience_info_list;
15     int num=0;
16     cout << "录入信息:\n\n";
17     cout << "昵称\t\t";
18     cout << "联系方式(邮箱/手机号)\t\t";
19     cout << "所在城市\t";
20     cout << "预定人数" << endl;
21     
22     while (cin >> nickname >> contact >> city >> n) {
23         num+=n;
24         if(num<100){
25             Info I(nickname,contact,city,n);
26             audience_info_list.push_back(I);
27         }
28         if(num>100){
29             num-=n;
30             cout<<"对不起,只剩"<<capacity - num<<"个位置。\n";
31             cout<<"1.输入u,更新(updata)预定信息"<<endl;
32             cout<<"2.输入q,退出预定"<<endl;
33             cout<<"你的选择:";
34             cin>>choose;
35             if(choose=='q'){
36                 break;
37             }
38             if(choose=='p'){
39                 cout<<"请重新从昵称输入:"<<endl; 
40                 continue;
41             }
42         }
43         if(num==100){
44             cout<<"预订人数已满,已关闭入口。"<<endl;
45             break;
46         }
47     }
48     cout<<"截至目前,一共有" << num << "位听众预定参加。预定听众信息如下"<<endl;
49     for(auto i=0;i<audience_info_list.size();i++){
50             audience_info_list[i].print();
51             cout<<endl;
52     }
53     
54     
55  } 
View Code

 

 

2.图片: