实验3 数组、指针与现代C++标准库

实验任务5:

info.hpp文件源码

 

 1 #pragma once
 2 #include <string>
 3 #include <iostream>
 4 #include <iomanip>
 5 using namespace std;
 6 class info {
 7 public:
 8     info(string nickname , string contact, string city, int n) : nickname(nickname), contact(contact),
 9     city(city),n(n) {}
10     void print() {
11         cout << setw(15) << "昵称" << nickname << setw(15) << "联系方式(邮箱/手机号)" << contact
12             << setw(15) << "所在城市" << city  << setw(15) << "预定参加人数" << n << endl;
13 
14     }
15     ~info() {}
16 private:
17     string nickname;
18     string contact;
19     string city;
20     int n;
21 };

 

task5.cpp源码

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include <vector>
 4 #include "info.h"
 5 #include <string>
 6 static int people = 0;
 7 int main() {
 8     const int capacity = 100;
 9     vector<info> audience_info_list;
10     cout << "录入信息:" << endl;
11     cout << endl;
12     cout  << "昵称" << setw(30) << "联系方式(邮箱/手机号)" << setw(15) << "所在城市"
13         << setw(15) << "预定参加人数" << endl;
14     string nickname, contact, city;
15     int n;
16     while (cin >> nickname && cin >> contact && cin >> city && cin >> n) {
17         if (people + n <= capacity) {
18             info x(nickname, contact, city, n);
19             audience_info_list.push_back(x);
20             people += n;
21             /*if (people >= capacity)
22                 break;*/
23         }
24         else {
25             char choice;
26             cout << "预定参加人数超过livehouse场地剩余容量,输入q退出预定,或,输入u更新预定信息" << endl;
27             getchar();
28             choice = getchar();
29             if (choice == 'q')
30                 break;
31             else if (choice == 'u') {
32                 cout << "信息更新" << endl;
33             }
34             else
35                 cout << "输入错误,请重新输入" << endl;
36         
37         }
38         
39         
40     }
41     cout << "截止目前,一共有" << people << "位听众,预定听众信息如下:" << endl;
42     for (auto& item : audience_info_list)
43         item.print();
44 
45     return 0;
46 }

运行截图 

 实验任务6:

TextCoder.hpp源码:

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

task6.cpp程序源码:

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include "TextCoder.h"
 3 #include <iostream>
 4 #include <string>
 5 using namespace std;
 6 void test() {
 7     string text;
 8     string encoded_text, decoded_text;
 9     cout << "输入:";
10     while (getline(cin, text)) {
11         encoded_text = TextCoder(text).get_ciphertext();
12         cout << "加密:" << encoded_text << endl;
13         decoded_text = TextCoder(text).get_deciphertext();
14         cout << "解密:" << decoded_text << endl;
15         cout << endl;
16         cout << "输入:";
17     }
18     return ;
19 }
20 int main() {
21     test();
22 }

程序运行截图:

 

  

posted @ 2022-10-25 13:11  ABCDEFGOGOGO  阅读(22)  评论(1编辑  收藏  举报