实验三

实验任务5

Info.hpp

#pragma once 

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

using namespace std;
class Info{
public:
    Info(string nickname0, string contact0, string city0, int n0);
    ~Info()=default;

    void print() const;
    
private:
    string nickname,contact,city;
    int n;
};

Info::Info(string nickname0, string contact0, string city0, int n0)
{
    nickname = nickname0;
    contact = contact0;
    city = city0;
    n=n0;
}

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

 

task5.cpp

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

using namespace std;

int main()
{
    const int capacity=100;
    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();
    }
}

 

 

实验任务6

TextCoder.hpp

#pragma once
#include<iostream>
#include<string>

using namespace std;

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

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

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

void TextCoder::encoder()
{
    for(auto i = 0; i < text.size(); ++i)
    {
        if(text[i] >= 'v' && text[i] <= 'z' || text[i] >= 'V' && text[i] <= 'Z')
                text[i] = char(text[i] - 21);
        else if(text[i] >='A' && text[i] <= 'z')
                text[i] = char(text[i] + 5);  
    }
}

void TextCoder::decoder()
{
    for(auto i = 0; i < text.size(); ++i)
    {
        if(text[i] >= 'a' && text[i] <= 'e' || text[i] >= 'A' && text[i] <= 'Z')
                text[i] = char(text[i] + 21);
        else if(text[i] >='A' && text[i] <= 'z')
                text[i] = char(text[i] - 5 );  
     }
}

 

 

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-24 22:09  帽子戏法cyt  阅读(21)  评论(0)    收藏  举报