实验四_OOP_张文瑞_202213260018

发布时间 2023-12-01 00:19:19作者: 张文瑞

实验任务5
TextCoder.hpp源码

 1 #include<iostream>
 2 #include<string>
 3 
 4 using std::string;
 5 
 6 class TextCoder {
 7     private:
 8         string text;
 9         void encoder();
10         void decoder();
11     public:
12         TextCoder(string &str);
13         TextCoder(const TextCoder &t);
14         string get_ciphertext();
15         string get_deciphertext();
16 };
17 TextCoder::TextCoder(string &str)
18     : text{str} {
19 }
20 TextCoder::TextCoder(const TextCoder &t)
21     : text{t.text} {
22 }
23 void TextCoder::encoder() {
24     for (auto &i : text) {
25         if (i >= 'a' && i <= 'z')
26             i = 'a' + ((i - 'a') + 7) % 26;
27         else if (i >= 'A' && i <= 'Z')
28             i = 'A' + ((i - 'A') + 7) % 26;
29     }
30 }
31 void TextCoder::decoder() {
32     for (auto &i : text) {
33         if (i >= 'a' && i <= 'z')
34             i = 'a' + ((i - 'a') + 26 - 7) % 26;
35         else if (i >= 'A' && i <= 'Z')
36             i = 'A' + ((i - 'A') + 26 - 7) % 26;
37     }
38 
39 }
40 string TextCoder::get_ciphertext() {
41     encoder();
42     return text;
43 }
44 string TextCoder::get_deciphertext() {
45     decoder();
46     return text;
47 }

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 }

运行测试截图

 实验任务6
Info.hpp文件源码

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

task6.cpp源码

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