实验任务五cpp
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include "info.hpp"
using namespace std;
int main() {
const int capacity = 100;
vector<info> audience_info_list;
string name, con, city;
int reserve_n;
int reserved_n = 0, left_n = capacity;
cout << "录入信息:" << endl << endl;
cout << left << setw(15) << "昵称"
<< setw(15) << "联系方式(邮箱/手机号)"
<< setw(15) << "所在城市"
<< setw(15) << "预定参加人数" << endl;
while (cin >> name) {
cin >> con >> city >> reserve_n;
reserved_n += reserve_n;
left_n -= reserve_n;
if (left_n < 0) {
char choice;
cout << endl;
cout << "对不起,只剩" << left_n + reserve_n << "个位置." << endl;
cout << "1.输入u,更新(update)预定信息" << endl;
cout << "2.输入q,退出预定" << endl;
cout << "你的选择:";
reserved_n -= reserve_n;
left_n += reserved_n;
cin >> choice;
if (choice == 'u')
continue;
if (choice == 'q')
break;
}
else {
info audience(name, con, city, reserve_n);
audience_info_list.push_back(audience);
}
}
cout << endl;
cout << "截至目前,一共有" << reserved_n << "位听众预定参加。";
cout << "预定听众信息如下:" << endl;
for (auto audience_info : audience_info_list) {
audience_info.print();
}
}
hpp
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class info {
public:
info(string nickname0 = " ", string contact0 = " ", string city0 = " ", int n0 = 0);
~info() = default;
void print() const;
private:
string nickname;
string contact;
string city;
int n;
};
// 函数定义
info::info(string nickname0, string contact0, string city0, int n0) :nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ n0 } {}
void info::print() const {
cout << setw(15) << "昵称:"
<< setw(15) << nickname << endl;
cout << setw(15) << "联系方式:"
<< setw(15) << contact << endl;
cout << setw(15) << "所在城市:"
<< setw(15) << city << endl;
cout << setw(15) << "预订人数:"
<< setw(15) << n << endl;
cout << endl;
}
实验6cpp
#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();
}
hpp
#pragma once
#include<iostream>
#include<string>
using namespace std;
class TextCoder
{
public:
TextCoder(string c1) :text(c1) {}
string get_ciphertext();
string get_deciphertext();
private:
string text;
void encoder();
void decoder();
};
void TextCoder::encoder() {
int n = text.length();
for (int i = 0; i < n; i++)
{
if (text[i] >= 'a' && text[i] <= 'z')
{
if (text[i] + 5 <= 'z')
{
text[i] = text[i] + 5;
}
else {
text[i] = text[i] - 21;
}
}
else if (text[i] >= 'A' && text[i] <= 'Z')
{
if (text[i] + 5 <= 'Z')
{
text[i] = text[i] + 5;
}
else {
text[i] = text[i] - 21;
}
}
}
}
void TextCoder::decoder() {
int n = text.length();
for (int i = 0; i < n; i++)
{
if (text[i] >= 'a' && text[i] <= 'z')
{
if (text[i] - 5 >= 'a')
{
text[i] = text[i] - 5;
}
else {
text[i] = text[i] + 21;
}
}
else if (text[i] >= 'A' && text[i] <= 'Z')
{
if (text[i] - 5 >= 'A')
{
text[i] = text[i] - 5;
}
else {
text[i] = text[i] + 21;
}
}
}
}
string TextCoder::get_ciphertext() {
encoder();
return text;
}
string TextCoder::get_deciphertext() {
decoder();
return text;
}
标签:info,string,int,text,void,实验,include
From: https://www.cnblogs.com/2003dingjiajie/p/16823652.html