实验四

发布时间 2023-11-30 12:27:41作者: 干饭中,请勿打扰

实验五
TextCoder.hpp

点击查看代码
#include <string>

class TextCoder {
private:
    std::string text;

    void encoder() {
        for (char& c : text) {
            if (c >= 'a' && c <= 'z') {
                c = ((c - 'a' + 7) % 26) + 'a';
            } else if (c >= 'A' && c <= 'Z') {
                c = ((c - 'A' + 7) % 26) + 'A';
            }
        }
    }

    void decoder() {
        for (char& c : text) {
            if (c >= 'a' && c <= 'z') {
                c = ((c - 'a' + 19) % 26) + 'a';
            } else if (c >= 'A' && c <= 'Z') {
                c = ((c - 'A' + 19) % 26) + 'A';
            }
        }
    }

public:
    TextCoder() = default;
    TextCoder(const std::string& t) : text(t) {}

    std::string get_ciphertext() {
        encoder();
        return text;
    }

    std::string get_deciphertext() {
        decoder();
        return text;
    }
};

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

实验六
info.hpp

点击查看代码
#pragma once

#include <iostream>
#include <string>

using namespace std;

class Info {
    private:
        string nickname;
        string contact;
        string city;
        int n;
    public:
        Info(const string& nickname, const string& contact, const string& city, const int& n);
        void print() const;
};

Info::Info(const string& nickname, const string& contact, const string& city, const int& n): nickname{nickname}, contact{contact}, city{city}, n{n} {
}

void Info::print() const {
    cout << "昵称:\t\t" << nickname << endl;
    cout << "联系方式:\t" << contact << endl;
    cout << "所在城市:\t" << city << endl;
    cout << "预定人数:\t" << n << endl;
}

task6.cpp

点击查看代码
#include<iostream>
#include<string>
#include<vector>
#include "info.hpp"

const int capacity = 100;


int main()
{
    vector<Info> audience_info_list;

    cout << "录入信息:" << endl;
    cout << endl;
    cout << "昵称     联系方式(邮箱/手机号)     所在城市     预定参加人数     " << endl;
    char choice;
    int sum=0;
    string name, contact, city;
    int n;
    while(cin >> name >> contact >> city >> n)
    {
        sum+=n;

        if(sum > capacity)
        {
            sum-=n;
            cout << "对不起,只剩" << capacity-sum << "个位置" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl
                 << "2.输入q, 退出预定" << endl
                 <<"你的选择: ";
            cin >> choice;

            if(choice=='u')
            {
                cin >> name >> contact >> city >> n;
                Info a(name,contact,city,n);
                audience_info_list.push_back(a);
                sum+=n;
            }
            else
             break;
        }
        else audience_info_list.push_back(Info(name, contact, city, n));
    }
    cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
    for(auto &Info:audience_info_list)
    {
        Info.print();
    }
}