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

实验任务5

task5.cpp

#include<iostream>
#include<vector>
#include"info.hpp"
using namespace std;
int main() {
    vector<Info> audience_info_list;
    const int cpacity = 100;
    string nickname, contact, city;
    int n;
    cout << "录入信息" << "\n" << "\n" << endl;
    cout << "称呼/昵称,联系方式(邮箱/电话号码),所在城市,预定参与人数" << endl;
    while (cin >> nickname >> contact >> city >> n)
    {
        int remain;
        remain = Info::rest(cpacity);
        if (n > remain) {
            cout << "对不起,只剩" << Info::rest(cpacity) << "个位置" << endl;
            cout << "1.输入u,更新预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择" << endl;
            char choice;
            cin >> choice;
            if (choice == 'u') {
                continue;
            }
            else
            {
                break;
            }
        }
        else
        {
            Info info = Info(nickname, contact, city, n);
            audience_info_list.push_back(info);
        }
    }
    cout << "\n" << "\n" << endl;
    cout << "截至目前,一共有" << cpacity - Info::rest(cpacity) << "位听众参加,预定听众信息如下" << endl;
    for (auto i : audience_info_list) {
        i.print();
    }
    cout << "\b\b\n";
}

info.hpp

#include<iostream>
#include<string>
using namespace std;
class  Info
{
public:
    Info(string nickname0, string contact0, string city0, int n0);
    void print();
    static int rest(int capcity);

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

Info::Info(string nickname0, string contact0, string city0, int n0):nickname(nickname0) , contact(contact0) , city
(city0) , n(n0)
{
    total += n0;
}
void Info::print() {
    cout << "称呼:" << nickname << endl;
    cout << "联系方式:" << contact << endl;
    cout << "所在城市" << city << endl;
    cout << "预定人数" << n << endl;
}
int Info::rest(int cpacity) {
    return (cpacity - total);
}

 

实验任务6

textcoder.hpp

#ifndef TEXTCODER
#define TEXTCODER

#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(){
    for(string::iterator it = text.begin(); 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;
}

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

 

 

 

posted @ 2021-11-02 22:09  致命华彩真实伤害  阅读(80)  评论(3)    收藏  举报