实验二

#任务5

info.hpp

 1 #ifndef INFO_HPP
 2 #define INFO_HPP
 3 
 4 #include<iostream>
 5 #include<string>
 6 using namespace std;
 7 
 8 class info
 9 {
10 public:
11 
12     info(string a = "", string b = "", string c = "", int d = 0): nickname{a}, contact{b}, city{c}, n{d} {}
13         
14     void print() const
15     {
16         cout << "称呼:\t\t" << nickname << endl;
17         cout << "联系方式:\t" << contact << endl;
18         cout << "所在城市:\t" << city << endl;
19         cout << "预定人数:\t" << n << endl;
20     }
21 
22 private:
23     
24     string nickname;
25     string contact;
26     string city;
27     int n;//预定参加人数
28      
29 };
30 #endif

task5.cpp

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

 

 

 

#任务6

textcoder.hpp

 1 #ifndef TEXTCODER_HPP
 2 #define TEXTCODER_HPP
 3 
 4 #include<iostream>
 5 #include<string>
 6  
 7 using namespace std;
 8 
 9 class TextCoder
10 {
11 public:
12     TextCoder(string t) : text{t} {} 
13     string encoder()
14     {
15         for(int i = 0; text[i]!='\0'; i++)
16         {
17             if( ('a' <= text[i] && text[i] <= 'u' )||( 'A' <= text[i] &&text[i] <= 'U'))
18             text[i] += 5;
19             else if( ('v' <= text[i] && text[i] <= 'z' )||( 'V' <= text[i] &&text[i] <= 'Z'))
20             text[i] -=21;
21         }
22         return text;
23     }
24     
25     string decoder()
26     {
27         for(int i = 0; text[i]!='\0'; i++)
28         {
29             if( ('f' <= text[i] && text[i] <= 'z' )||( 'F' <= text[i] &&text[i] <= 'Z'))
30             text[i] -= 5;
31             else if( ('a' <= text[i] && text[i] <= 'e' )||( 'A' <= text[i] &&text[i] <= 'E'))
32             text[i] +=21;            
33         }    
34         return text;    
35     }
36     
37 private:
38     string text;
39 };
40 
41 #endif 

task6.cpp

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

 

 

posted @ 2021-10-28 19:17  曾经蜡笔没有小新i  阅读(28)  评论(2)    收藏  举报