编程语言
首页 > 编程语言> > 实验2 数组、指针与C++标准库

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

作者:互联网

T1:

info.hpp

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

info.cpp

 1 #include"info.hpp"
 2 #include<iostream>
 3 #include<vector>
 4 int main(void) {
 5     vector<info>audience_info_list;
 6     const int capacity = 100;
 7     cout << "录入信息:" << endl;
 8     cout << "称呼,联系方式(邮箱/手机号),所在城市,预定参加人数  " << endl;
 9     string nm, tel, ct;
10     int n;
11     int rem=capacity;
12     int reserve = 0;
13     while (cin >> nm) {
14         cin >> tel;
15         cin >> ct;
16         cin >> n;
17         if (rem - n >= 0) {
18             reserve += n;
19             audience_info_list.push_back(info(nm, tel, ct, n));
20             rem -= n;
21         }
22         else {
23             cout << "对不起,只剩" << rem << "个位置" << endl;
24             cout << "1、输入u,更新(update)预定信息" << endl;
25             cout << "2、输入q,退出预订" << endl;
26             cout << "请输入选择:" << endl;
27             string ch;
28             cin >> ch;
29             if (ch == "u") {
30                 cout << "请重新输入信息" << endl;
31             }
32             else
33                 break;
34         }
35 
36     }
37     cout << "\n现在一共有" << reserve << "名听众预定参加,信息如下:" << endl;
38     vector<info>::const_iterator i; 
39       for (i = audience_info_list.begin(); i != audience_info_list.end(); i++)
40            (*i).print();
41 
42 }

 

 T2:

TextCoder.hpp

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 class TextCoder {
 5 public:
 6     TextCoder(string t):text(t){}
 7     string encoder();
 8     string decoder();
 9 private:
10     string text;
11 };
12 string TextCoder::encoder() {
13     int n = 0;
14     while (text[n] != NULL) {
15         if (text[n] >= 'a' && text[n] <= 'z') {
16             if (text[n] > 'u')
17                 text[n] = 'a' + (text[n] - 'v');
18             else
19                 text[n] += 5;
20             }
21         else if (text[n] >= 'A' && text[n] <= 'Z') {
22             if (text[n] > 'U')
23                 text[n] = 'A' + (text[n] - 'V');
24             else
25                 text[n] += 5;
26         }
27         n++;
28     }
29     return text;
30 }
31 string TextCoder::decoder() {
32     int n=0;
33     while (text[n] != NULL) {
34         if (text[n] >= 'a' && text[n] <= 'z') {
35             if (text[n] < 'f')
36                 text[n] = 'z' - ('e' - text[n]);
37             else
38                 text[n] -= 5;
39         }
40         else if (text[n] >= 'A' && text[n] <= 'Z') {
41             if (text[n] < 'F')
42                 text[n] = 'Z' - ('E' - text[n]);
43             else
44                 text[n] -= 5;
45         }
46         n++;
47     }
48     return text;
49 }

TextCoder.cpp

 1 #include"TextCoder.hpp"
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 int main() {
 6     string text, encoded_text, decoded_text;
 7     cout << "输入英文文本:";
 8     while ( getline(cin,text) ){
 9         encoded_text = TextCoder(text).encoder();
10         cout << "加密后英文文本:\t" << encoded_text << endl;
11         decoded_text = TextCoder(encoded_text).decoder(); 
12         cout << "解密后英文文本:\t" << decoded_text << endl;
13         cout << "\n输入英文文本: ";
14     }
15 }

 

 

标签:info,string,int,text,C++,数组,include,TextCoder,指针
来源: https://www.cnblogs.com/finimpyq-blog/p/15503600.html