实验三

task5:

info.hpp

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class info{
    public:
        info(string n, string c, string ci, int n1);    
        void print()const ;
    private:
        string nickname;
        string contact;
        string city;
        int n;
};

info::info(string n, string c, string ci, int n1)  : nickname(n), contact(c), city(ci), n(n1){};    

void info::print() const
{
    cout << left << endl
           << setw(15) << "\n称呼:  " << nickname
           << setw(15) << "\n联系方式:" << setw(15) << contact
           << setw(15) << "\n所在城市:" << setw(15) << city
           << setw(15) << "\n预定人数:" << setw(15) << n << endl;
}

task5.cpp

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

using namespace std;

static int count = 0;
const int capacity = 100;

int main(){
    vector<info> audience_info_list;
    string n, c, ci;
    int n1;
    cout << "录入信息:\n\n称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数\n";
    while (cin >> n)
    {
        cin >> c >> ci >> n1;
        if (count + n1 <= capacity) 
        {
            info tmp(n, c, ci, n1);
            audience_info_list.push_back(tmp);
            count += n1;
            if (count == capacity)
                break;
        }
        else
        {
            char i;
            cout << "亲爱的 " << n << " 对不起,只剩" << capacity - count << "个位置."
                 << "\n1. 输入u, 更新(update)预定信息"
                 << "\n2. 输入q, 退出预定"
                 << "您的选择:";

            cin >> i;
            if (i == 'u')
                cout << "请重新输入:" << endl;
            if (i == 'q')
            {
                cout << endl;
                break;
            }
        }
    }
    cout << "截至目前,一共有" << count << "位听众预定参加。预定观众信息如下:" << endl;
    for (auto list : audience_info_list)
        list.print();
    return 0;
}

 

task6:

TextCoder.hpp

#pragma once

#include <iostream>
#include <string>

using namespace std;

class TextCoder {
private :
    string text;
    void encoder();
    void decoder();
public:
    TextCoder(string text);
    string get_ciphertext();
    string get_deciphertext();
};

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

void TextCoder::encoder() {
    for (int i = 0; i < text.length(); i++)
    {
        if((text[i]>='a'&&text[i]<='u')||(text[i]>='A'&&text[i]<='U'))
        {
            text[i]+=5;
        }
        else if((text[i]>'u'&&text[i]<='z')||(text[i]>'U'&&text[i]<='Z'))
        {
            text[i]-=21;
        } 
    }
}

void TextCoder::decoder() {
    for (int i = 0; i < text.length(); i++)
    {
        if((text[i]>='f'&&text[i]<='z')||(text[i]>='F'&&text[i]<='Z'))
        {
            text[i]-=5;
        }
        else if((text[i]>='a'&&text[i]<'f')||(text[i]>='A'&&text[i]<'F'))
        {
            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 @ 2022-10-23 22:30  miantiaooooo  阅读(3)  评论(0编辑  收藏  举报