实验五
hpp
#pragma once #include<iostream> #include<iomanip> #include<string> using namespace std; class Info { public: Info(string name0,string contact0,string city0,int n):nickname(name0),contact(contact0),city(city0),n(n){} void print(); private: string nickname, contact, city; int n; }; void Info::print() { cout << setw(10) << "昵称:\t" << nickname << endl; cout << setw(10) << "联系方式:\t" << nickname << endl; cout << setw(10) << "所在城市:\t" << nickname << endl; cout << setw(10) << "预订人数:\t" << n<< endl; }
cpp
#include"info.hpp" #include<iostream> #include<string> #include<vector> using namespace std; int main() { const int capacity = 100; vector<Info> audience_info_list; cout << "录入信息" << endl; cout << endl; cout << "昵称\t"; cout << "联系方式\t"; cout << "所在城市\t"; cout << "预订人数\t" << endl; string nickname, contact, city; int num, sum = 0; while (cin >> nickname) { cin >> contact >> city >> num; sum += num; if (capacity - sum > 0) { Info p = Info(nickname, contact, city, sum); audience_info_list.push_back(p); } else { sum -= num; char c; cout << "对不起,只剩" << capacity - sum << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预订" << endl; cout << "你的选择:"; cin >> c; cout << endl; if (c == 'q') break; else if (c == 'u') { cout << "请更新您的预定信息" << endl; continue; } } } cout << "截至目前,一共有" << sum << "名听众预定参加.预定听众信息于下:" << endl; for (int i = 0; i < audience_info_list.size(); i++) { audience_info_list.at(i).print(); cout << endl; } }
运行结果
实验6
hpp
#pragma once #include<iostream> #include<string> using namespace std; class textcoder { public: textcoder(string text0) :text{ text0 } {} textcoder(textcoder& t) :text{ t.text } {} string get_ciphertext(); string get_deciphertext(); private: string text; void encoder(); void decoder(); }; void textcoder::encoder() { for (int i = 0; i < text.size(); i++) { if (isupper(text.at(i))) text.at(i) = (text.at(i) + 5 - 65) % 26 + 65; else if (islower(text.at(i))) text.at(i) = 97 + (text.at(i) + 5 - 97) % 26; } } void textcoder::decoder() { for (int i = 0; i < text.size(); i++) { if (isupper(text.at(i))) text.at(i) = (text.at(i) - 65 - 5 + 26) % 26 + 65; else if (islower(text.at(i))) text.at(i) = (text.at(i) - 97 - 5 + 26) % 26 + 97; } } string textcoder::get_ciphertext() { encoder(); return text; } string textcoder::get_deciphertext() { decoder(); return text; }
cpp
#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(); }
标签:string,int,text,void,实验,include,textcoder From: https://www.cnblogs.com/zqpqaz151034/p/16826225.html