实验二 数组,指针与C++标准库
1.实验5
// 任务4 作业1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <string> #include <iostream> #include <vector> 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; } void print() { cout << "称呼: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预约人数: " << n << endl; } }; int main() { vector<info> audience_info_list; string nickname, contact, city; char choice; int n; int count = 0; const int capacity = 100; cout << "录入信息:"; cout << "\n\n"; cout << "称呼/昵称: ,联系方式(邮箱/手机号): ,所在城市: ,预约参加人数: " << endl; while (cin >> nickname) { cin >> contact; cin >> city; cin >> n; if (n + count > capacity) { cout << "对不起,只剩" << capacity - count << "个位置。" << endl; cout << "1.输入u,更新(update)预约信息" << endl; cout << "2.输入q,退出预约" << endl; cout << "你的选择:"; cin >> choice; if (choice == 'q') { break; } else { cout << "请重新输入:" << endl; continue; } } count += n; audience_info_list.push_back(info(nickname, contact, city, n)); } cout << "目前一共有" << count << "位听众预约参加,预约听众信息如下: " << endl; for (auto i = audience_info_list.begin(); i != audience_info_list.end(); i++) { i->print(); } }
实验6:
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include <string> #include <vector> #include <iostream> using namespace std; class TextCoder { private: string text; public: TextCoder(string txt) { text = txt; } string encoder(); string decoder(); }; string TextCoder::encoder() { for (auto& ch : text) { if (ch >= 'v' && ch <= 'z' || ch >= 'V' && ch <= 'Z') { ch = (char)(ch - 21); } if (ch >= 'a' && ch <= 'u' || ch >= 'A' && ch <= 'U') { ch = (char)(ch + 5); } } return text; } string TextCoder::decoder() { for (auto& ch : text) { if (ch >= 'a' && ch <= 'e' || ch >= 'A' && ch <= 'E') { ch = (char)(ch + 21); } if (ch >= 'f' && ch <= 'z' || ch >= 'F' && ch <= 'Z') { ch = (char)(ch - 5); } } return text; } #endif
#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输入英文文本: "; } }