实验三

info.hpp

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class info
{
    private:
        string nickname,contact,city;
        int n;
    public:
        info(string NICKNAME,string CONTACT,string CITY,int N):nickname{NICKNAME},contact{CONTACT},city{CITY},n{N} { } ;
        void print() const
        {
            cout<<"昵称:       "<<nickname<<endl;
            cout<<"联系方式:   "<<contact<<endl;
            cout<<"所在城市:   "<<city<<endl;
            cout<<"预订人数:   "<<n<<endl;
        }
};

task5

#include"info.hpp"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
static int k=0;
void test()
{
    const int capacity=100;
    vector<info>audience_info_list;
    string nickname0,contact0,city0;
    char input;
    int n,sy;
    cout<<"录入信息:"<<endl<<endl;
    cout<<"昵称      "<<"联系方式(邮箱/手机)     "<<"所在城市    "<<"预定参加人数"<<endl;
    while(cin>>nickname0)
    {
        cin>>contact0>>city0>>n;
        info yh(nickname0,contact0,city0,n);
        sy=capacity-k;
        k=k+n;
        if(sy==0)
        {
            cout<<"Sorry,没有空位了,请下次预定。"<<endl; 
            break;
        }
        if(k>capacity)
        {
            cout<<"对不起,只剩"<<sy <<"个位置"<<endl;
            cout<<"1.输入u,更新(update)预定信息"<<endl;
            cout<<"2.输入q,退出预定"<<endl;
            cout<<"你的选择"<<endl; 
            cin>>input;
            cout<<endl;
            if(input=='q')
            {
                k=k-n;
                break;
            }
            else if(input=='u')
            {
                k=k-n;
                continue;
            }
        }
        else
        {
            audience_info_list.push_back(yh);
        }
    }
    cout<<endl;
    cout<<"截至目前,一共有"<<k<<"位听众预定参加。预订信息如下:"<<endl;
    for(auto &result:audience_info_list)
    {
        result.print();
        cout<<endl;
    }
}
int main()
{
    test();    
}

 

运行结果

 textcoder.hpp

#include<iostream>
#include<string>
using namespace std;
class TextCoder
{
    private:
        string text;
        void encoder();
        void decoder();
    public:
        TextCoder(string TEXT): text{TEXT} { }; 
        string get_ciphertext();
        string get_deciphertext();
};
void TextCoder::encoder()
{
    for(auto i=0;i<text.length();i++)
    {
        if(text[i]>='a'&&text[i]<='z')
        {
            text[i]+=5;
            if(text[i]>'z')
            {
                text[i]-=26;
            }
        }
        else if(text[i]>='A'&&text[i]<='Z')
        {
            text[i]+=5;
            if(text[i]>'Z')
            {
                text[i]-=26;
            }
        }
    }
}
void TextCoder::decoder()
{
    for(auto i=0;i<text.length();i++)
    {
        if(text[i]>='a'&&text[i]<='z')
        {
            text[i]-=5;
            if(text[i]<'a')
            {
                text[i]+=26;
            }
        }
        else if(text[i]>='A'&&text[i]<='Z')
        {
            text[i]-=5;
            if(text[i]<'A')
            {
                text[i]+=26;
            }
        }
    }
}
string TextCoder::get_ciphertext()
{
    encoder();
    return text;
}
string TextCoder::get_deciphertext()
{
    decoder();
    return text;
}

task6

#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-20 18:25  zoRing  阅读(30)  评论(0)    收藏  举报