实验任务五:
Info.hpp:
#pragma once #include<iostream> #include<string> #include<iomanip> using namespace std; class Info { private: string nickname, contact, city; int n; public: Info(string a, string b, string c, int d) :nickname{ a }, contact{ b }, city{ c }, n{ d }{} ~Info() = default; void print()const; }; void Info::print()const { cout << left << setw(7) << "昵称:" << nickname << endl; cout << left << setw(7) << "联系方式:" << contact << endl; cout << left << setw(7) << "所在城市:" <<city << endl; cout << left << setw(7) << "预定人数:" <<n << endl; cout << endl; }
task5.cpp:
#include"Info.hpp" #include<iostream> #include<string> #include<vector> #include<iomanip> int main() { using namespace std; const int capacity = 100; vector<Info> audience_info_list; string s1, s2, s3; char ch; int m; static int count; count = 0; cout << "录入信息:\n\n" << "昵称 " << "联系方式(邮箱/手机号) " << "所在城市 " << "预定参加人数" << endl; while (cin >> s1) { cin >> s2 >> s3 >> m; if (count + m < capacity) { Info aud(s1, s2, s3, m); audience_info_list.push_back(aud); count += m; if (m == capacity) break; } else { int left = capacity - count ; cout << "对不起,只剩" << left << "个位置。" << endl; cout << "1.输入u,更新(update)预定信息。" << endl; cout << "2.输入q,退出预定。" << endl; cout << "你的选择:"; cin >> ch; if (ch == 'q') { cout << endl;break; } else continue; } } cout << "截至目前,一共有" << count << "位听众预定参与。预定听众信息如下:" << endl; for (auto item : audience_info_list) { item.print(); } }
当听众在限制人数内的测试结果:
当听众人数超载的测试结果:
实验任务六:
TextCoder.hpp:
#pragma once #include<iostream> #include<string> using namespace std; class TextCoder { private: string text; void encoder() { for (auto i = 0;i < size(text);++i) if (isalpha(text.at(i))) { if ((text.at(i) >= 'v'&&text.at(i)<='z' )||(text.at(i)<='Z'& text.at(i) >= 'V')) text.at(i) -= 21; else text.at(i) += 5; } } void decoder() { for (auto i = 0;i < size(text);++i) if (isalpha(text.at(i))) { if ((text.at(i) <= 'e'&&text.at(i)>='a') || (text.at(i)>='A'&&text.at(i) <= 'E')) text.at(i) += 21; else text.at(i) -= 5; } } public: TextCoder(string t):text{t}{} ~TextCoder() = default; string get_ciphertext() { encoder();return text; } string get_deciphertext() { decoder();return text; } };
task6.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(); }
测试结果:
实验总结:
双引号赋值一个字符串;
s[i]访问第i+1个字符;由此可见,string是类似数组的容器 ;
s.at(i)访问;
boolalpha<<(s=="nuist")字符串比较,false和true
s.length(),s.size()字符串长度;
auto pos=s.email("c")查找子串第一次出现的索引位置,失败返回string::nops;
s.substr(0,pos),s.substr(pos+1)取子串
标签:Info,string,int,text,cout,C++,数组,include,指针 From: https://www.cnblogs.com/z-zy/p/16811023.html