实验四

任务五
textcoder.hpp
 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <iomanip>
 5 using namespace std;
 6 class TextCoder
 7 {
 8 private:
 9     string text;
10     void encoder(string &a)
11     {
12         for (int i = 0; i < a.length(); i++)
13         {
14             if (a[i] >= 65 && a[i] <= 90)
15             {
16                 a[i] = 65 + (a[i] - 65 + 7) % 26;
17             }
18             else if (a[i] >= 97 && a[i] <= 122)
19             {
20                 a[i] = 97 + (a[i] - 97 + 7) % 26;
21             }
22         }
23     }
24     void docedor(string &b)
25     {
26         for (int i = 0; i < b.length(); i++)
27         {
28             if (b[i] >= 65 && b[i] <= 90)
29             {
30                 b[i] = 90 - (90 - b[i] + 7) % 26;
31             }
32             else if (b[i] >= 97 && b[i] <= 122)
33             {
34                 b[i] = 122 - (122 - b[i] + 7) % 26;
35             }
36         }
37     }
38 public:
39     TextCoder(string text) :text{ text } {}
40     ~TextCoder() {}
41     string get_ciphertext();//调用私有成员encoder()加密text中的文本
42     string get_deciphertext();//调用私有成员decoder()解密text中的文本
43 };
44 string TextCoder::get_ciphertext()
45 {
46     string a=text;
47     encoder(a);
48     return a;
49 }
50 string TextCoder::get_deciphertext()
51 {
52     string b = text;
53     docedor(b);
54     return b;
55 }
View Code
task5.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 }
View Code

 

任务六

task6.cpp

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include"info.hpp" 
 5 int main()
 6 {
 7     string a, b, c; int d,sum=0;
 8     int const capacitd = 100;
 9     vector<Info> x;
10     cout << "录入信息:" << endl<<endl;
11     cout << "昵称\t    " << "联系方式(邮箱/手机号)\t    " << "所在城市\t    " << "预定参加人数\t" << endl;
12     for ( sum ;;)
13     {
14         cin >> a >> b>> c >> d;
15         Info z(a, b, c, d);
16         int y = sum; 
17         if (sum+d > capacitd)
18         {
19             cout << "对不起,只剩" << capacitd - y << "个位置" << endl;
20             cout << "1.输入u,更新(update)预定信息" << endl;
21             cout << "2.输入o,退出(out)预定" << endl; char s;
22             cout << "您的选择:"; cin >> s;
23             if (s == 'u')    continue; cout << endl;
24             break;
25         }
26         else
27         {
28             sum += d;
29             x.push_back(z);
30         }
31     }
32     cout << "截至目前,一共有" << sum << "听众预定参加。预定听众信息如下。" << endl;
33     for (int i = 0; i < x.size(); i++)
34     {
35         x[i].print();
36     }
37 }
View Code

info.hpp

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

 

 

posted @ 2023-11-28 19:02  dmsx  阅读(25)  评论(0)    收藏  举报