实验2 数组、指针与C++标准库

使用C++实现信息管理和文本编码解码
该博客展示了两个C++实现的类:Info用于管理个人信息,包括昵称、联系方式、城市和预定人数;TextCoder则实现了简单的字母编码和解码功能,通过将字母向前偏移5位进行加密。在Info类中,用户可以输入信息并检查预定位置的可用性。TextCoder类提供了文本的加密和解密方法,适用于简单的信息安全需求。

 

task5

Info.hpp

#include<iostream>
#include<string>
using namespace std;

class Info{

public:
    Info(string name = "",string con = "",string ci = "",int nu = 0):nickname{name},contact{con},city{ci},n{nu}{}
    ~Info() = default;
    void print()
    {
        cout << "称呼:    "<< nickname << endl;
        cout << "联系方式:" << contact << endl;
        cout << "所在城市:" << city << endl;
        cout << "预订人数:" << n << endl;
    }
private:
    string nickname;
    string contact;
    string city;
    int n;
};

task5.cpp

#include"Info.hpp"
#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
    const int capacity = 100;
    int Number = 100,count = 0;
    vector<Info> audience_info_list;
    cout << "录入信息" << endl;
    cout << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
    string s1,s2,s3,s4;
    int n1;
    while(cin >> s1)
    {
        cin >> s2 >> s3 >> n1;
        audience_info_list.push_back(Info(s1,s2,s3,n1));
        if(Number - n1 < 0)
        {
            cout << "对不起,只剩下" << Number << "个位置" << endl;
            cout << "1.输入u,更新(updata)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            char a;
            cin >> a;
            if(a == 'q')
            {
                cout << "你的选择:q" << endl;
                break;
            }
            if(a == 'u')
            {
                audience_info_list.pop_back();
                continue;
            }
        }
        Number -= n1;
    }
    cout << "截至目前,一共有" << Number << "位听众参加预定。预定听众信息如下:" << endl;
     for(auto it=audience_info_list.begin();it!=audience_info_list.end();++it)
        it->print();
    return 0;
}

task 6

textcoder.hpp

#include<iostream>
#include<string>
using namespace std;

class TextCoder{

public:
    TextCoder(string s = ""):text(s){}
    ~TextCoder() = default;
    string encoder()
    {
        for(int i = 0;i < text.size();i++)
        {
            if(text[i] >= 'a'&&text[i] <= 'u'||text[i] >= 'A'&&text[i] <= 'U')
                text[i] = text[i] + 5;
            else if(text[i] > 'u'&&text[i] <= 'z'||text[i] >'U'&&text[i] <= 'Z') 
                text[i] = text[i] - 26 + 5;
        }
        return text;
    }
    string decoder()
    {
        for(int i = 0;i < text.size();i++)
        {
            if(text[i] >= 'f'&&text[i] <= 'z'||text[i] >= 'F'&&text[i] <= 'Z')
                text[i] = text[i] - 5;
            else if(text[i] >= 'a'&&text[i] < 'f'||text[i] >='A'&&text[i] < 'F') 
                text[i] = text[i] + 26 - 5;
        }
        return text;
    }
private:
    string text;
};

task6.cpp


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值