task 3
#pragma once #include<iostream> #include<string> #include <iomanip> using namespace std; class Info { public: Info() {} Info(string _nickname, string _contact, string _city, int _n) :nickname{ _nickname }, contact{ _contact }, city{ _city }, n{ _n } {} ~Info() {} void print(); void set_nickname(string s1) { nickname = s1; } void set_contact(string s1) { contact = s1; } void set_city(string s1) { city = s1; } void set_n(int m) { n = m; } private: string nickname; string contact; string city; int n; }; void Info::print() { cout << "昵称:" << "\t" << nickname << endl; cout << "联系方式:" << "\t" << contact << endl; cout << "所在城市:" << "\t" << city << endl; cout << "预定人数:" << "\t" << n << endl << endl; }
#include <iostream> #include"info.h" #include <string> #include <vector> #include <iomanip> using namespace std; int main() { const int capacity = 100; string name, city, contact; string choice; int n, i = 0, flag = 1, m; int sum = 0; vector<Info>audience_info_list(100); cout << "录入信息:" << endl << endl; cout << "昵称" << "\t" << "联系方式:" << "\t" << "所在城市:" << "\t" << "预定参加人数:" << "\t" << endl; while (cin >> name >> contact >> city >> n) { if (n<=capacity -sum) { audience_info_list[i].set_nickname(name); audience_info_list[i].set_contact(contact); audience_info_list[i].set_city(city); audience_info_list[i].set_n(n); i++; sum += n; } else { cout << "对不起,只剩" << capacity - sum << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; while (cin >> choice) { if (choice == "q") { flag = 0; break; } else if (choice == "u") { cout << "昵称" << "\t" << "联系方式:" << "\t" << "所在城市:" << "\t" << "预定参加人数:" << "\t" << endl; cin >> name >> contact >> city >> n; if (n > capacity - sum) { cout << "对不起,只剩" << capacity - sum << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; continue; } else { audience_info_list[i].set_nickname(name); audience_info_list[i].set_contact(contact); audience_info_list[i].set_city(city); audience_info_list[i].set_n(n); i++; sum += n; break; } } } } if (flag == 0) break; } m = i; for (i = 0; i <m; i++) { audience_info_list[i].print(); } return 0; }
#pragma once #include<iostream> #include<string> using namespace std; class TextCoder { public: TextCoder(string test0):text{ test0 }{} string get_ciphertext(); string get_deciphertext(); private: string text; void encoder(); void decoder(); }; void TextCoder::encoder() { int num = text.length(); for (int i = 0; i < num; i++) { if (text.at(i) >= 'a' && text.at(i) <= 'u') text.at(i) += 5; else if (text.at(i) >= 'v' && text.at(i) <= 'z') text.at(i) -= 21; else if (text.at(i) >= 'A' && text.at(i) <= 'U') text.at(i) += 5; else if (text.at(i) >= 'v' && text.at(i) <= 'Z') text.at(i) -= 21; } } void TextCoder::decoder() { int num = text.length(); for(int i=0;i<num;i++) { if (text.at(i) >= 'f' && text.at(i) <= 'z') { text.at(i) = text.at(i) - 5; } else if (text.at(i) >= 'a' && text.at(i) <= 'e') { text.at(i) = text.at(i) + 21; } else if (text.at(i) >= 'F' && text.at(i) <= 'Z') { text.at(i) = text.at(i) - 5; } else if (text.at(i) >= 'A' && text.at(i) <= 'E') { text.at(i) = text.at(i) + 21; } } } string TextCoder::get_ciphertext() { encoder(); return text; } string TextCoder::get_deciphertext() { decoder(); return text; }
#include <iostream> #include "textcoder.hpp" #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(); }