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

实验任务5

Info.hpp

 1 #ifndef INFO_HPP
 2 #define INFO_HPP
 3 
 4 #include <iostream>
 5 #include <string>
 6 using namespace std;
 7 
 8 class Info{
 9 public:
10     Info(string nickname1, string contact1, string city1, int n1);
11     void print() const;
12 
13 private:
14     string nickname;
15     string contact;
16     string city;
17     int n;
18 };
19 
20 Info::Info(string nickname1, string contact1, string city1, int n1) : nickname{nickname1}, contact{contact1}, city{city1}, n{n1} {
21 
22 }
23 
24 void Info::print() const {
25     cout << "称呼:\t\t" << nickname << endl;
26     cout << "联系方式:\t" << contact << endl;
27     cout << "所在城市:\t" << city << endl;
28     cout << "预订人数:\t" << n << endl;
29 }
30 
31 #endif

task5.cpp

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

 

 

 

实验任务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 public:
10     TextCoder(string text1);
11     string encoder();
12     string decoder();
13 
14 private:
15     string text;
16 };
17 
18 TextCoder::TextCoder(string text1): text{text1} {
19 
20 }
21 
22 string TextCoder::encoder() {
23     for (int i = 0; i < text.length(); i++) {
24         if (text[i] >= 'a' && text[i] <= 'z') {
25             text[i] += 5;
26             if (text[i] > 'z') {
27                 text[i] -= 26;
28             }
29         }
30         if (text[i] >= 'A' && text[i] <= 'Z') {
31             text[i] += 5;
32             if (text[i] > 'Z') {
33                 text[i] -= 26;
34             }
35         }
36     }
37     return text;
38 }
39 
40 string TextCoder::decoder() {
41     for (int i = 0; i < text.length(); i++) {
42         if (text[i] >= 'a' && text[i] <= 'z') {
43             text[i] -= 5;
44             if (text[i] < 'a') {
45                 text[i] += 26;
46             }
47         }
48         if (text[i] >= 'A' && text[i] <= 'Z') {
49             text[i] -= 5;
50             if (text[i] < 'A') {
51                 text[i] += 26;
52             }
53         }
54     }
55     return text;
56 }
57 
58 #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 }

 

实验总结

\b : 将光标从当前位置向前(左)移动一个字符(遇到\n或\r则停止移动),并从此位置开始输出后面的字符(空字符和换行符\n除外)

posted @ 2021-10-27 20:27  ╮君颜~ઇଓ  阅读(26)  评论(1编辑  收藏  举报