实验任务5
info部分:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class info {
public:
info(string nick, string cont, string ci, int m) :nickname{ nick }, contact{ cont }, city{ ci }, n{ m } {};
void print();
private:
string nickname,contact,city;
int n = 0;
};
void info::print() {
cout << "昵称:" << setw(7) << nickname << endl;
cout << "联系方式:" << setw(7) << contact << endl;
cout << "所在城市:" << setw(7) << city << endl;
cout << "预定人数:" << setw(7) << n << endl;
cout << endl;
}
task5:
#include "info.hpp"
#include <iostream>
#include<vector>
#include<iomanip>
int main() {
const int capacity = 100;
vector<info>audience_info_list ;
string nickname, contact, city;
int sum_people = 0,yuding=0;
char choice;
cout << "录入信息:" << endl;
cout << "昵称" << " 联系方式(邮箱/手机号)" <<" 所在城市" <<" 预定参加人数" << endl;
while (cin >> nickname)
{
cin >> contact >> city >> yuding;
sum_people +=yuding;
if (sum_people > capacity)
{
cout << "对不起,只剩下:" << capacity - (sum_people - yuding) << "个位置了." << endl;;
cout << "1.输入u,更新(update)预定信息." << endl;
cout << "2.输入q,退出预定." << endl;
cout << "你做出的选择是:" ;
cin >> choice;
if (choice == 'u')
{
sum_people -= yuding;
continue;
}
else if (choice == 'q')
break;
}
info x(nickname,contact,city,yuding);
audience_info_list.push_back(x);
}
if(sum_people>capacity)
cout << "截至目前,一共有" << sum_people-yuding << "位听众预定参加,预定听众信息如下:" << endl;
else
cout << "截至目前,一共有" << sum_people << "位听众预定参加,预定听众信息如下:" << endl;
for (auto i = 0; i < audience_info_list.size(); ++i)
audience_info_list[i].print();
}
![]()
![]()
![]()
#include<iostream>
#include<string>
using namespace std;
class TextCoder {
public:
TextCoder(string text1) :text{ text1 } {}
string get_ciphertext() { encoder(); return text; }
string get_deciphertext() { decoder(); return text; }
private:
string text;
void encoder() {
for (int i = 0; i < text.size(); i++)
{
if (text[i] >= 'a' && text[i] <= 'u')
text[i] += 5;
else if (text[i] > 'u' && text[i] <= 'z')
text[i] -= 21;
if (text[i] >= 'A' && text[i] <= 'U')
text[i] += 5;
else if (text[i] > 'U' && text[i] <= 'Z')
text[i] -= 21;
}
}
void decoder()
{
for (int i = 0; i < text.size(); i++)
{
if (text[i] >= 'f' && text[i] <= 'z')
text[i] -= 5;
else if (text[i] < 'f' && text[i] >= 'a')
text[i] += 21;
if (text[i] >= 'F' && text[i] <= 'Z')
text[i] -= 5;
else if (text[i] < 'F' && text[i] >= 'A')
text[i] += 21;
}
}
};
上面的代码是textcoder.hpp
task6:
#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();
}
![]()
![]()