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 12 private: 13 int x, y; 14 }; 15 16 // 类B的定义 17 class B { 18 public: 19 B(double x0, double y0) : x{ x0 }, y{ y0 } { } 20 void show()const { cout << x << "," << y << endl; } 21 22 private: 23 double x, y; 24 }; 25 26 int main() { 27 A a(3, 4); 28 a.show(); 29 30 B b(3.2, 5.6); 31 b.show(); 32 }
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 14 private: 15 T x, y; 16 }; 17 18 int main() { 19 X<int> x1(3, 4); 20 x1.show(); 21 22 X<double> x2(3.2, 5.6); 23 x2.show(); 24 25 X<std::string> x3("hello", "c plus plus"); 26 x3.show(); 27 }
task2_1.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'; // 支持通过xx.at()方法访问 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{ "[email protected]" }; 18 auto pos = email.find("@"); // 查找字串"@"第一次出现的索引位置,如果失败,返回string::npos 19 if (pos == string::npos) 20 cout << "illegal email address"; 21 else { 22 auto s1 = email.substr(0, pos); // 取子串,从索引0~pos-1 23 auto s2 = email.substr(pos + 1); // 取子串,从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; // 把从索引位置为3开始的连续五个字符替换成* 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); // 把string转换成int 42 cout << x1 << endl; 43 44 int x2 = 12306; 45 string s7 = to_string(x2); // 把int转换成string 46 cout << s7 << endl; 47 48 double x3 = 123.06; 49 string s8 = to_string(x3); // 把double转换成string 50 cout << s8 << endl; 51 }
task2_2.cpp
1 #include <iostream> 2 #include <string> 3 #include <limits> 4 5 int main() { 6 using namespace std; 7 8 const int n = 10; 9 string prompt = string(n, '*') + "Enter a string: " + string(n, '*') + '\n'; 10 11 cout << prompt; 12 string s1; 13 cin >> s1; // 从输入流中提取字符串给s1,碰到空格、回车、Tab键即结束 14 cout << "s1: " << s1 << endl; 15 cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清空输入缓冲区 16 17 cout << prompt; 18 getline(cin, s1); // 从输入流中提取一行字符串给s1,直到换行 19 cout << "s1: " << s1 << endl; 20 21 string s2, s3; 22 cout << prompt; 23 getline(cin, s2, ' '); // 从输入流中提取字符串给s2,直到指定分隔符空格 24 getline(cin, s3); 25 cout << "s2: " << s2 << endl; 26 cout << "s3: " << s3 << endl; 27 }
task2_3.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 }
task2_4.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 }
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; // 创建一个vector对象v1,未指定大小,元素是int型,未初始化 16 vector<int> v2(5); // 创建一个vector对象v2,包含5个元素,元素是int型,初始值是默认值0 17 vector<int> v3(5, 42); // 创建一个vector对象v3,包含5个元素,元素是int型,指定初始值是42 18 vector<int> v4{ 1,9,8,4 }; // 创建一个vector对象v4,元素是int型,使用初始化列表方式 19 vector<int> v5{ v4 }; // 创建一个vector对象v5,使用已经存在的对象v4创建 20 21 output(v2); 22 output(v3); 23 output(v4); 24 output(v5); 25 }
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 }
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()); // 对对象s2中的数据项排序(默认升序) 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, 2, 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 << "test1: " << endl; 76 test1(); 77 78 cout << "test2: " << endl; 79 test2(); 80 81 cout << "test3: " << endl; 82 test3(); 83 }
info.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 #include <iomanip> 6 7 using namespace std; 8 9 class info { 10 public: 11 info(string nickname0 = " ", string contact0 = " ", string city0 = " ", int n0 = 0); 12 ~info() = default; 13 void print() const; 14 15 private: 16 string nickname; 17 string contact; 18 string city; 19 int n; 20 }; 21 22 23 // 函数定义 24 info::info(string nickname0, string contact0, string city0, int n0) :nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ n0 } {} 25 26 void info::print() const { 27 cout << setw(15) << "昵称:" 28 << setw(15) << nickname << endl; 29 cout << setw(15) << "联系方式:" 30 << setw(15) << contact << endl; 31 cout << setw(15) << "所在城市:" 32 << setw(15) << city << endl; 33 cout << setw(15) << "预订人数:" 34 << setw(15) << n << endl; 35 cout << endl; 36 }
task5.cpp
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <iomanip> 5 #include "info.hpp" 6 7 using namespace std; 8 9 int main() { 10 const int capacity = 100; 11 vector<info> audience_info_list; 12 13 string name, con, city; 14 int reserve_n; 15 16 int reserved_n = 0, left_n = capacity; 17 18 cout << "录入信息:" << endl << endl; 19 cout << left << setw(15) << "昵称" 20 << setw(15) << "联系方式(邮箱/手机号)" 21 << setw(15) << "所在城市" 22 << setw(15) << "预定参加人数" << endl; 23 24 while (cin >> name) { 25 cin >> con >> city >> reserve_n; 26 reserved_n += reserve_n; 27 left_n -= reserve_n; 28 if (left_n < 0) { 29 char choice; 30 cout << endl; 31 cout << "对不起,只剩" << left_n + reserve_n << "个位置." << endl; 32 cout << "1.输入u,更新(update)预定信息" << endl; 33 cout << "2.输入q,退出预定" << endl; 34 cout << "你的选择:"; 35 reserved_n -= reserve_n; 36 left_n += reserved_n; 37 cin >> choice; 38 if (choice == 'u') 39 continue; 40 if (choice == 'q') 41 break; 42 } 43 else { 44 info audience(name, con, city, reserve_n); 45 audience_info_list.push_back(audience); 46 } 47 } 48 49 cout << endl; 50 cout << "截至目前,一共有" << reserved_n << "位听众预定参加。"; 51 cout << "预定听众信息如下:" << endl; 52 53 for (auto audience_info : audience_info_list) { 54 audience_info.print(); 55 } 56 }
textcoder.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 class TextCoder { 9 public: 10 TextCoder(string text0):text{text0} {} 11 string get_ciphertext(); 12 string get_deciphertext(); 13 14 private: 15 string text; 16 void encoder(); 17 void decoder(); 18 }; 19 20 21 // 函数定义 22 void TextCoder::encoder() { 23 for (auto& ch : text) { 24 if ((ch >= 'A' && ch <= 'U') || (ch >= 'a' && ch <= 'u')) 25 ch += 5; 26 else if ((ch >= 'V' && ch <= 'Z') || (ch >= 'v' && ch <= 'z')) 27 ch -= 21; 28 } 29 } 30 31 void TextCoder::decoder() { 32 for (auto& ch : text) { 33 if ((ch >= 'F' && ch <= 'Z') || (ch >= 'f' && ch <= 'z')) 34 ch -= 5; 35 else if ((ch >= 'A' && ch <= 'E') || (ch >= 'a' && ch <= 'e')) 36 ch += 21; 37 } 38 } 39 40 string TextCoder::get_ciphertext() { 41 encoder(); 42 return text; 43 } 44 45 string TextCoder::get_deciphertext() { 46 decoder(); 47 return text; 48 }
task6.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 << "加密后英文文本:" << encoded_text << endl; 14 15 decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象 16 cout << "解密后英文文本:" << decoded_text << endl; 17 cout << "\n输入英文文本:"; 18 } 19 } 20 21 int main() { 22 test(); 23 }标签:std,cout,对象,int,实验,using,include,string From: https://www.cnblogs.com/xiao-shi/p/16818051.html