实验二 数组,指针与c++标准库
//Info.hpp #ifndef EMPLOYEE_HPP #define EMPLOYEE_HPP #include<iostream> #include<string> using namespace std; class Info { private: string nickname; string contact; string city; int n; public: Info(string name0, string contact0, string city0, int n0) { nickname = name0; contact = contact0; city = city0; n = n0; } void print() { cout << "称呼: \t" << nickname << endl; cout << "联系方式:\t" << contact << endl; cout << "所在城市:\t" << city << endl; cout << "预定参加人数:\t" << n << endl; } }; #endif
//task5.cpp #include"Info.hpp" #include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<Info>audience_info_list; string nickname, contact, city; int n; int count = 0;//用于统计总人数 int const capacity = 100;//音乐会场总人数 cout << "录入信息:" << endl; cout << endl; cout << "称呼(昵称),联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; while (cin >> nickname >> contact >> city >> n) { count += n; if (count > capacity) { count -= n; cout << "对不起,只剩下" << capacity - count << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; char choice; cout << "你的选择:" << endl; cin >> choice; if (choice == 'u') continue; else break; } Info v1(nickname, contact, city, n); audience_info_list.push_back(v1); } cout << endl; cout << "截至目前,一共" << count << "位听众预定参加。预定听众信息如下:" << endl; for (auto& i : audience_info_list) i.print(); }



//textcoder.hpp #ifndef EMPLOYEE_HPP #define EMPLOYEE_HPP #include<iostream> #include<string> #include<vector> using namespace std; class TextCoder { private: string text; public: TextCoder() {}; TextCoder(string text0) { text = text0; } string encoder() { for (auto& c : text) { if ((c >= 'a' && c <= 'u')||(c>='A'&&c<='U')) c += 5; else if((c>'u'&&c<='z')||(c>'U'&&c<='Z')) c-=21; } return text; } string decoder() { for (auto& c : text) { if ((c >= 'f' && c <= 'z') || (c >= 'F' && c <= 'Z')) c -= 5; else if ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) c += 21; } return text; } };
//task6.cpp #include"textcoder.hpp" #include<iostream> #include<string> int main() { using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本:"; while (getline(cin, text)) { encoded_text = TextCoder(text).encoder();//这里用的是临时无名对象 cout << "加密后的英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).decoder();//这里用的时临时无名对象 cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本:"; } }

浙公网安备 33010602011771号