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

1. 本次实验,目标是在实践中学习C++标准库中string, vector, array以及迭代器相关的基础用法。

task5.cpp:

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

Info.hpp

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 class Info
 5 {
 6 public:
 7     Info(string nickname, string contact, string city, int n1) :m_nickname(nickname), m_contact(contact), m_city(city), n(n1) {};
 8     void print() const;
 9 private:
10     string m_nickname, m_contact, m_city;
11     int n;
12 };
13 void Info::print()const
14 {
15     cout << "称呼: " << m_nickname << endl;
16     cout << "联系方式: " << m_contact << endl;
17     cout << "所在城市: " << m_city << endl;
18     cout << "预定人数: " << n << endl;
19 }

更改数据后得两次运行结果:

 

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 }

TextCoder.hpp:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 class TextCoder
 5 {
 6 public:
 7     TextCoder(string text);
 8     string encoder();
 9     string decoder();
10 private:
11     string text;
12 };
13 TextCoder::TextCoder(string text0) :text(text0) {}
14 string TextCoder::encoder()
15 {
16     for (int i = 0; i < text.length(); i++)
17     {
18         if (text[i] >= 'A' && text[i] <= 'U' || text[i] >= 'a' && text[i] <= 'u')
19             text[i] += 5;
20         else if (text[i] >= 'V' && text[i] <= 'Z' || text[i] >= 'v' && text[i] <= 'z')
21             text[i] -= ('V' - 'A');
22     }
23     return text;
24 }
25 string TextCoder::decoder()
26 {
27     for (int i = 0; i < text.length(); i++)
28     {
29         if (text[i] >= 'F' && text[i] <= 'Z' || text[i] >= 'f' && text[i] <= 'z')
30             text[i] -= 5;
31         else if (text[i] >= 'A' && text[i] <= 'E' || text[i] >= 'a' && text[i] <= 'e')
32             text[i] += ('V' - 'A');
33     }
34     return text;
35 }

更改数据后的测试结果:

 

 

 

posted @ 2021-11-03 10:18  呆瓜不呆baci  阅读(14)  评论(3编辑  收藏  举报