实验二(数组、指针与C++标准库)
info.hpp:
#include<iostream> #include<string> using namespace std; class info { public: info(string name, string con, string cs, int m) :nickname(name),contact(con),city(cs),n(m) {} void print() { cout << "称呼: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: "<<city << endl; cout << "预定人数: " << n << endl; }; private: string nickname; string contact; string city; int n; };
task5.cpp:
#include"info.hpp" #include<iostream> #include<string> #include<vector> void main() { using namespace std; vector<info>audience_info_list; const int capacity=100; int sum = 0; int i = 0; string str1,str2,str3; char choice; int n4; cout << "录入信息:" << endl << endl; cout << "称呼/昵称 联系方式(邮箱/手机号) 所在城市 预定参加人数" << endl; while (cin>>str1) { if (str1 == "^Z") break; else{ cin >> str2 >> str3 >> n4; i++; audience_info_list.push_back(info(str1, str2, str3, n4)); sum += n4; if (sum > capacity) { sum -= n4; cout << "对不起,只剩" << capacity-sum << "个位置." << endl; cout << "1.输入u,更新(update)预定信息" << endl << "2.输入q,退出预定" << endl << "你的选择:"; cin >> choice; break; } } } cout << endl << endl; cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:"<<endl; for (int j = 0;j < i;j++) { audience_info_list[j].print(); } }


textcoder.hpp:
#include<iostream> #include<string> using namespace std; class TextCoder { public: TextCoder(string a) :text(a) {} string encoder() { string str=""; for (auto e : text) { if (e >= 'a' && e <= 'u' || e >= 'A' && e <= 'U') { e += 5;str += e; } else if (e >= 'v' && e <= 'z' || e >= 'V' && e <= 'Z') { e -= 21;str += e; } else str += e; } return str; }; string decoder() { string str = ""; for (auto e : text) { if (e >= 'f' && e <= 'z' || e >= 'F' && e <= 'Z') { e -= 5;str += e; } else if (e >= 'a' && e <= 'e' || e >= 'A' && e <= 'E') { e += 21;str += e; } else str += e; } return str; }; private: string 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)) { if (text != "^Z") { encoded_text = TextCoder(text).encoder(); cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).decoder(); cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } } }


浙公网安备 33010602011771号