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

2021/11/2 22:40:27

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

------------恢复内容开始------------

任务5

Info.hpp:

#include <iostream>
#include<string>
using namespace std;
class Info
{
public:
    Info( string nickname_v, string contact_v, string city_v="", int n_v=0) :
        nickname{ nickname_v }, contact{ contact_v }, city{ city_v }, n{ n_v }{};

    Info(Info const& i) : nickname(i.nickname), contact(i.contact), city(i.city), n(i.n) {};
    void print()const
    {
        cout << "称呼:\t" << nickname << endl;
        cout << "联系方式:\t" << contact << endl;
        cout << "所在城市:\t" << city << endl;
        cout << "预约人数:\t" << n << endl;
    };
private:
    string nickname, city;
    string contact;
    int n;
};

源.cpp:

#include <iostream>
#include<string>
#include<vector>
#include"Info.hpp"
using namespace std;
 const int capacity = 100;
int main()
{
    string  nickname_v, city_v,contact_v,x;
    int n_v;
    vector<Info> audience_info_list;
    int now = 0;
    cout << "录入信息:\n" << endl;
    cout << "称呼、昵称,联系方式(邮箱、手机号),所在城市,预定参加人数;" << endl;

    while(cin>>nickname_v)
    {
        cin >> contact_v >> city_v >> n_v;
        if(now + n_v > capacity)
        {
            cout << "对不起,只剩" << capacity - now << "个位置." << endl;
            cout << "1、输入u,更新(update)预定信息" << endl;
            cout << "2、输入q,退出预订" << endl;
            cout << "你的选择:";
            cin >> x;
            if (x == "u")
                cout << "请重新输入:" << endl;
            else
               break;
        }
        else
        {
            now += n_v;
            audience_info_list.push_back( Info(nickname_v, contact_v, city_v, n_v));

        }
    }
    cout << "\n截至目前,一共有" << now << "位听众参加,预定听众信息如下:" << endl;
    vector<Info>::const_iterator i;
    for (i = audience_info_list.begin(); i != audience_info_list.end(); i++)
        (*i).print();
    return 0;
}

实验结果截图:

 

实验任务6

 

textcoder.hpp

#include<iostream>
 #include<string>

using namespace std;

 class TextCoder
 {
     public:
             TextCoder(string t = "") : text(t) {};
             TextCoder(TextCoder & t) : text(t.text) {};
             string encoder();
            string decoder();
     private:
             string text;
     };

 string TextCoder::encoder()
 {
         string TextEncoder;
         for (auto& i : text)
             {
                 if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
                         TextEncoder.push_back((i - 'a' + 5) % 26 + 'a');
                 else
                         TextEncoder.push_back(i);
             }
         return TextEncoder;
     }

 string TextCoder::decoder()
 {
         string TextDecoder;
         for (auto i : text)
             {
                 if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
                         TextDecoder.push_back((i - 'a' + 21) % 26 + 'a');
                 else
                         TextDecoder.push_back(i);
             }
         return TextDecoder;
     }

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

 



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


扫一扫关注最新编程教程