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

实验任务5

info.hpp

#include <bits/stdc++.h>
using namespace std;

class Info
{
public:
    Info(string nickname, string contact, string city, int n) : nickname{nickname}, contact{contact}, city{city}, n{n} {}
    void print()
    {
        cout << left << setw(10) << "称呼:" << nickname << endl;
        cout << left << setw(10) << "联系方式:" << contact << endl;
        cout << left << setw(10) << "所在城市:" << city << endl;
        cout << left << setw(10) << "预定人数:" << n << endl;
        cout << endl;
    }
    const int get_n()
    {
        return n;
    }

private:
    string nickname, contact, city;
    int n;
};

 

task5.cpp

#include <bits/stdc++.h>
#include "info.hpp"
using namespace std;

int main()
{
    const int capacity = 100;
    vector<Info> audience_Info_list;
    cout << "录入信息:" << endl
         << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数:" << endl;
    string nickname, contact, city;
    int n, t = capacity;
    while (cin >> nickname >> contact >> city >> n)
    {
        t -= n;
        if (t >= 0)
        {
            Info s(nickname, contact, city, n);
            audience_Info_list.push_back(s);
            continue;
        }
        else
        {
            t += n;
            cout << "对不起,只剩" << t << "个位置。" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            char c;
            cout << "你的选择:";
            cin >> c;
            if (c == 'q' || c == 'u')
            {
                if (c == 'u')
                {
                    continue;
                }
                else if (c == 'q')
                {
                    cout << endl << "截至目前,一共有" << capacity - t << "位听众预定参加。预定听众信息如下:" << endl;
                    for (auto &s : audience_Info_list)
                        s.print();
                    return 0;
                }
            }
        }
    }
    cout << endl << "截至目前,一共有" << capacity - t << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto &s : audience_Info_list)
        s.print();
    return 0;
}

 

运行结果:

 

实验任务6

textcoder.hpp

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP

#include <bits/stdc++.h>
using namespace std;

class TextCoder
{
public:
    TextCoder(string code) : text{code} {}
    string encoder()
    {
        for (auto &s : text)
            if ((s >= 'a' && s<= 'z') || (s >= 'A' && s <= 'Z'))
                if(s >= 'v' && s <= 'z')
                {
                    s -= 21;
                }
                else if(s >= 'V' && s <= 'Z')
                {
                    s -= 21;
                }
                else
                    s += 5;
        string a(text);
        return a;
    }
    string decoder()
    {
        for (auto &s : text)
            if ((s >= 'a' && s<= 'z') || (s >= 'A' && s <= 'Z'))
                if(s >= 'v'-21 && s <= 'z'-21)
                {
                    s += 21;
                }
                else if(s >= 'V'-21 && s <= 'Z'-21)
                {
                    s += 21;
                }
                else
                    s -= 5;
        string a(text);
        return a;
    }

private:
    string text;
};

#endif

 

task6.cpp

#include "textcoder.hpp"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    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输入英文文本: ";
    }
}

 

运行结果:

实验总结:

迭代器和STL的使用在task5中充分体现,

包括循环算法也好,

task5很精炼。

 

posted @ 2021-10-31 14:53  katachip  阅读(46)  评论(3编辑  收藏  举报