yy-L886

导航

实验三

 1 #include <iostream>
 2 #include <string>
 3 #include <iomanip>
 4 using namespace std;
 5 class Info
 6 {
 7 public:
 8     Info() {}
 9     Info(string x, string y, string z, int num) : nickname{x}, contact{y}, city{z}, n{num} {}
10     ~Info() {}
11     void set_nickname(string m)
12     {
13         nickname = m;
14     }
15     void set_contact(string r)
16     {
17         contact = r;
18     }
19     void set_city(string p)
20     {
21         city = p;
22     }
23     void set_n(int e)
24     {
25         n = e;
26     }
27     void print()
28     {
29         cout << left << setw(8) << "昵称:" << nickname << endl;
30         cout << left << setw(8) << "联系方式:" << contact << endl;
31         cout << left << setw(8) << "所在城市:" << city << endl;
32         cout << left << setw(8) << "预定人数:" << n << endl;
33     }
34 
35 private:
36     string nickname, contact, city;
37     int n;
38 };
 1 源文件
 2 #include <iostream>
 3 #include <vector>
 4 #include "info.h"
 5 
 6 using namespace std;
 7 
 8 void test()
 9 {
10     const int capacity=100;
11     vector<Info> audience_info_list;
12     int n, sum = 0;
13     string name, number, city;
14     cout << "录入信息\n" << endl;
15     cout << "昵称\t" << "联系方式(邮箱/手机号)\t" << "所在城市\t" << "预定参加人数" << endl;
16     while (cin >> name)
17     {
18         cin >>number ;
19         cin >> city;
20         cin >> n;
21         sum = sum + n;
22         if (capacity >= sum)
23         {
24             Info T = Info(name, number, city, n);
25             audience_info_list.push_back(T);
26         }
27         else
28         {
29             string x;
30             sum = sum - n;
31             cout << "对不起,只剩" << capacity-sum << "个位置."<<endl;
32             cout << "1.输入u,更新(update)预定信息" << endl;
33             cout << "2.输入q,退出预定" << endl;
34             cout << "你的选择:"<< endl;
35             cin >> x;
36             if (x == "u")
37             {
38                 cout << "录入信息\n" << endl;
39                 cout << "昵称\t" << "联系方式(邮箱/手机号)\t" << "所在城市\t" << "预定参加人数" << endl;
40                 continue;
41             }
42             else if (x == "q")
43             {
44                 break;
45             }
46         }
47         if (sum == capacity) break;
48     }
49     cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
50     for (int j = 0; j < audience_info_list.size(); j++)
51     {
52         audience_info_list.at(j).print();
53     }
54 }
55 
56 int main()
57 {
58     test();
59 }
60 头文件
61 #pragma once
62 #include <iostream>
63 
64 using namespace std;
65 
66 class Info
67 {
68 public:
69     Info();
70     Info(string nickname,string contact,string city,int n):nickname{ nickname },contact{ contact },city{ city },n{n}{}
71     void print();
72 private:
73     string nickname, contact, city;
74     int n;
75 };
76 
77 void Info::print()
78 {
79     cout << "昵称:\t  \t" << nickname << endl;
80     cout << "联系方式:\t" << contact << endl;
81     cout << "所在城市:\t" << city << endl;
82     cout << "预定人数:\t" << n << endl;
83 }

 

 

 

 实验三 task6

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

 

 1 1 #include "textcoder.hpp"
 2  2 #include <iostream>
 3  3 #include <string>
 4  4 
 5  5 void test() {
 6  6     using namespace std;
 7  7 
 8  8     string text, encoded_text, decoded_text;
 9  9 
10 10     cout << "输入英文文本: ";
11 11     while (getline(cin, text)) {
12 12         encoded_text = TextCoder(text).get_ciphertext();  // 这里使用的是临时无名对象
13 13         cout << "加密后英文文本:\t" << encoded_text << endl;
14 14 
15 15         decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
16 16         cout << "解密后英文文本:\t" << decoded_text << endl;
17 17         cout << "\n输入英文文本: ";
18 18     }
19 19 }
20 20 
21 21 int main() {
22 22     test();
23 23 }
24 
25 task6.cpp

运行测试截图

 

 

 

posted on 2022-10-25 17:07  小猪葛屁了  阅读(7)  评论(0)    收藏  举报