19物联网一班李大伟

实验二


Info.hpp

复制代码
#pragma once
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

class Info
{
public:
    Info(string nickname1, string contact1, string city1, int n1) :
        nickname(nickname1), contact(contact1), city(city1),n(n1) {}
    void print()const;
private:
    string nickname;
    string contact;
    string city;
    int n;
};

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

task5.cpp

复制代码
#include<iostream>
#include<vector>
#include"Info.hpp"

int main()
{
    using namespace std;

    const int capacity = 100;
    vector<Info> audience_info_list;
    string nickname;
    string contact;
    string city;
    int n,t=0;
    
    cout << "录入信息:\n" << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
    
    while (cin>>nickname>>contact>>city>>n)
    {
        t += n;
        if (t > capacity)
        {
            cout << "对不起,只剩" << capacity + n - t << "个位置." << endl
                << "1.输入u,更新预定信息." << endl
                << "2.输入q,退出预定." << endl
                << "你的选择:";
            char ch;
            cin >> ch;
            if (ch == 'q')
            {
                t = t - n;
                break;
            }
            else
            {
                t = t - n;
                continue;
            }
        }

        Info info(nickname, contact, city, n);
        audience_info_list.push_back(info);

        if (t == 100)
            break;
    }
    cout << "截至目前,一共有" << t << "位听众预定参加。预定听众信息如下:" << endl;
    for (vector<Info>::iterator it = audience_info_list.begin();it != audience_info_list.end();it++)
        it->print();
}    
复制代码

运行截图

 

 

 

 6.实验任务6

TextCoder.hpp

复制代码
#pragma once
#include<iostream>
#include<string>

using namespace std;

class TextCoder
{
public:
    TextCoder(string Text):text(Text){}
    string encoder();
    string decoder();
private:
    string text;
};

string TextCoder::encoder()
{
    string::iterator it = text.begin();
    for (;it != text.end();it++)
    {
        if (*it >= 118&&*it<=122)
            *it -= 21;
        else if (*it >= 86 && *it <= 90)
            *it -= 21;
        else if((*it>=97&&*it<=117)||(*it>=65&&*it<=85))
            *it += 5;
    }
    return text;
}

string TextCoder::decoder()
{
    string::iterator it = text.begin();
    for (;it != text.end();it++)
    {
        if (*it <= 69&&*it>=65)
            *it += 21;
        else if (*it >= 97 && *it <= 101)
            *it += 21;
        else if((*it>=70&&*it<=90)||(*it>=102&&*it<=122))
            *it -= 5;
    }
    return text;
}
复制代码

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

 

运行截图

posted on 2021-12-22 15:37  19物联网一班李大伟  阅读(11)  评论(0编辑  收藏  举报

导航