实验3 数组、指针与现代C++标准库
#pragma once #include<iostream> #include<string> #include<iomanip> using namespace std; class Info { public: string nickname, contact, city; int n; Info(string ni, string co, string ci, int x) { nickname = ni; contact = co; city = ci; n = x; } void print() { cout << left << setw(10) << "昵称:" << nickname<<endl; cout << left << setw(10) << "联系方式:" <<contact << endl; cout << left << setw(10) << "所在城市:" << city << endl; cout << left << setw(10) << "预定人数:" << n << endl; cout << endl; } };
#include"Info.h" #include<iostream> #include<vector> #include<limits> #include<string> #include<algorithm> using namespace std; int main() { cout << "录入信息:" << endl; cout << left << setw(10) << "昵称" << setw(20) << "联系方式(邮箱、手机号)" << setw(10) << "所在城市" << setw(10) << "预定参加人数" << endl; const int capacity = 100; int n = 0, s = 0, m = 0, i = 0; string ju; string name, contact, city; vector<Info>audience_info_list; while (cin>>name) { cin >> contact >> city; cin >> n; s += n; m++; if (s > capacity) { cout << "对不起,只剩" << (capacity - s + n) << "个位置" << endl << "1.输入u,更新(update)预定信息" << endl << "2.输入q,退出预定" << endl << "你的选择:"; cin >> ju; if (ju == "u") { s -= n; m -= 1; continue; } if (ju == "q") { s -= n; m -= 1; break; } } Info a{ name,contact,city,n }; audience_info_list.push_back(a); } cout << "截至目前,一共有" << s << "位听众预定参加。预定听众信息如下:" << endl; for (i=0;i < m;i++) { audience_info_list[i].print(); } }
6.
#pragma once #include<iostream> #include<string> using namespace std; class TextCoder { private: string text; void encoder() { for (auto& st : text) { if (st >= 'a' && st <= 'u') st += 5; else if (st >= 'A' && st <= 'U') st += 5; else if (st >= 'v' && st <= 'z') st = st - 26+ 5; else if (st >= 'V' && st <= 'Z') st = st - 26+ 5; } } void decoder() { for (auto& st : text) { if (st >= 'f' && st <= 'z') st -= 5; else if (st >= 'F' && st <= 'Z') st -= 5; else if (st >= 'a' && st <= 'e') st = st + 26 - 5; else if (st >= 'A' && st <= 'E') st = st +26 - 5; } } public: TextCoder(string t) { text = t; } TextCoder(TextCoder& obj); string get_ciphertext() { encoder(); return text; } string get_deciphertext() { decoder(); return text; } };
#include "TextCoder.h" #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(); }