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

发布时间 2023-11-30 23:45:15作者: 春满城

实验任务5

 1 #include <iostream>
 2 #include <string>
 3 using namespace std; 
 4 class TextCoder 
 5 {
 6 public:
 7     TextCoder() = default;
 8     TextCoder(string str);
 9     string get_ciphertext();
10     string get_deciphertext();
11     ~TextCoder() = default;
12 
13 private:
14     string text;
15     void encoder();
16     void decoder();
17 };
18 TextCoder::TextCoder(string str) 
19 {
20     text = str;
21 }
22 string TextCoder::get_ciphertext() 
23 {
24     encoder();
25     return text;
26 }
27 string TextCoder::get_deciphertext() 
28 {
29     decoder();
30     return text;
31 }
32 void TextCoder::encoder() 
33 {
34     for (int i = 0; i < text.length(); i++) 
35     {
36         if (islower(text[i])) 
37         {
38             text[i] = (text[i] - 'a' + 7) % 26 + 'a';
39         }
40         else if (isupper(text[i])) 
41         {
42             text[i] = (text[i] - 'A' + 7) % 26 + 'A';
43         }
44     }
45 }
46 void TextCoder::decoder() 
47 {
48     for (int i = 0; i < text.length(); i++) 
49     {
50         if (islower(text[i])) {
51             text[i] = (text[i] - 'a' + 26 - 7) % 26 + 'a';
52         }
53         else if (isupper(text[i])) 
54         {
55             text[i] = (text[i] - 'A' + 26 - 7) % 26 + 'A';
56         }
57     }
58 }
textcoder.hpp
 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 
 5 void test() 
 6 {
 7     using namespace std;
 8     string text, encoded_text, decoded_text;
 9     cout << "输入英文文本: ";
10     while (getline(cin, text)) 
11     {
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 {
23     test();
24 }
task5.cpp

实验任务6

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