task5:
//info.hpp
#include<iostream> #include<string> using namespace std; class info { private: string nickname, contact, city; int n; static int a; public: info(string ni, string co, string ci, int nn) :nickname(ni), contact(co), city(ci), n(nn) { a += n; } info() :nickname("n"), contact("co"), city("ci"), n(0) {} void print()const; static void printa(); static int geta() { return a; } }; int info::a = 0; void info::print()const { cout << "昵称: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "城市: " << city << endl; cout << "预定人数: " <<n<<endl << endl; } void info::printa() { cout << "截至目前,一共有" << a << "位听众预定参加。预定听众信息如下:"<<endl; }
//task5.cpp
#include<iostream> #include<string> #include<iomanip> #include"info.hpp" using namespace std; const int capacity = 100; int main() { string ni, co, ci, choose; int nn, f = 1, l = 0; info vector[capacity]; cout << "录入信息:" << endl << endl; cout << "昵称\t\t"<< "联系方式(邮箱/手机号)\t\t" << "所在城市\t\t" << "预定参加人数\t\t" << endl; while (cin >> ni>>co>> ci>> nn) { if (info::geta()+nn> 100) { f = 0; break; } info i(ni, co, ci, nn); vector[l] = i; l++; } if (f == 0) { cout << "对不起,只剩" << capacity - info::geta() << "个位置。" << endl << "1.输入u,更新预定信息" << endl << "2.输入q,退出预定" << endl << "你的选择:"; cin >> choose; cout << endl << endl; } info::printa(); for (int j = 0; j < l; j++) { vector[j].print(); } }
运行结果:
task6:
//textcoder.hpp
#include<iostream> #include<string> using namespace std; class TextCoder { public: TextCoder(){} TextCoder(string t):text(t){} string get_ciphertext(); string get_deciphertext(); private: string text; void encoder();//形参列表为空! void decoder(); }; void TextCoder::encoder() { for (int i = 0; i < text.length(); i++) { if (('a' <= text[i] && text[i] <= 'z') || ('A' <= text[i] && text[i] <= 'Z')) { if (('v' <= text[i] && text[i] <= 'z') || ('V' <= text[i] && text[i] <= 'Z')) { text[i] = text[i] - 21; } else { text[i] = text[i] + 5;} } } } string TextCoder::get_ciphertext() { encoder(); return text; } string TextCoder::get_deciphertext() { decoder(); return text; } void TextCoder::decoder() { for (int i = 0; i <text.length(); i++) { if (('a' <= text[i] && text[i] <= 'z') || ('A' <= text[i] && text[i] <= 'Z')) { if (('f' <= text[i] && text[i] <= 'z') || ('F' <= text[i] && text[i] <= 'Z')) { text[i] = text[i] - 5; } else { text[i] = text[i] + 21;} } } }
//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(); }
运行结果:
标签:info,ci,string,int,void,实验,include From: https://www.cnblogs.com/jww12/p/16819794.html