实验二 数组、指针与C++标准库

实验任务5

info.hpp文件源码

 1 #ifndef INFO_HPP
 2 #define INFO_HPP
 3 
 4 //info类的实现
 5 #include <iostream>
 6 #include <iomanip>
 7 #include <string>
 8 
 9 using namespace std;
10 
11 class Info
12 {
13 
14 public:
15     Info();
16     Info(string nickname0, string contact0, string city0, int n0 = 1);
17     Info(const Info &p);
18     ~Info();
19     void print(); //打印信息(昵称、联系方式、所在城市、预定参加人数)
20 
21 private:
22     string nickname; //昵称
23     string contact;  //联系方式
24     string city;     //所在成市
25     int n;           //预定参加人数
26 };
27 
28 Info::Info() {}
29 
30 Info::Info(string nickname0, string contact0, string city0, int n0) : nickname(nickname0), contact(contact0), city(city0), n(n0) {}
31 
32 Info::Info(const Info &p) : nickname(p.nickname), contact(p.contact), city(p.city), n(p.n) {}
33 
34 void Info::print()
35 {
36     cout << left << setw(10) << "称呼:" << nickname << endl;
37     cout << left << setw(10) << "联系方式:" << contact << endl;
38     cout << left << setw(10) << "所在城市:" << city << endl;
39     cout << left << setw(10) << "预订人数:" << n << endl;
40 }
41 
42 #endif

task5.cpp文件源码

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

实验结果截图

 

 

 


实验任务6

textcoder.hpp文件源码

 1 #ifndef TEXTCODER_HPP
 2 #define TEXTCODER_HPP
 3 
 4 #include <iostream>
 5 #include <string>
 6 using namespace std;
 7 
 8 class TextCoder
 9 {
10 public:
11     TextCoder();
12     TextCoder(string text0);
13     string encoder();
14     string decoder();
15 
16 private:
17     string text;
18 };
19 
20 TextCoder::TextCoder() {}
21 
22 TextCoder::TextCoder(string text0) : text(text0) {}
23 
24 string TextCoder::encoder()
25 {
26     string encode_text;
27 
28     for (auto const &r : text)
29     {
30         //如果r是字母
31         if ((r >= 65 && r <= 90) || (r >= 97 && r <= 122))
32         {
33             //如果r是大写字母,且r+5>90
34             if (r + 5 > 90 && r <= 90)
35                 encode_text += ((r - 59) % 26 + 64);
36             //如果r是小写字母,且r+5>122
37             else if (r + 5 > 122 && r <= 122)
38                 encode_text += ((r - 91) % 26 + 96);
39             else
40                 encode_text += (r + 5);
41         }
42 
43         else
44             encode_text += r;
45     }
46 
47     return encode_text;
48 }
49 
50 string TextCoder::decoder()
51 {
52     string decode_text;
53 
54     for (auto const &r : text)
55     {
56         //如果r是字母
57         if ((r >= 65 && r <= 90) || (r >= 97 && r <= 122))
58         {
59             // 如果r是大写字母,且r-5<65
60             if (r - 5 < 65 && r >= 65)
61                 decode_text += (r + 21);
62             // 如果r是小写字母,且r-5<92
63             else if (r - 5 < 97 && r >= 97)
64                 decode_text += (r + 21);
65             else
66                 decode_text += (r - 5);
67         }
68 
69         else
70             decode_text += r;
71     }
72     return decode_text;
73 }
74 
75 #endif

task6.cpp文件源码

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

实验结果截图

 


实验总结

  实验任务5总结:

  • 第一个困难的地方是输入格式的问题,选用哪一个函数输入这个问题一直困扰了好久,在getline()和cin函数中纠结()了好久。。。。。。。
  • 第二个困难的地方是用vector构建动态数组类的问题,包括如何声明的定义的问题(vector<Info> audience_info_list)。还有如何在动态数组类中插入新元素/对象,一开始使用的是push_back()函数,但是这么做,测试数据就没有对过。。。。(非常无语),后来翻书发现P414页还介绍了一种插入函数emplace_back(args)  (args是参数),抱着试试就试试的心态,测试了一下,发现效果意外的好,最后解决了问题。至于为什么之前采用push_back()失败的原因,还未解决,有待日后思考。
  • 第三个,task5.cpp第45行使用了迭代器,但是迭代器的知识还不是很了解,需要继续看书,对应内容在P402页。
  • 第四个,看了下原来的代码,info.hpp文件里的Info类中的复制构造函数似乎没有必要,有机会删掉再测试看一下。

  -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  实验任务6总结:  

  • 第一个encode加密,一个需要考虑的问题是如果你要加密的字母是xyz这样在26字母中倒数几个字母,那么加上5之后很显然会越界,所以需要一个公式来让它加5之后又从abc开始。没有推导出通用的公式,但是推出了一个可以解决几个特定字母的公式。
  • 第二个decode加密,一个需要考虑的问题是如果你要解密的字母是abc这样在26字母中开头几个字母,那么减去5之后很显然会越界,所以需要一个公式来让它减5之后又从xyz开始。没有推导出通用的公式,但是推出了一个可以解决几个特定字母的公式。
  • 第三个textcoder.hpp文件第28行代码for (auto const &r : text)里for循环的特殊使用方法,对应书本P49页,忘记的时候要记得去回顾。

  -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  其他实验任务总结

  • task2_2.cpp文件里介绍了getline()和cin用法,需要记得去回顾。  

 

posted @ 2021-10-27 22:30  请去看诡秘之主  阅读(34)  评论(3编辑  收藏  举报