实验3 数组、指针与现代C++标准库
task 5 Info.hpp
#include<iostream>
#include<string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
class Info
{
public:
Info(string ni= "", string co = "", string ci="", int n0=0) :nickname{ni}, contact{co}, city{ci}, n{n0} {};
void set_nickname(string n1){ nickname=n1; }
void set_contact(string c1) { contact = c1; }
void set_city(string c2) { city = c2; }
void set_n(int n1) { n = n1; }
void print();
private:
string nickname;
string contact;
string city;
int n;
};
void Info::print()
{
cout << endl;
cout << "用户名为:" << nickname << endl;
cout << "该用户联系方式为:" << contact << endl;
cout << "该用户所在城市为:" << city << endl;
cout << "该用户的参会人数:" << n << endl<<endl;
}
task5.cpp
#include<iostream>
#include<string>
#include<vector>
#include<limits>
#include<iomanip>
#include"Info.hpp"
using namespace std;
int main() {
cout.setf(std::ios::left);
const int capacity = 100;
string a, b, c;
int n=0, count = 0,i= 0,sum=0;
vector<Info>audience_info_list(capacity);
flag:
cout << "录入信息:" << endl << endl;
cout << setw(15) << "昵称" << setw(30) << "联系方式(邮箱/手机号)" << setw(20) << "所在城市" << setw(20) << "预定参加人数" << endl;
while (cin >> a >> b >> c >> n)
{
sum += n;
if (sum <= capacity)
{
audience_info_list[count].set_nickname(a);
audience_info_list[count].set_contact(b);
audience_info_list[count].set_city(c);
audience_info_list[count].set_n(n);
count++;
}
else
{
sum -= n;
int other = 0;
other = 0;
other = capacity - sum;
cout << "对不起只剩" << other << "个座位" << endl;
string choice;
cout << "1.输入u,更新(update)预定信息。" << endl;
cout << "2.输入q,退出预定。" << endl;
cin >> choice;
cout << "你的选择:" << choice << endl;
if (choice == "q")
{
cout << "已退出预定!";
return 0;
}
else if (choice == "u")
{
cout << "重新开始预定:" << endl;
goto flag;
}
}
}
cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
while (i < count)
{
audience_info_list[i].prtint();
i++;
}
}
运行结果如下:
task6 textcoder.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;
}
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();
}
标签:std,string,int,text,void,C++,数组,include,指针 From: https://www.cnblogs.com/lkx1366070554/p/16821085.html运行结果截图如下: