实验三

Task5

info.hpp

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

task5.cpp

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<algorithm>
 5 #include<iomanip>
 6 #include<limits>
 7 #include"info.hpp"
 8 #define MAX 100;
 9 
10 using namespace std;
11 
12 int main() {
13     const int capacity = MAX;
14     vector<Info> audience_info_list;
15     string nn,cont,ct; //具体信息
16     int num=0,sum=0;
17 
18     //录入信息
19     cout << "录入信息:\n\n";
20     cout << left
21         << setw(16) << "昵称"
22         << setw(30) << "联系方式(邮箱/手机号)"
23         << setw(16) << "所在城市"
24         << setw(12) << "预定参加人数"
25         << endl;
26     while (cin>>nn>>cont>>ct>>num) { //每输入一组信息,就尝试放到Info类数组中
27         sum += num;
28         if (sum > capacity) {
29             cout << "对不起,只剩" << capacity-sum + num << "个位置.\n";
30             sum -= num;
31             cout << "1.输入u,更新(update)预定信息\n2.输入q,退出预定\n你的选择:";
32             char choice;
33             cin >> choice;
34             if (choice == 'u') {
35                 cout << "请输入更新预定信息:\n";
36                 continue;
37             }
38             else if (choice == 'q')
39                 break;
40         }
41         else
42             audience_info_list.push_back( Info (nn, cont, ct, num));
43     }
44     //打印信息
45     cout <<endl<< "截至目前,一共有" << sum << "位听众参加。预定听众信息如下:\n";
46     for (auto& item : audience_info_list)
47         item.print();
48 }

 

结果:

 

 

 

Task6

textcoder.hpp

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

task6.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 << "加密后英文文本:" << encoded_text << endl;
14 
15         decoded_text = TextCoder(encoded_text).get_deciphertext(); // 使用临时无名对象
16         cout << "解密后英文文本:" << decoded_text << endl;
17         cout << "输入英文文本:";
18     }
19 }
20 
21 int main() {
22     test();
23 }

 

结果:

 

posted @ 2022-10-26 09:31  Weslie_xyy  阅读(10)  评论(0编辑  收藏  举报