实验三
## 实验五
### info.hpp
#pragma once
#include <iostream>
using namespace std;
class Info{
public:
Info(string name,string cont,string city,int n){
this->nickname = name;
this->contact = cont;
this->city = city;
this->n = n;
size += n;
}
public:
string nickname;
string contact;
string city;
int n;
static int size; //当前容量
};
int Info::size = 0;
### task5.cpp
#include "info.hpp"
#include <vector>
#include <algorithm>
const int capacity = 100;
vector<Info> audience_info_list;
void show_info(){
cout << "截止目前,一共有" << Info::size << "位听众预定参加。预定信息如下:" << endl;
for(vector<Info>::iterator it = audience_info_list.begin(); it!=audience_info_list.end(); it++) {
cout << "昵称: " << it->nickname << endl;
cout << "联系方式: " << it->contact << endl;
cout << "所在城市: " << it->city << endl;
cout << "预定人数: " << it->n << endl;
}
cout << endl;
puts("");
}
void add_info(string nickname,string contact,string city,int n){
if(n+Info::size <=capacity){
Info inf(nickname, contact, city, n);
audience_info_list.push_back(inf);
cout << "恭喜您!预约成功~~" << endl;
return;
}else {
char c;
cout << "人数剩余不足~~还剩 " << capacity - Info::size << " 人" << endl;
cout << "1.输入u更新预定人数:" << endl;
cout << "2.输入q退出预定:" << endl;
cout << "你的选择:" << endl;
cin >>c;
if(c=='q'){
show_info();
return;
}
else if(c=='u')
{
cout << "更新人数:" << endl;
cin >> n;
if(n+Info::size <=capacity){
cout << " 更新成功" << endl;
Info inf(nickname, contact, city, n);
audience_info_list.push_back(inf);
return;
}
else{
cout << "错误指令!!" << endl;
return;
}
}
}
}
int main(){
cout << "录入信息:" << endl << endl;
cout << "输入1录入信息 输入2显示当前录入人数:" << endl;
int op;
while(cin >> op){
if(op==1){
string nickname;
string contact;
string city;
int n;
cout << "昵称 " << "联系方式(邮箱/手机号) " << "所在城市 " << "预定参加人数 " << endl;
cin >> nickname >> contact >> city >> n;
add_info(nickname, contact, city, n);
}else if (op == 2) {
show_info();
}else {
cout << "错误指令!" << endl;
break;
}
}
return 0;
}
## task 6
### textcoder.hpp
#pragma once
#include <iostream>
using namespace std;
class TextCoder{
public:
TextCoder(string text) : text(text) {}
string get_ciphertext(){
encoder();
return text;
}
string get_deciphertext(){
decoder();
return text;
}
private:
// 2022, oop and c plus plus
void encoder(){
for (int i = 0; i <text.length(); i++){
if(text[i] >='a' && text[i] <='u') text[i]+=5;
else if(text[i] >='v' && text[i] <='z') text[i]-=21;
else if(text[i] >='A' && text[i] <='U') text[i]+=5;
else if(text[i] >='V' && text[i] <='Z') text[i] -=21;
else
;
}
}
void decoder() {
for (int i = 0; i <text.length(); i++){
if(text[i] >='f' && text[i] <='z') text[i]-=5;
else if(text[i] >='a' && text[i] <='e') text[i]+=21;
else if(text[i] >='F' && text[i] <='Z') text[i]-=5;
else if(text[i] >='V' && text[i] <='Z') text[i] +=21;
else
;
}
}
private:
string text;
};
### 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();
}