实验三

task5

Info.hpp

#pragma once
#include<iostream>
#include<string>
#include<iomanip>

using std::cout;
using std::endl;
using std::setw;
using std::left;
using std::string;

class Info
{
    public:
        Info(string s1,string s2,string s3,int num):
            nickname{s1},contact{s2},city{s3},n{num}{}

        void print() const;
    private:
        string nickname,contact,city;
        int n;
};
void Info::print()const
{
    cout << left << setw(10) << "呢称:" << left << setw(10) << nickname << endl;
    cout << left << setw(10) << "联系方式:" << left << setw(10) << contact << endl;
    cout << left << setw(10) << "所在城市:" << left << setw(10) << city << endl;
    cout << left << setw(10) << "预定人数:" << left << setw(10) << n<< endl;
}

  tesk5.cpp

#include"info.hpp"
#include<iostream>
#include<vector>

using std::cout;
using std::cin;
using std::vector;
using std::string;

int main()
{
	const int capacity = 100;
	vector<Info> audience_info_list;
	int sum = 0;

	cout << "录入信息:" << endl;

	while (1)
	{
		cout << endl;
		string nickname, contact, city;
		int n, num;
		cout << "呢称:"; cin >> nickname;
		cout << "联系方式(邮箱/手机号):"; cin >> contact;
		cout <<  "所在城市:"; cin>> city;
		cout << "预定参加人数:"; cin >> n;
		cout << endl;
		while (audience_info_list.size() + n > capacity)
		{
			char choice;
			cout << "对不起,只剩" << capacity - audience_info_list.size() << "个位置." << endl;
			cout << "1.输入u,更新预定情况" << endl;
			cout << "2.输入q,退出预定" << endl;
			cout << "你的选择:";
			cin >> choice;
			if (choice == 'q')
				break;
			if (choice == 'u')
			{
				cout << endl;
				cout << "预定参加人数:";
				cin >> n;
				cout << "预定人数已更新" << endl;
			}
		}
		sum = sum + n;
		cout << endl;
		audience_info_list.push_back(Info(nickname, contact, city, n));
		if (sum == capacity)
			break;
		cout << "输入 1 继续输入,输入 0 停止输入" << endl;
		cout << "你的选择:"; cin >> num;
		if (num == 0)
			break;
		
	}
	cout << endl;
	cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下" << endl;
	for (int i = 0; i < audience_info_list.size(); i++)
	{
		audience_info_list.at(i).print();
		cout << endl;
	}

}

  实验结果

 

task6

TextCoder

#pragma once
#include<iostream>
#include<string>

using std::string;

class TextCoder
{
    public:
        TextCoder(string s):text{s}{}

        string get_ciphertext() { encoder(); return text; }
        string get_deciphertext() { decoder(); return text; }

    private:
        string text;
        void encoder();
        void decoder();
};
void TextCoder::encoder()
{
    for (int i = 0; i < text.length(); ++i)
    {
        if (text[i] >= 'a' && text[i] <= 'z')
            text[i] = (text[i] - 'a' + 5) % 26 + 'a';
        if (text[i] >= 'A' && text[i] <= 'Z')
            text[i] = (text[i] - 'A' + 5) % 26 + 'A';
    }
}
void TextCoder::decoder()
{
    for (int i = 0; i < text.length(); ++i)
    {
        if (text[i] >= 'a' && text[i] <= 'z')
            text[i] = (text[i] - 'a' -5 + 26) % 26 + 'a';
        if (text[i] >= 'A' && text[i] <= 'Z')
            text[i] = (text[i] - 'A' - 5 + 26) % 26 + 'A';
    }
}

  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();
}

  实验结果

 

posted @ 2022-10-19 19:32  gosj  阅读(56)  评论(0)    收藏  举报