C/C++教程

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

本文主要是介绍实验二 数组、指针与C++标准库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

任务5 源代码:

info.hpp

#pragma once
#include<iostream>
#include<iomanip>

class Info
{
public:
    Info(const std::string &nickname0 = "", const std::string &contact0 = "", const std::string &city0 = "", const int &n0 = 0) : nickname{nickname0}, contact{contact0}, city{city0}, n{n0} { this->total_reservation += n; }
    ~Info() = default;
    void print() const;
    static int get_total();

private:
    std::string nickname;
    std::string contact;
    std::string city;
    int n;
    static int total_reservation;
};

int Info::total_reservation = 0;

void Info::print() const
{
    using namespace std;
    cout.width(14);
    cout << setiosflags(ios::left) << "称呼:";
    cout << this->nickname << endl;
    cout.width(14);
    cout << setiosflags(ios::left) << "联系方式:";
    cout << this->contact << endl;
    cout.width(14);
    cout << setiosflags(ios ::left)<< "所在城市:";
    cout << this->city << endl;
    cout.width(14);
    cout << setiosflags(ios::left) << "预定人数:";
    cout << this->n;
}

int Info::get_total()
{
    return Info::total_reservation;
}

task5.cpp

#include "info.hpp"
#include<vector>
#include<string>

int main()
{
    using namespace std;
    vector<Info> audience_info_list;
    const int capacity = 100;
    string str;
    cout << "录入信息:" << endl
         << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数(每个信息之间请以英文逗号分隔,每个人员请换行输入)" << endl;
    while(getline(cin, str))
    {
        int pos = 0, count = 0, n;
        string nikcname, contact, city, tmp;
        while(true) //拆分读入的一行字符串
        {
            int ret = str.find(",", pos);
            tmp = str.substr(pos, ret - pos);
            if (ret == static_cast<int>(string::npos))
            {
                tmp = str.substr(pos, str.size() - pos);
                int num = atoi(tmp.c_str());
                n = num;
                break;
            }
            switch (count)
            {
            case 0:
                nikcname = tmp;
                break;
            case 1:
                contact = tmp;
                break;
            case 2:
                city = tmp;
                break;
            default:
                throw out_of_range("count overflow");
                break;
            }
            pos = ret + 1;
            count++;
        }
        if(Info::get_total() + n >= capacity) //判断容量是否够
        {
            char choice;
            cout << "对不起,只剩" << capacity - Info::get_total() << "个位置." << endl;
            cout << "1. 输入u,更新(updata)预定信息" << endl;
            cout << "2. 输入q,退出预定" << endl;
            cout << "你的选择:";
            while(true)
            {
                cin >> choice;
                if(choice == 'u')
                {
                    int tmp_n;
                    cout << "请重新输入您的预定参加人数,不大于" << capacity - Info::get_total() << "人:";
                    while(true)
                    {
                        cin >> tmp_n;
                        if(tmp_n >= (capacity - Info::get_total()))
                            cout << "输入人数仍大于最多可预定人数,请重新输入:";
                        else
                        {
                            cout << "更新成功!" << endl;
                            n = tmp_n;
                            audience_info_list.push_back(Info(nikcname, contact, city, n));
                            cin.ignore(numeric_limits<streamsize>::max(), '\n');
                            break;
                        }
                    }
                    break;
                }
                else if(choice == 'q')
                    break;  
                else
                    cout << "输入有误!请重新输入你的选择(u or q):";
            }
            if(Info::get_total() >= capacity || choice == 'q')
                break;
        }
        else
            audience_info_list.push_back(Info(nikcname, contact, city, n));
    }
    cout << endl;
    cout << "截至目前,一共有" << Info::get_total() << "位听众预定参加。预定听众信息如下:" << endl;
    for(auto info:audience_info_list)
    {
        info.print();
        cout << endl;
    }
    return 0;
}

 

 任务6 源代码:

textcoder.hpp

#pragma once
#include<iostream>
#include<vector>
#include<ctime>

class TextCoder
{
public:
    TextCoder(std::string text0 = "") : text(text0) { ; }
    ~TextCoder() = default;
    std::string encoder();
    std::string decoder();

private:
    std::string text;
};

std::string TextCoder::encoder()
{
    std::string res;
    for (int i = 0; i < static_cast<int>(this->text.size()); i++)
    {
        if(this->text[i] >= 'a' && this->text[i] <= 'z')
        {            
            if(this->text[i] + 5 <= 'z')
                res.push_back(this->text[i] + 5);
            else
                res.push_back(this->text[i] + 5 - 'z' - 1 + 'a');
        }     
        else if(this->text[i] >= 'A' && this->text[i] <= 'Z')
        {
            if(this->text[i] + 5 <= 'Z')
                res.push_back(this->text[i] + 5);
            else
                res.push_back(this->text[i] + 5 - 'Z' - 1 +'A');
        }    
        else
            res.push_back(this->text[i]);
    }
    return res;
}

std::string TextCoder::decoder()
{
    std::string res;
    for (int i = 0; i < static_cast<int>(this->text.size()); i++)
    {
        if(this->text[i] >= 'a' && this->text[i] <= 'z')
        {
            if(this->text[i] - 5 >= 'a')
                res.push_back(this->text[i] - 5);
            else 
                res.push_back(this->text[i] + 26 - 5);
        }
        else if (this->text[i] >= 'A' && this->text[i] <= 'Z')
        {
            if(this->text[i] - 5 >= 'A')
                res.push_back(this->text[i] - 5);
            else
                res.push_back(this->text[i] + 26 - 5);
        }
        else 
            res.push_back(this->text[i]);
    }
    return res;
}

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

 

这篇关于实验二 数组、指针与C++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!