实验三

实验 任务5 info.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<vector>
 5 using namespace std;
 6 class info {
 7 private:
 8     string nickname;
 9     string contact;
10     string city;
11     int   n;
12     //static int m;
13 public:
14     info(string nickname0=0, string contact0=0, string city0=0, int  n0=0) :nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ n0 } {}
15     void print();
16 };
17 //int info::m = 0;
18 void info::print() {
19     std::cout << "昵称:       " << nickname << endl;
20     std::cout << "联系方式:  " << contact << endl;
21     std::cout << "所在城市:  " << city << endl;
22     std::cout << "预定人数:  " << n << endl;
23     std::cout << endl;
24 }

实验任务5 源文件

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

实验任务6.hpp

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

实验任务6 cpp

#include<iostream>
#include"textcoder.hpp"
#include<string>

void test() {
using namespace std;

string text, encoded_text, decoded_text;

cout << "输入英文文本: ";
while (getline(cin, text)) {
encoded_text = TextCoder(text).get_ciphertext();
cout << "加密后英文文本:\t" << encoded_text << endl;
decoded_text = TextCoder(encoded_text).get_deciphertext();
cout << "解密后英文文本:\t" << decoded_text << endl;
cout << "\n输入英文文本: ";
}
}
int main() {
test();

}

 

posted @ 2022-10-25 17:42  观湖  阅读(49)  评论(0)    收藏  举报