实验任务5
实验代码:
textcoder.hpp
#pragma once
#include <iostream>
#include <vector>
#include <array>
#include <string>
using namespace std;
class TextCoder
{
private:
string text;
void encoder()
{
for(auto i=0; i<text.size(); i++) {
if(text.at(i) >= 'a' && text.at(i) <= 'z') {
text.at(i) = 'a' + (text.at(i) - 'a' + 7) % 26;
}
else if(text.at(i) >= 'A' && text.at(i) <= 'Z') {
text.at(i) = 'A' + (text.at(i) - 'A' + 7) % 26;
}
}
}
void decoder()
{
for(auto i=0; i<text.size(); i++) {
if(text.at(i) >= 'a' && text.at(i) <= 'z') {
text.at(i) = 'a' + (text.at(i)-'a' +26- 7) % 26;
}
else if(text.at(i) >= 'A' && text.at(i) <= 'Z') {
text.at(i) = 'A' + (text.at(i) -'A'+26 - 7) % 26;
}
}
}
public:
string get_ciphertext()
{
encoder();
return text;
}
string get_deciphertext()
{
decoder();
return text;
}
TextCoder(string a):text{a}{}
TextCoder(const TextCoder& b):text{b.text}{}
};
textcoder.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();
}
实验结果:
实验任务6
实验代码:
info.hpp
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Info
{
public:
Info(string name, string contact, string city, int n);
void print();
private:
string name, contact, city;
int n;
};
Info::Info(string name, string contact, string city, int n)
{
this->name = name; this->contact = contact; this->city = city; this->n = n;
}
void Info::print()
{
cout << "昵称: " << name << endl;
cout << "联系方式: " << contact << endl;
cout << "所在城市: " << city << endl;
cout<<"预定人数: " << n << endl;
}
.cpp
#include<iostream>
#include<string>
#include<vector>
#include"info.hpp"
int main()
{
string a, b, c; int d,sum=0;
int const capacitd = 100;
vector<Info> x;
cout << "录入信息:" << endl<<endl;
cout << "昵称\t " << "联系方式(邮箱/手机号)\t " << "所在城市\t " << "预定参加人数\t" << endl;
for ( sum ;;)
{
cin >> a >> b>> c >> d;
Info z(a, b, c, d);
int y = sum;
if (sum+d > capacitd)
{
cout << "对不起,只剩" << capacitd - y << "个位置" << endl;
cout << "1.输入u,更新(update)预定信息" << endl;
cout << "2.输入o,退出(out)预定" << endl; char s;
cout << "您的选择:"; cin >> s;
if (s == 'u') continue; cout << endl;
break;
}
else
{
sum += d;
x.push_back(z);
}
}
cout << "截至目前,一共有" << sum << "听众预定参加。预定听众信息如下。" << endl;
for (int i = 0; i < x.size(); i++)
{
x[i].print();
cout<<endl;
}
}
实验结果:
标签:26,cout,int,text,实验,include,string From: https://www.cnblogs.com/lwjddd/p/17868124.html