实验任务5
#include<iostream> #include<string> using namespace std; class TextCoder { public: TextCoder() = default; TextCoder(string str); string get_ciphertext(); string get_deciphertext(); ~TextCoder() = default; private: string text; void encoder(); void decoder(); }; TextCoder::TextCoder(string str) { text = str; } string TextCoder::get_ciphertext() { encoder(); return text; } string TextCoder::get_deciphertext() { decoder(); return text; } void TextCoder::encoder() { for (int i = 0; i < text.length(); i++) { if (islower(text[i])) { text.replace(i, 1, 1, (text[i] - 'a' + 7) % 26 + 'a'); } else if (isupper(text[i])) { text.replace(i, 1, 1, (text[i] - 'A' + 7) % 26 + 'A'); } } } void TextCoder::decoder() { for (int i = 0; i < text.length(); i++) { if (islower(text[i])) { text.replace(i, 1, 1, (text[i] - 'a' + 26 - 7) % 26 + 'a'); } else if (isupper(text[i])) { text.replace(i, 1, 1, (text[i] - 'A' + 26 - 7) % 26 + 'A'); } } }
#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(); }
实验任务6
#include<iostream> #include<string> #include<vector> using namespace std; class Info { public: Info(string name, string contact, string city, int n); void print(); private: string name, contact, city; int n; }; Info::Info(string name, string contact, string city, int n) { name = name0; contact = contact0; city = city0; n = n0; } void Info::print() { cout << "昵称: " << name << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout<<"预定人数: " << n << endl; }
#include<iostream> #include<string> #include<vector> #include"info.hpp" int main() { string a, b, c; int d,sum=0; int const capacitd = 100; vector<Info> x; cout << "录入信息:" << endl<<endl; cout << "昵称\t " << "联系方式(邮箱/手机号)\t " << "所在城市\t " << "预定参加人数\t" << endl; for ( sum ;;) { cin >> a >> b>> c >> d; Info z(a, b, c, d); int y = sum; if (sum+d > capacitd) { cout << "对不起,只剩" << capacitd - y << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入o,退出(out)预定" << endl; char s; cout << "您的选择:"; cin >> s; if (s == 'u') continue; cout << endl; break; } else { sum += d; x.push_back(z); } } cout << "截至目前,一共有" << sum << "听众预定参加。预定听众信息如下。" << endl; for (int i = 0; i < x.size(); i++) { x[i].print(); } }
标签:string,int,text,void,实验,include,TextCoder From: https://www.cnblogs.com/bjdhjctl/p/17868787.html