四、实验内容
1.实验任务1
//在C++中定义和使用类模板
task1_1.cpp
#include <iostream> using namespace std; // 类A的定义 class A{ public: A(int x0, int y0): x{ x0 }, y{ y0 } {} void show() const { cout << x << ", " << y << endl; } private: int x, y; }; // 类B的定义 class B{ public: B(double x0, double y0): x{ x0 }, y{ y0 } {} void show() const { cout << x << ", " << y << endl;} private: double x, y; }; void test() { cout << "测试类A: " << endl; A a(3, 4); a.show(); cout << "\n测试类B: " << endl; B b(3.2, 5.6); b.show(); } int main() { test(); }
运行结果:
task1_2.cpp
#include <iostream> #include <string> using namespace std; // 定义类模板X template<typename T> class X{ public: X(T x0, T y0): x{x0}, y{y0} {} void show() const { cout << x << ", " << y << endl;} private: T x, y; }; void test() { cout << "测试1: 模板类抽象类型T用int实例化" << endl; X<int> x1(3, 4); x1.show(); cout << "\n测试2: 模板类抽象类型T用double实例化" << endl; X<double> x2(3.2, 5.6); x2.show(); cout << "\n测试3: 模板类抽象类型T用标准库中的string实例化" << endl; X<std::string> x3("hello", "c plus plus"); x3.show(); } int main() { test(); }
运行结果:
2.实验任务2
//string类的部分使用方法
task2_1.cpp
#include <iostream> #include <string> #include <limits> using namespace std; int main() { const int n = 10; string prompt = string(n, '*') + "Enter a string: " + string(n, '*') + '\n'; cout << "测试1:"; cout << prompt; string s1; cin >> s1; // 从输入流中提取字符串给s1,碰到空格、回车、Tab键即结束 cout << "s1: " << s1 << endl; cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清空输入缓冲区 cout << "\n测试2:"; cout << prompt; getline(cin, s1); // 从输入流中提取一行字符串给s1,直到换行 cout << "s1: " << s1 << endl; cout << "\n测试3:"; string s2, s3; cout << prompt; getline(cin, s2, ' '); // 从输入流中提取字符串给s2,直到指定分隔符空格 getline(cin, s3); cout << "s2: " << s2 << endl; cout << "s3: " << s3 << endl; }
运行结果:
task2_2.cpp
#include <iostream> #include <string> int main() { using namespace std; string s; cout << "Enter words: \n"; // 重复录入字符串,将小写字符转换成大写,直到按下Ctrl+Z while( getline(cin, s) ) { for(auto &ch: s) ch = toupper(ch); cout << s << "\n"; } }
运行结果:
task2_3.cpp
#include <iostream> #include <string> #include <algorithm> int main() { using namespace std; string s1; cout << "Enter words: \n"; getline(cin, s1); cout << "original words: \n"; cout << s1 <<endl; cout << "to uppercase: \n"; transform(s1.begin(), s1.end(), s1.begin(), ::toupper); cout << s1 << endl; }
运行结果:
task2_4.cpp
#include <iostream> #include <string> int main() { using namespace std; string s1, s2; s1 = "nuist"; // 赋值 s1[0] = 'N'; // 支持通过[]和索引方式访问 s1.at(1) = 'U'; // 支持通过xx.at()方法访问 cout << boolalpha << (s1 == "nuist") << endl; // 字符串比较 cout << s1.length() << endl; // 字符串长度 cout << s1.size() << endl; // 字符串长度 s2 = s1 + ", 2050"; // 字符串连接 cout << s2 << endl; string email{"[email protected]"}; auto pos = email.find("@"); // 查找子串"@"第一次出现的索引位置,如果失败,返回string::npos if (pos == string::npos) cout << "illegal email address"; else { auto s1 = email.substr(0, pos); // 取子串, 从索引0 ~ pos-1 auto s2 = email.substr(pos + 1); // 取子串,从pos+1到末尾 cout << s1 << endl; cout << s2 << endl; } string phone{"15216982937"}; cout << phone.replace(3, 5, string(5, '*')) << endl; // 把从索引位置为3开始的连续5个字符替换成* string s3{"cosmos"}, s4{"galaxy"}; cout << "s3: " + s3 + " s4: " + s4 << endl; s3.swap(s4); // 交换 cout << "s3: " + s3 + " s4: " + s4 << endl; string s5{"abc"}; const char *pstr = s5.c_str(); // 方法c_str()把string类字符串组转换成C风格的字符串 cout << pstr << endl; string s6{"12306"}; int x1 = stoi(s6); // 把string转换成int cout << x1 << endl; int x2 = 12306; string s7 = to_string(x2); // 把int转换成string cout << s7 << endl; double x3 = 123.06; string s8 = to_string(x3); // 把double转换成string cout << s8 << endl; }
运行结果:
3.实验任务3
//动态数组类模板vector的基础用法
task3_1.cpp
#include <iostream> #include <vector> using namespace std; template<typename T> void output(const T &obj) { for(auto &item: obj) cout << item << ", "; cout << "\b\b \n"; } int main() { vector<int> v1; // 创建一个vector对象v1, 未指定大小, 元素是int型, 未初始化 vector<int> v2(5); // 创建一个vector对象v2, 包含5个元素,元素是int型,初始值是默认值0 vector<int> v3(5, 42); // 创建一个vector对象v3, 包含5个元素,元素是int型,指定初始值是42 vector<int> v4{1, 9, 8, 4}; // 创建一个vector对象v4, 元素是int型,使用初始化列表方式 vector<int> v5{v4}; // 创建一个vector对象v5, 使用已经存在的对象v4创建 cout << "v2: "; output(v2); cout << "v3: "; output(v3); cout << "v4: "; output(v4); cout << "v5: "; output(v5); }
运行结果:
task3_2.cpp
#include <iostream> #include <vector> using namespace std; void output(const vector<int> &v) { cout << "v.size() = " << v.size() << endl; cout << "v.capacity() = " << v.capacity() << endl; cout << endl; } int main() { vector<int> v{42}; output(v); v.push_back(55); v.push_back(90); output(v); for(auto i = 0; i < 8; ++i) v.push_back(i); output(v); v.pop_back(); output(v); }
运行结果:
4.实验任务4
//C++标准模板库中序列容器、迭代器在数据存储、访问/操作的通用处理方式
task4.cpp
#include <iostream> #include <vector> #include <array> #include <string> #include <algorithm> using namespace std; // 函数模板 // 通过索引方式输出对象值 template<typename T> void output1(const T &obj) { for(auto i = 0; i < obj.size(); ++i) cout << obj.at(i) << ", "; cout << "\b\b \n"; } // 函数模板 // 通过迭代器方式输出对象值 template<typename T> void output2(const T &obj) { for(auto p = obj.cbegin(); p != obj.cend(); ++p) cout << *p << ", "; cout << "\b\b \n"; } // 函数模板 // 通过auto for方式输出对象值 template<typename T> void output3(const T &obj) { for(auto &item: obj) cout << item << ", "; cout << "\b\b \n"; } // 测试string类对象 void test1() { string s1{"cplus"}; output1(s1); reverse(s1.begin(), s1.end()); // 对对象s1中的数据项进行翻转 output2(s1); sort(s1.begin(), s1.end()); // 对对象s1中的数据项排序(默认升序) output3(s1); } // 测试array<int>类对象 void test2() { array< array<int, 4>, 3> x{1, 9, 8, 4, 2, 0, 2, 3, 2, 0, 4, 9 }; output1( x.at(0) ); output2( x.at(1) ); output3( x.at(2) ); } // 测试vector<string>类对象 void test3() { vector<string> v1 {"Sheldon", "Leonard", "Howard", "Raj"}; v1.push_back("Penny"); v1.push_back("Amy"); output1(v1); sort(v1.begin(), v1.end(), std::greater<string>()); // 对v1对象中的字符串按降序排序 output2(v1); reverse(v1.begin(), v1.end()); // 对v1对象中的字符串翻转 output3(v1); } int main() { cout << "测试1: " << endl; test1(); cout << "测试2: " << endl; test2(); cout << "测试3: " << endl; test3(); }
运行结果:
5.实验任务5
textcoder.hpp
#pragma once #include<iostream> #include<string> using namespace std; class TextCoder { public: TextCoder(const string& s); string getText() const; string get_ciphertext(); string get_deciphertext(); private: string text; void encoder(); void decoder(); }; TextCoder::TextCoder(const string& s) : text(s) {} string TextCoder::getText() const { return text; } string TextCoder::get_ciphertext() { encoder(); return text; } string TextCoder::get_deciphertext() { decoder(); return text; } void TextCoder::encoder() { for (char& ch : text) { if (isalpha(ch)) { char base = islower(ch) ? 'a' : 'A'; ch = (ch - base + 7) % 26 + base; } } } void TextCoder::decoder() { for (char& ch : text) { if (isalpha(ch)) { char base = islower(ch) ? 'a' : 'A'; ch = (ch - base + 19) % 26 + base; } } }
task5.cpp
#include "textcoder.hpp" #include <iostream> #include <string> void test() { using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本: "; while (getline(cin, text)) { encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象 cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象 cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } } int main() { test(); }
运行结果:
6.实验任务6
Info.hpp
#pragma once #include <iostream> #include <string> #include <iomanip> using namespace std; class Info { public: Info(const string& nickname, const string& contact, const string& city, int n); void print() const; private: string nickname; string contact; string city; int n; }; Info::Info(const string& s1, const string& s2, const string& s3, int x) :nickname{ s1 }, contact{ s2 }, city{ s3 }, n{ x } {} void Info::print() const { cout << left << setw(15) << "昵称:" << nickname << endl; cout << left << setw(15) << "联系方式:" << contact << endl; cout << left << setw(15) << "所在城市:" << city << endl; cout << left << setw(15) << "预定人数:" << n << endl; cout << endl; }
task6.cpp
#include "Info.hpp" #include <iostream> #include <vector> #include<limits> using namespace std; const int capacity = 100; void test() { vector<Info> audience_info_list; cout << "录入信息:\n\n"; string nickname, contact, city; int n; int N = 0; while ( N <= capacity) { // 录入信息 cout << "昵称:"; cin >> nickname; if (cin.eof()) { break; // Ctrl+Z 导致循环中止 } cout << "联系方式(邮箱/手机号):"; cin >> contact; cout << "所在城市:"; cin >> city; cout << "预定参加人数:"; cin >> n; cout << endl; if (cin.eof()) { break; // Ctrl+Z 导致循环中止 } N += n; //判断预定参加人数是否超过livehouse场地剩余容量 if (N > capacity) { N -= n; int remain = capacity - N; char choice; cout << "对不起,只剩" << remain << "个位置。\n"; cout << "1.输入u,更新(update)预定信息\n"; cout << "2.输入q,退出预定\n"; cout << "你的选择:"; cin >> choice; if (choice == 'q') { cout << "退出预定。\n"; break; } else if (choice == 'u') { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "请重新输入信息。\n"; continue; } } audience_info_list.emplace_back(nickname, contact, city, n); } // 打印预约参加livehouse的听众信息 cout << "\n截至目前,一共有" << N << "位听众预定参加。预定听众信息如下:" << endl; for (const auto& info : audience_info_list) { info.print(); } } int main() { test(); return 0; }
运行结果1:
运行结果2:
六、实验总结
仍然有很多需要改进的地方,实验六的部分代码还可以进一步完善。希望在不断动手实践的过程中学习新的知识,多加以运用到今后实际代码的编写,加深记忆,学会真正运用。
标签:const,string,int,void,C++,实验,include,模板,cout From: https://www.cnblogs.com/Rainbow-forest/p/17868081.html