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

实验目的

1. 理解和掌握类模板

2. 掌握普通数组的基础用法

3. 掌握指针的基础用法

4. 体验和熟悉C++标准库中固定大小的数组类模板array、动态数组类模板vector、字符串类string、 迭代器iterator的用法

5. 能够灵活、组合使用C++标准库与自定义类对具体应用场景的问题进行抽象和编程求解

实验任务 1 类模板

实验任务 2 string的基础用法

实验任务 3 动态数组类模板vector的基础用法及迭代器的使用

实验任务 4 固定大小数组类模板array的基础用法

实验任务 5 预约登记系统

编写代码实现场地预约信息登记

<info.hpp>

 1 #ifndef INFO_HPP
 2 #define INFO_HPP
 3 
 4 #include<iostream>
 5 #include<iomanip>
 6 using namespace std;
 7 
 8 class Info{
 9 public:
10     Info(string nkn,string ctt,string ct,int n0):nickname{nkn},contact{ctt},city{ct},n{n0}{};
11     void print() const;
12 private:
13     string nickname; //昵称
14     string contact; //联系方式
15     string city; //所在城市
16     int n; //预定参加人数
17 };
18 
19 void Info::print() const
20 {
21     cout << endl;
22     cout << left << setw(10) <<"称呼:" << nickname << endl;
23     cout << left << setw(10) <<"联系方式:" << contact << endl;
24     cout << left << setw(10) <<"所在城市:" << city << endl;
25     cout << left << setw(10) <<"预定人数:" << n << endl;
26 }
27 #endif

 

<task5.cpp>

 1 #include "info.hpp"
 2 #include <iostream>
 3 #include <vector>
 4 
 5 using namespace std;
 6 int main()
 7 {
 8     vector<Info> audience_info_list;
 9     const int capacity = 100; //场地最大容量
10     string s1, s2, s3;
11     int n, sum = 0, left = 100;
12     cout << "录入信息:" << endl
13          << endl;
14     cout << "称呼       联系方式        所在城市        预定参加人数" << endl;
15     while (cin >> s1 >> s2 >> s3 >> n)
16     {
17         Info info(s1, s2, s3, n);
18         left = capacity - sum; //剩余容量
19         if ((sum + n) < capacity)
20         {
21             audience_info_list.push_back(info);
22             sum = sum + n;
23         }
24         else
25         {
26             cout << "对不起,仅剩" << left << "个位置!" << endl;
27             cout << "1.输入u,更新(update)预定信息" << endl;
28             cout << "2.输入q,退出预定系统" << endl;
29             cout << "你的选择:";
30             char ch;
31             cin >> ch;
32             if (ch == 'q' || ch == 'u')
33                 break;
34         }
35     }
36 
37     cout << "截至目前,一共有" << sum << "位听众预定参加,预定信息如下:" << endl;
38     int nn = audience_info_list.size();
39     for (int i = 0; i < nn; i++)
40     {
41         audience_info_list[i].print();
42     }
43 }

测试结果:

 

实验任务 6 英文文本字符串的加密和解密

设计并实现一个类TextCoder,用于对英文文本字符串进行简单的加密和解密操作。

加密:每个英文字母用其后第5个字母替换

解密:每个英文字母用其前第5个字母替换

<textcoder.hpp>

 1 #ifndef TEXT_CODER_HPP
 2 #define TEXT_CODER_HPP
 3 
 4 #include <iostream>
 5 using namespace std;
 6 
 7 class TextCoder
 8 {
 9 public:
10     TextCoder(string txt) : text{txt} {};
11     ~TextCoder() = default;
12     string encoder(); //加密
13     string decoder(); //解密
14 
15 private:
16     string text; //待加密或解密的字符串
17 };
18 
19 string TextCoder::encoder()
20 {
21     int len = text.length();
22     for (int i = 0; i < len; i++)
23     {
24         if (text[i] >= 97 && text[i] <= 122) //'a'~'z'
25             if (text[i] + 5 > 122)
26                 text[i] = (text[i] - 21); //出界的情况
27             else
28                 text[i] = (text[i] + 5);
29         else if (text[i] >= 65 && text[i] <= 90) //'A'~'Z'
30             if (text[i] + 5 > 90)
31                 text[i] = (text[i] - 21); //出界的情况
32             else
33                 text[i] = (text[i] + 5);
34     }
35     return text;
36 }
37 
38 string TextCoder::decoder()
39 {
40     int len = text.length();
41     for (int i = 0; i < len; i++)
42     {
43         if (text[i] >= 97 && text[i] <= 122)
44             if (text[i] - 5 < 97)
45                 text[i] = (text[i] + 21); //出界的情况
46             else
47                 text[i] = (text[i] - 5);
48         else if (text[i] >= 65 && text[i] <= 90)
49             if (text[i] - 5 < 65)
50                 text[i] = (text[i] + 21); //出界的情况
51             else
52                 text[i] = (text[i] - 5);
53     }
54     return text;
55 }
56 #endif

<task6.cpp>

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

测试结果:

实验总结

1、对于输入输出流的使用需要更加灵活

posted @ 2021-11-02 20:28  dd摇摆  阅读(70)  评论(1)    收藏  举报