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

2021/10/27 22:14:07

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

(因为编码问题,代码中选择英文代替中文)

实验任务5

源代码:

//info,h
#ifndef INC_27_LAB1_INFO_H
#define INC_27_LAB1_INFO_H

#include "string"
#include "iostream"

using namespace std;

class info {
public:
    info(string name, string co, string ci, int num) {
        nickname = name;
        contact = co;
        city = ci;
        n = num;
    }

    ~info() = default;

    void print() {
        cout << "nickname:  " << nickname << endl;
        cout << "contact:   " << contact << endl;
        cout << "city:      " << city << endl;
        cout << "people n:  " << n << endl;
    }


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

#endif //INC_27_LAB1_INFO_H
//task5.cpp
#include <iostream>
#include "info.h"
#include "vector"
#include "string"

using namespace std;

int main() {
    const int capacity = 100;
    int num = 0, n;
    vector<info> audience_info_list;
    string nickname, contact, city;
    while (num < capacity) {
        cout << "please enter member information(enter quit to end):" << endl;
        cin >> nickname;
        if (nickname == "quit")break;
        cin >> contact >> city >> n;
        info f(nickname, contact, city, n);
        if (num + n > capacity) {
            cout << "no quota left!" << endl;
            cout << "there are still " << capacity - num << " place left" << endl;
            cout << "enter u to update your info or enter q to quit" << endl;
            char flag;
            cin >> flag;
            if (flag == 'u') { continue; }
            else { break; }
        } else {
            num += n;
            audience_info_list.push_back(f);
        }
    }
    cout << "there are " << num << " people now" << endl;
    for (auto it = audience_info_list.begin(); it != audience_info_list.end(); ++it) {
        it->print();
    }
}

运行结果:

 

实验任务6:

源程序

// TextCoder.h
#ifndef INC_27_LAB2_TEXTCODER_H
#define INC_27_LAB2_TEXTCODER_H

#include <utility>

#include "string"

using namespace std;

class TextCoder {
public:
    TextCoder(string t) {
        text = t;
    }

    ~TextCoder() = default;

    string encoder() {
        for (int i = 0; i < text.length(); i++) {
            if (text[i] >= 'a' && text[i] <= 'z') {
                if (text[i] >= 'v') {
                    text[i] = text[i] + 5 - 26;
                } else {
                    text[i] += 5;
                }
            } else if (text[i] > 'A' && text[i] <= 'Z') {
                if (text[i] >= 'V') {
                    text[i] = text[i] + 5 - 26;
                } else {
                    text[i] += 5;
                }
            }
        }
        return text;
    }

    string decoder() {
        for (int i = 0; i < text.length(); i++) {
            if (text[i] >= 'a' && text[i] <= 'z') {
                if (text[i] <= 'e') {
                    text[i] = text[i] - 5 + 26;
                } else {
                    text[i] -= 5;
                }
            } else if (text[i] > 'A' && text[i] <= 'Z') {
                if (text[i] <= 'E') {
                    text[i] = text[i] - 5 + 26;
                } else {
                    text[i] -= 5;
                }
            }
        }
        return text;
    }

private:
    string text;
};


#endif //INC_27_LAB2_TEXTCODER_H
#include "textcoder.h"
#include <iostream>
#include <string>

int main() {
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "Enter English text:";
    while (getline(cin, text)) {
        if(text=="end")break;
        encoded_text = TextCoder(text).encoder();  // 这里使用的是临时无名对象
        cout << "Encrypted English text:" << encoded_text << endl;

        decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
        cout << "Decrypted English text:" << decoded_text << endl;
        cout << "Enter English text:";
    }
}

运行结果:

 

 

 



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


扫一扫关注最新编程教程