5.实验任务5

info.hpp

#pragma once

#include <iostream>
#include <string>
#include <iomanip>
using std::string;
using std::cout;
using std::endl;

class info
{
private:
    string nickname;
    string contact;
    string city;
    int n;

public:
    info(string Ni = "", string Co = "", string Ci = "", int N = 0);
    ~info();
    void print();
};

info::info(string Ni, string Co, string Ci, int N) : nickname(Ni), contact(Co), city(Ci), n(N) {}

info::~info() {}

void info::print()
{
    cout << std::setw(20) << std::left << "昵称: " << nickname << endl;
    cout << std::setw(20) << std::left << "联系方式: " << contact << endl;
    cout << std::setw(20) << std::left << "所在城市: " << city << endl;
    cout << std::setw(20) << std::left << "预订人数: " << n << endl;
}

task5.cpp

#include "info.hpp"
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    const int capacity = 100;
    vector<info> audience_info_list;
    string nickname, contact, city;
    int n, sum = 0;
    
    cout << "录入信息: " << endl << endl;
    cout << "昵称\t" << "联系方式(邮箱/手机号)\t" << "所在城市\t" << "预定参加人数\t" << endl;
    while (cin >> nickname >> contact >> city >> n)
    {
        sum += n;
        if (sum <= capacity)
        {
            audience_info_list.push_back(info(nickname, contact, city, n));
        }
        else
        {
            sum -= n;
            cout << "对不起, 只剩" << capacity - sum << "个位置。" << endl;
            cout << "1. 输入u, 更新(update)预定信息" << endl;
            cout << "2. 输入q, 退出预定" << endl;
            cout << "你的选择: ";
            string S;
            cin >> S;
            if (S == "q")
            {
                break;
            }else if (S == "u")
            {
                continue;
            }
        }
    }
    cout << endl;
    cout << "截止目前,一共有" << sum << "位听众预定参加。" << "预定听众信息如下: " << endl;

    for (int i = 0; i < audience_info_list.size(); i++)
    {
        audience_info_list.at(i).print();
        cout << endl;
    }
}

测试1:

测试2:

6.实验任务6

TextCoder.hpp

#pragma once

#include <iostream>
#include <string>
using namespace std;

#include <algorithm>

class TextCoder
{
private:
    string text;

public:
    TextCoder(string T);
    ~TextCoder();

    string get_ciphertext();
    string get_deciphertext();

private:
    void encoder();
    void decoder();
};

TextCoder::TextCoder(string T) : text(T) {}

TextCoder::~TextCoder() {}

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

测试:

 

posted on 2022-10-23 11:52  Duelist  阅读(18)  评论(0)    收藏  举报