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

实验任务5:

livehouse场地容量有限,最多容纳100位乐迷听众,现通过某平台开通线上预约登记。

请设计线上预约登记信息类Info。

​程序包括 Info.hpp与 task5.cpp 。

Info.hpp:


#ifndef INFO_HPP
#define INFO_HPP
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
class Info{ //Info类的定义
public:
Info(string nickname0, string contact0, string city0, int n0) :nickname(nickname0), contact(contact0), city(city0), n(n0) {};
~Info() {};
void print() const ;
private:
string nickname, contact, city;
int n;
};
void Info::print() const {
cout << "称呼: " << nickname<<endl;
cout << "联系方式: " << contact << endl;
cout << "所在城市: " << city << endl;
cout << "预定人数: " << n << endl;
}
#endif

 

task5.cpp:

 1 #include "Info.hpp"
 2 #include <vector>
 3 #include <iostream>
 4 
 5 const int capacity = 100;
 6 int main()
 7 {
 8     using namespace std;
 9     vector<Info>audience_info_list;
10     string a, b, c,choice;
11     int n,sum=0;
12     cout << "录入信息: " << endl;
13     cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
14     while (cin >> a&&a!="end")
15     {
16         cin >> b >> c >> n;
17         if (sum + n > capacity)
18         {
19             cout << "对不起,只剩" << capacity - sum << "个位置。" << endl;
20             cout << "1.输入u,更新(update)预定信息。" << endl;
21             cout << "2.输入q,退出预定。" << endl;
22             cout << "你的选择:";
23                 cin >> choice;
24                 if (choice == "u")  cout << "请重新输入信息:" << endl;
25                 else if (choice == "q") break;
26         }
27 
28         else
29         {    sum += n;
30         audience_info_list.push_back(Info(a, b, c, n));
31         }
32     }
33     cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
34      for (auto const& i : audience_info_list)
35         i.print();
36      cout << "\b\b \n";
37 //另一种遍历的办法: 
38  // for (auto it = audience_info_list.begin(); it != audience_info_list.end(); ++it)   
39  //(*it).print();
40  //cout << "\b\b \n";
41 }

更改数据后的运行结果:

 

实验任务6: 

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

程序包括task5.cpp和textcoder.hpp。

textcoder.hpp:

 1 #ifndef TEXTCODER_HPP
 2 #define TEXTCODER_HPP
 3 
 4 // Employee类的定义
 5 #include <iostream>
 6 #include <string>
 7 using std::cin;
 8 using std::cout;
 9 using std::endl;
10 using std::string;
11 class TextCoder {
12 public:
13     TextCoder(string text0) :text(text0) {};
14     string encoder() ;
15     string decoder() ;
16 private:
17     string text;
18 
19 };
20 string TextCoder::encoder(){
21     for (auto& i : text)
22         if ((i >= 'a' && i <= 'u') || (i >= 'A' && i <= 'U'))
23             i += 5;
24         else if ((i >= 'v' && i <= 'z') || (i >= 'V' && i <= 'Z'))
25             i -= 21;
26     return text;
27 }
28 string TextCoder::decoder(){
29     for (auto& i : text)
30         if ((i >= 'f' && i <= 'z') || (i >= 'F' && i <= 'Z'))
31             i -= 5;
32         else if ((i >= 'a' && i <= 'e') || (i >= 'A' && i <= 'E'))
33             i += 21;
34     return text;
35 }
36 #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 }

 

posted @ 2021-10-28 19:04  热橙汁  阅读(65)  评论(3)    收藏  举报