实验(3)

实验5

task_5.cpp

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

info.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 
 6 using std::cout;
 7 using std::endl;
 8 using std::string;
 9 
10 class Info{
11 public:
12     Info();
13     Info(string a,string b,string c ,int t):nickname{a},contact{b},city{c},n{t}{}
14     void print();
15 private:
16     string nickname;
17     string contact;
18     string city;
19     int n;
20 };
21 
22 Info::Info() {
23     nickname = "";
24     contact = "";
25     city = "";
26     n = 100;
27 }
28 
29 
30 void Info::print() {
31     cout << "昵称:        " << nickname << endl;
32     cout << "联系方式:    " << contact << endl;
33     cout << "所在城市:    " << city << endl;
34     cout << "预定人数:    " << n <<endl;
35 }

 

实验6

task_6.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 }

textcoder.hpp

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

 

posted @ 2022-10-26 08:46  Z`x  阅读(47)  评论(0)    收藏  举报