实验4 现代c++标准库与类模板

发布时间 2023-11-30 22:48:29作者: oacuiaka

实验任务1

task1.cpp源码

task1_1.cpp:

 1 #include <iostream>
 2 
 3 using std::cout;
 4 using std::endl;
 5 
 6 //类A的定义
 7 class A{
 8 public:
 9     A(int x0, int y0): x{x0}, y {y0} {}
10     void show() const {cout << x << "," << y << endl;}
11 private:
12     int x, y;
13 }; 
14 
15 //类B的定义
16 class B{
17 public:
18     B(double x0, double y0): x{x0}, y {y0} {}
19     void show() const {cout << x << "," << y << endl;}
20 private:
21     double x, y;
22 }; 
23 
24 void test(){
25     A a(3,4);
26     a.show();
27     
28     B b(3.2, 5.6);
29     b.show();
30 }
31 
32 int main() {
33     test();
34 }
View Code

运行测试结果:

task1_2.cpp:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 //定义模板类X
 8 template<typename T>
 9 class X{
10 public:
11     X(T x0, T y0):x{x0}, y{y0} {}
12     void show() const {cout << x << "," << y << endl;}
13 private:
14     T x, y; 
15 }; 
16 
17 void test() {
18     cout <<"测试1:模板类抽象类型T用int实例化" << endl;
19     X<int> x1(3,4);
20     x1.show();
21     
22     cout <<"\n测试2:模板类抽象类型T用double实例化" << endl;
23     X<double> x2(3.2,5.6);
24     x2.show();
25     
26     cout <<"\n测试3:模板类抽象类型T用标准库中的string实例化" << endl;
27     X<std::string> x3("hello","c plus plus");
28     x3.show();
29 }
30 
31 int main() {
32     test();
33 }
View Code

运行测试结果:

task2.cpp源码

task2_1,.cpp:

 1 #include <iostream>
 2 #include <string>
 3 #include <limits>
 4 
 5 using namespace std;
 6 
 7 int main() {
 8     
 9     
10     const int n = 10;
11     string prompt = string(n,'*') + "Enter a string: " + string(n,'*') + '\n';
12     
13     cout << "测试1:";
14     cout << prompt;
15     string s1;
16     cin >> s1;  //从输入流中提取字符串给s1,碰到空格、回车、Tab键即结束
17     cout << "s1: " << s1 << endl;
18     cin.ignore(numeric_limits<streamsize>::max(), '\n'); //清空缓冲区
19     
20     cout <<"\n测试2: "; 
21     cout << prompt;
22     getline(cin,s1);  //从输入流中提取字符串给s1,直到换行 
23     cout << "s1: " << s1 << endl; 
24     
25     cout << "\n测试3:";
26     string s2, s3;
27     cout << prompt;
28     getline(cin,s2,' ');  //从输入流中提取字符串给s2,直到换行
29     getline(cin,s3); 
30     cout << "s2: " << s2 << endl;
31     cout << "s3: " << s3 << endl; 
32 }
View Code

运行测试结果:

task2_2.cpp:

 1 #include <iostream>
 2 #include <string>
 3 
 4 int main() {
 5     using namespace std;
 6     
 7     string s;
 8     
 9     cout << "Enter words: \n";
10     //重复录入字符串,将小写字符转换为大写,直到按下ctrl+z
11     while( getline(cin, s)){
12         for(auto &ch: s)
13             ch = toupper(ch);
14         cout << s << "\n";
15     } 
16 }
View Code

运行测试结果:

task2_3.cpp:

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 
 5 int main(){
 6     using namespace std;
 7     
 8     string s1;
 9     
10     cout << "Enter words: \n";
11     getline(cin, s1);
12     cout << "original words: \n";
13     cout << s1 <<endl;
14     cout << "to uppercase: \n";
15     transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
16     cout << s1 << endl;
17 }
View Code

运行测试结果:

task2_4.cpp:

 1 #include <iostream>
 2 #include <string>
 3 
 4 int main() {
 5     using namespace std;
 6     
 7     string s1, s2;
 8     s1 = "nuist";
 9     s1[0] = 'N';
10     s1.at(1) = 'U';
11     cout << boolalpha << (s1 == "nuist") << endl;
12     cout << s1.length() << endl;
13     cout << s1.size() << endl;
14     s2 = s1 + ", 2050";
15     cout << s2 << endl;
16     
17     string email{"xyz@gmail.com"};
18     auto pos = email.find("@");
19     if(pos == string::npos)
20         cout << "illegal email address";
21     else{    
22         auto s1 = email.substr(0, pos);
23         auto s2 = email.substr(pos + 1);
24         cout << s1 << endl;
25         cout << s2 << endl;
26     }
27     
28     string phone{"15216982937"};
29     cout << phone.replace(3, 5, string(5, '*')) << endl;
30     
31     string s3{"cosmos"} , s4{"galaxy"};
32     cout << "s3: " + s3 + "s4: " + s4 << endl;
33     s3.swap(s4);
34     cout << "s3: " + s3 + "s4: " + s4 << endl;
35     
36     string s5{"abc"};
37     const char*pstr = s5.c_str();
38     cout << pstr << endl;
39     
40     string s6{"12306"};
41     int x1 = stoi(s6);
42     cout << x1 << endl;
43     
44     int x2 = 12306;
45     string s7 = to_string(x2);
46     cout << s7 << endl;
47     
48     double x3 = 123.06;
49     string s8 = to_string(x3);
50     cout << s8 << endl; 
51 }
View Code

运行测试结果:

task3.cpp源码

task3_1.cpp:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 
 5 template<typename T>
 6 void output(const T &obj) {
 7     for(auto &item: obj)
 8         std::cout << item << ",";
 9     std::cout << "\b\b \n";
10 }
11 
12 int main(){
13     using namespace std;
14     
15     vector<int> v1;
16     vector<int> v2(5);
17     vector<int> v3(5,42);
18     vector<int> v4{1, 9, 8, 4};
19     vector<int> v5{v4};
20     
21     output(v2);
22     output(v3);
23     output(v4);
24     output(v5);
25 }
View Code

运行测试结果:

task3_2.cpp:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::vector;
 7 
 8 void output(const vector<int> &v) {
 9     cout << "v.size() = " << v.size() << endl;
10     cout << "v.capacity() = " << v.capacity() << endl;
11     cout << endl;
12 }
13 
14 int main() {
15     vector<int> v{42};
16     output(v);
17     
18     v.push_back(55);
19     v.push_back(90);
20     output(v);
21     
22     for(auto i = 0; i < 8; i++)
23         v.push_back(i);
24     output(v);
25     
26     v.pop_back();
27     output(v);
28 }
View Code

运行测试结果:

task4.cpp源码

task4.cpp:

 1 #include <iostream>
 2 #include <vector>
 3 #include <array>
 4 #include <string>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 // 函数模板
10 // 通过索引方式输出对象值
11 template<typename T>
12 void output1(const T &obj) {
13     for(auto i = 0; i < obj.size(); ++i)
14         cout << obj.at(i) << ", ";
15     cout << "\b\b \n";
16 }
17 
18 // 函数模板
19 // 通过迭代器方式输出对象值
20 template<typename T>
21 void output2(const T &obj) {
22     for(auto p = obj.cbegin(); p != obj.cend(); ++p)
23         cout << *p << ", ";
24     cout << "\b\b \n";
25 }
26 
27 // 函数模板
28 // 通过auto for方式输出对象值
29 template<typename T>
30 void output3(const T &obj) {
31     for(auto &item: obj) 
32         cout << item << ", ";
33     cout << "\b\b \n";
34 }
35 
36 // 测试string类对象
37 void test1() {
38     string s1{"cplus"};
39 
40     output1(s1);
41 
42     reverse(s1.begin(), s1.end());  // 对对象s1中的数据项进行翻转
43     output2(s1);
44 
45     sort(s1.begin(), s1.end());   // 对对象s1中的数据项排序(默认升序)
46     output3(s1);
47 }
48 
49 // 测试array<int>类对象
50 void test2() {
51     array< array<int, 4>, 3> x{1, 9, 8, 4, 2, 0, 2, 3, 2, 0, 4, 9 };
52 
53     output1( x.at(0) );
54     output2( x.at(1) );
55     output3( x.at(2) );
56 }
57 
58 // 测试vector<string>类对象
59 void test3() {
60     vector<string> v1 {"Sheldon", "Leonard", "Howard", "Raj"};
61 
62     v1.push_back("Penny");
63     v1.push_back("Amy");
64 
65     output1(v1);
66 
67     sort(v1.begin(), v1.end(), std::greater<string>());  // 对v1对象中的字符串按降序排序
68     output2(v1);
69 
70     reverse(v1.begin(), v1.end());  // 对v1对象中的字符串翻转
71     output3(v1);
72 }
73 
74 int main() {
75     cout << "测试1: " << endl;
76     test1();
77 
78     cout << "测试2: " << endl;
79     test2();
80 
81     cout << "测试3: " << endl;
82     test3();
83 }
View Code

运行测试结果:

task5.cpp源码

textcoder.hpp:

 1 #include<iostream>
 2 #include<string>
 3 
 4 using std::string;
 5 
 6 class TextCoder {
 7 private:
 8     string text;
 9     void encoder(){
10         for (char& c : text) {
11             if (isalpha(c)) {
12                 if (islower(c)) {
13                        c = ((c - 'a' + 7) % 26) + 'a';
14                 } else {
15                     c = ((c - 'A' + 7) % 26) + 'A';
16                 }
17             }
18         }    
19     }
20         void decoder(){
21         for (char& c : text) {
22             if (isalpha(c)) {
23                 if (islower(c)) {
24                        c = ((c - 'a' - 7) % 26) + 'a';
25                 } else {
26                     c = ((c - 'A' - 7) % 26) + 'A';
27                 }
28             }
29         }    
30     }
31         };
32     public:
33         TextCoder(string &str): text{str} {};
34         TextCoder(const TextCoder &t): text{t.text} {};
35         string get_ciphertext() {
36             encoder();
37             return text;
38         }
39         string get_deciphertext() {
40             decoder();
41             return text;
42         }
43 };
View Code

task5.cpp:

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

运行测试结果:

task6.cpp源码

task6.cpp:

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

info.hpp:

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

运行测试结果: