实验三
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 using namespace std; 6 class Info 7 { 8 public: 9 Info() 10 { 11 nickname = "wang"; 12 contact = "123"; 13 city = "yancheng"; 14 n = 1; 15 } 16 Info(string _nickname, string _contact, string _city, int _n); 17 void print(); 18 private: 19 string nickname; 20 string contact; 21 string city; 22 int n; 23 }; 24 Info::Info(string _nickname, string _contact, string _city, int _n) :nickname{ _nickname }, contact{ _contact }, city{ _city }, n{_n } 25 { 26 } 27 void Info::print() 28 { 29 cout << std::left << setw(16) << "昵称:" << setw(12) << nickname << endl; 30 cout << std::left << setw(16) << "联系方式:" << setw(12) << contact << endl; 31 cout << std::left << setw(16) << "所在城市:" << setw(12) << city << endl; 32 cout << std::left << setw(16) << "预订人数:" << setw(4) << n << endl; 33 }
#include<iostream> #include<vector> #include"Info.h" using namespace std; int main() { const int capacity = 100; vector<Info> v; int s = 0; cout << "昵称 " << "联系方式 " << "所在城市 " << "参加人数" << endl; while (1) { string s1, s2, s3; int k; cin >> s1 >> s2 >> s3 >> k; if (capacity - s < k) { string g; cout << "对不起,只剩" << capacity - s << "个位置。" << endl; cout << "1.输入u,更新预订信息" << endl; cout << "2.输入q,退出预订" << endl; cout << "你的选择:"; cin >> g; if (g == "u") { cin >> s1 >> s2 >> s3 >> k; Info f(s1, s2, s3, k); v.push_back(f); s = s + k; } else break; } else { if (s1 == "0" && s2 == "0" && s3 == "0" && k == 0) break; Info f(s1, s2, s3, k); v.push_back(f); s = s + k; } } cout << "截至目前,一共有" << s << "位听众预定参加。信息如下:" << endl;; for (int i = 0; i < v.size(); i++) { v.at(i).print(); cout << endl; } return 0; }
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 class TextCoder 5 { 6 public: 7 TextCoder(string _text); 8 string get_ciphertext(); 9 string get_deciphertext(); 10 private: 11 string text; 12 void encoder(); 13 void decoder(); 14 }; 15 TextCoder::TextCoder(string _text) :text{ _text }{} 16 string TextCoder::get_ciphertext() 17 { 18 encoder(); 19 return text; 20 } 21 string TextCoder::get_deciphertext() 22 { 23 decoder(); 24 return text; 25 } 26 void TextCoder::encoder() 27 { 28 for (int i = 0; i < text.size(); i++) 29 { 30 if (text[i] >= 'a' && text[i] <= 'u') 31 { 32 text[i] = text[i] + 5; 33 } 34 else if (text[i] >= 'v' && text[i] <= 'z') 35 { 36 text[i] = text[i] - 21; 37 } 38 else if (text[i] >= 'A' && text[i] <= 'U') 39 { 40 text[i] = text[i] + 5; 41 } 42 else if (text[i] >= 'V' && text[i] <= 'Z') 43 { 44 text[i] = text[i] - 21; 45 } 46 } 47 } 48 void TextCoder::decoder() 49 { 50 for (int i = 0; i < text.size(); i++) 51 { 52 if (text[i] >= 'f' && text[i] <= 'z') 53 { 54 text[i] = text[i] - 5; 55 } 56 else if (text[i] >= 'a' && text[i] <= 'e') 57 { 58 text[i] = text[i] + 21; 59 } 60 else if (text[i] >= 'F' && text[i] <= 'Z') 61 { 62 text[i] = text[i] - 5; 63 } 64 else if (text[i] >= 'A' && text[i] <= 'E') 65 { 66 text[i] = text[i] + 21; 67 } 68 } 69 }
1 #include "Textcoder.h" 2 #include <iostream> 3 #include <string> 4 void test() 5 { 6 using namespace std; 7 string text, encoded_text, decoded_text; 8 cout << "输入英文文本: "; 9 while (getline(cin, text)) 10 { 11 encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象 12 cout << "加密后英文文本:\t" << encoded_text << endl; 13 14 decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象 15 cout << "解密后英文文本:\t" << decoded_text << endl; 16 cout << "\n输入英文文本: "; 17 } 18 } 19 int main() 20 { 21 test(); 22 }