实验三

任务五

info.hpp

#include <iostream>
#include <string>
using namespace std;
class info {
private:
    string nickname, contact, city;
    int n;
public:
    info(string name, string contact0, string city0, int n0 = 0) :nickname{ name }, contact{ contact0 }, city{ city0 }, n{ n0 }{};
    void print();
};
void info::print() {
    cout << "昵称:    \t" << nickname << endl;
    cout << "联系方式:\t" << contact << endl;
    cout << "所在城市:\t" << city << endl;
    cout << "预定人数:\t" << n << endl;
}

task.cpp

#include "info.hpp"
#include <iostream>
#include <vector>
#include <string>
int main()
{
    using namespace std;
    const int capacity=100;
    vector<info> audience_info_list;
    cout << "录入信息:"<<endl;
    cout << "昵称\t" << "联系方式\t" << "所在城市\t" << "预定参加人数\t" << endl;
    string nickname, contact, city;
    int n,x=0;
    char mychoice;
    while (cin >> nickname >> contact >> city >> n)
    {
        x+=n;
        if (x<=capacity)
        {
            info p(nickname, contact, city, n);
            audience_info_list.push_back(p);
        }
        else
        {
            cout << "对不起,只剩" << capacity-x+n << "个位置"<< endl;
            cout << "1.输入u,更新预定信息" << endl << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            cin >> mychoice;
            if (mychoice == 'q')
                break;
            else
            {
                x -= n;
                continue;
            }
        }
    }
    if(x<=capacity)
        cout << "截至目前,一共有" << x << "位听众参加。预约听众信息如下:"<<endl;
    else
        cout << "截至目前,一共有" << x-n << "位听众参加。预约听众信息如下:" << endl;
    for (int i = 0; i < audience_info_list.size(); i++) 
    {
        audience_info_list.at(i).print();
        cout << endl;
    }

}

 

 

 

 实验六

textcoder.hpp

class textcoder {
private:
    string text;
    void encoder();
    void decoder();
public:
    textcoder(string text0):text{text0}{}
    string get_ciphertext();
    string get_deciphertext();
};
void textcoder::encoder() {
    for (auto i = 0;i < size(text);i++)
        if (text.at(i) >= 'a' && text.at(i) <= 'u')
            text.at(i) += 5;
        else if (text.at(i) >= 'v' && text.at(i) <= 'z')
            text.at(i) -= 21;
        else if (text.at(i) >= 'A' && text.at(i) <= 'U')
            text.at(i) += 5;
        else if (text.at(i) >= 'V' && text.at(i) <= 'Z')
            text.at(i) -= 21;
}
void textcoder::decoder() {
    for (auto i = 0;i < size(text);i++)
        if (text.at(i) >= 'f' && text.at(i) <= 'z')
            text.at(i) -= 5;
        else if (text.at(i) >= 'a' && text.at(i) <= 'e')
            text.at(i) += 21;
        else if (text.at(i) >= 'F' && text.at(i) <= 'Z')
            text.at(i) -= 5;
        else if (text.at(i) >= 'A' && text.at(i) <= 'E')
            text.at(i) += 21;
}
string textcoder::get_ciphertext() {
    encoder();
    return text;
}
string textcoder::get_deciphertext() {
    decoder();
    return text;
}

task.cpp

#include <iostream>
#include <string>
#include "textcoder.hpp"

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-23 14:54  淮左谪仙  阅读(3)  评论(0)    收藏  举报