实验3

Info.hpp
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;
class ReserveInfo
{
private:
    string nick_name;
    string contact;
    string city;
    int reserve_num;

public:
    ReserveInfo();
    ReserveInfo(string nick_name, string contact, string city, int reserve_num);
    ~ReserveInfo();

    int get_reserve_num() const;
    void print();
};

ReserveInfo::ReserveInfo()
{
}
ReserveInfo::ReserveInfo(string nick_name, string contact, string city, int reserve_num) : nick_name(nick_name), contact(contact), city(city), reserve_num(reserve_num)
{
}

ReserveInfo::~ReserveInfo()
{
}

int ReserveInfo::get_reserve_num() const
{
    return reserve_num;
}

void ReserveInfo::print()
{
    cout << left << endl
         << setw(15) << "\n称呼:" << setw(15) << nick_name
         << setw(15) << "\n联系方式:" << setw(15) << contact
         << setw(15) << "\n所在城市:" << setw(15) << city
         << setw(15) << "\n预定人数:" << setw(15) << reserve_num << endl;
}
Liveshow.hpp
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include "Info.hpp"
using namespace std;

// 现场表演类
class LiveShow
{
private:
    vector<ReserveInfo> reserve_list;
    const int capasity = 100;

public:
    LiveShow();
    ~LiveShow();
    int get_capacity_remain();
    void add_reserve();
    void print();
};
LiveShow::LiveShow()
{
}
LiveShow::~LiveShow()
{
}

int LiveShow::get_capacity_remain()
{
    int capacity_remain = capasity;
    for (auto const reserve_info : reserve_list)
    {
        capacity_remain -= reserve_info.get_reserve_num();
    }
    return capacity_remain;
}
void LiveShow::add_reserve()
{
    cout << "录入信息:\n\n称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数\n";
    string str_temp;
    while (cin >> str_temp)
    {
        string nick_name;
        string contact;
        string city;
        int reserve_num;
        nick_name = str_temp;
        cin >> contact >> city >> reserve_num;
        if (reserve_num > this->get_capacity_remain())
        {
            char option;
            cout << "亲爱的 " << nick_name << " 对不起,只剩" << this->get_capacity_remain() << "个位置."
                 << "\n1. 输入u,更新(update)预定信息"
                 << "\n2. 输入q,退出预定"
                 << "您的选择:";
            cin >> option;
            if (option == 'u')
            {
                continue;
            }
            if (option == 'q')
            {
                break;
            }
        }else{
            ReserveInfo reserveInfo(nick_name, contact, city, reserve_num);
            reserve_list.push_back(reserveInfo);
        }
    }
}

void LiveShow::print()
{
    for(auto reserve_info : reserve_list){
        cout << "截止目前,一共有 " << capasity-get_capacity_remain() << " 位听众预定参加,预定观众信息如下:\n";
        reserve_info.print();
    }
}
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include "LiveShow.hpp"
int main()
{
    LiveShow liveShow_bth;
    liveShow_bth.add_reserve();
    liveShow_bth.print();
    system("pause");
}

 

  

textcoder.hpp
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

class TextCoder
{
private:
    string text;

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

    void set_text(string text);
    string get_text();
    string encoder();
    string decoder();
};

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

TextCoder::~TextCoder()
{
}

void TextCoder::set_text(string text)
{
    this->text = text;
}

string TextCoder::get_text()
{
    return text;
}

string TextCoder::decoder()
{
    string text_decoded = text;
    for (int i = 0; i < text_decoded.length(); i++)
    {
    	// 负向溢出
        if (text_decoded[i] >= 65 && text_decoded[i] <= 90)
        {
            text_decoded[i] = 90 - (90-text_decoded[i] + 5)%26;
        }else if(text_decoded[i] >= 97 && text_decoded[i] <= 122){
            text_decoded[i] = 122 - (122-text_decoded[i] + 5)%26;
        }    
    }
    return text_decoded;
}

string TextCoder::encoder()
{
    string text_encoded = text;
    for (int i = 0; i < text_encoded.length(); i++)
    {
    	// 正向溢出
        if (text_encoded[i] >= 65 && text_encoded[i] <= 90)
        {
            text_encoded[i] = (text_encoded[i]-65 + 5)%26 + 65;
        }else if(text_encoded[i] >= 97 && text_encoded[i] <= 122){
            text_encoded[i] = (text_encoded[i]-97 + 5)%26 + 97;
        }    
    }
    return text_encoded;
}
test6.cpp
#include "textCoder.hpp"
#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string text, encoded_text, decoded_text;
    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;
        decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
        system("pause");
    }
}

 

  

posted @ 2022-10-25 20:22  左绪青  阅读(19)  评论(0)    收藏  举报