实验3
实验任务5:
task 5.hpp:
#include"intro.hpp" #include <iostream> #include <string> #include<iomanip> #include<vector> using namespace std; int main() { string na, c, city; int count = 0; const int top = 100; int total = 0; int n; intro s; vector<intro> aud_list; while (getline(cin, na, ' ')) { getline(cin, c, ' '); getline(cin, city, ' '); cin >> n; total = total + n; if (total > top) { string ch; cout << "sorry, we have had enough audiences" << endl; cout << "enter 'u' to update or 'q' to quit" << endl; cout << "your choice:"; cin >> ch; getchar(); if (ch == "q") { total = total - n; break; } else if (ch == "u") { total = total - n; continue; } } s.set_intro(na, c, city, n); getchar(); aud_list.push_back(s); count++; } cout<<"we now have "<<total<<" "<<"audiences"<<endl; for (int i = 0; i < count; i++) { aud_list[i].print(); cout << endl; }; }
intro.hpp:
#include <iostream> #include <string> #include<iomanip> #include<vector> using namespace std; class intro { public: intro() {}; intro(string s1, string s2, string s3, int num) :nakename{ s1 }, contact{ s2 }, city{ s3 }, n{ num } {} void print() { cout << left << setw(15) << "昵称" << nakename << endl << left << setw(15) << "联系方式" << contact << endl << left << setw(15) << "所在城市" << city << endl << left << setw(15) << "预定参加人数" << n << endl; } void set_intro(string a, string b, string c, int d) { nakename = a; contact = b; city = c; n = d; }; private:string nakename; string contact; string city; int n; }
输入:xiaoming 135677 shanghai 90
scp 231231 beijing 5

输入:xiaoming 135677 shanghai 90
scp 231231 beijing 50
再输入 u
scp 231231 beijing 5

实验任务6:
textcoder.hpp
#include <iostream> #include <string> using namespace std; class TextCoder { public: TextCoder(string s1):text{s1}{} void print() { cout << text; } string get_ciphertext(){ encoder(text); return text; } string get_deciphertext() { decoder(text); return text; } private: string text; void encoder(string a){ for(int i=0;i< a.length();i++) {if(a[i] == 'U') { a[i] = 'Z'; } else if (text[i] >= 'A' && text[i] <= 'Z') { a[i] = (a[i] - 64 + 5) % 26 + 64; } else if (a[i] =='u') { a[i] = 'z'; } else if (text[i] >= 'a' && text[i] <= 'z') { a[i] = (a[i] - 96 + 5) % 26 + 96; } else { continue; } } text = a; } void decoder(string a){ for (int i = 0; i < a.length(); i++) {if (a[i] == 'Z') { a[i] = 'U'; } else if (a[i] == 'E') { a[i] = 'Z'; } else if (text[i] >= 'A' && text[i] <= 'Z') { a[i] = (a[i] - 64 + 21) % 26 + 64; } else if (a[i] == 'e') { a[i] = 'z'; } else if (a[i] == 'z') { a[i] = 'u'; } else if (text[i] >= 'a' && text[i] <= 'z') { a[i] = (a[i] - 96 + 21) % 26 + 96; } else { continue; } } text = a; } };
task6.cpp
#include "textcoder.hpp" #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(); }

总结:
字母加密的算法公式: 对于小写字母(a的AS码为65):a[i] = (a[i] + 26 +(向后位移个数) - 1 - 64) % 26 + 1 + 64;

浙公网安备 33010602011771号