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

实验任务5

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;
}

运行结果

 

实验任务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

#include<bits/stdc++.h>
#include"textcoder.hpp"
using namespace std;
int main()
{
    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输入英文文本: ";
    }
}

 

 

posted @ 2021-10-30 18:02  Re-cursion  阅读(41)  评论(4)    收藏  举报