C/C++教程

实验二——数组、指针和c++标准库

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

info.hpp代码如下:

#ifndef INFO_HPP
#define INFO_HPP

#include<iostream>
#include<string>
#include<iomanip>
using std::string;

class info {
public:
    info(string nickname0, string contact0, string city0, int n0);
    void print();
private:
    string nickname;
    string contact;
    string city;
    int n;
    
};

info::info(string nickname0, string contact0, string city0, int n0) :
    nickname(nickname0), contact(contact0), city(city0), n(n0) {};

void info::print() {
    using namespace std;
    cout << setw(15)<< left << "称呼:" << nickname << endl;
    cout << setw(15) << left << "联系方式:" << contact << endl;
    cout << setw(15) << left << "所在城市:" << city << endl;
    cout << setw(15) << left<<"预定人数:" << n << endl;
    cout << endl;
}
#endif

task5.cpp代码如下:

#include"info.hpp"
#include<string>
#include<iostream>
#include<vector>
using std::string;

int main() {
    using namespace std;

    char a;
    vector<info>audience_info_list;
    const int capacity = 100;
    string nickname, contact, city;
    int n,sum=0;

    cout << "录入信息:" << endl;
    cout << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;

    while (cin>>nickname>>contact>>city>>n) {
        sum += n;
        if (capacity >= sum) {
            info massage(nickname, contact, city, n);
            audience_info_list.push_back(massage);        
        }

        else {
            int b;
            b=sum-capacity ;

            cout << "对不起,只剩" << n-b << "个位置." << endl;
            cout << "1.输入u,更新(update)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择是:";
            cin >> a;
            if (a == 'q') {
                break;
            }
            else if (a == 'u') {
                continue;
            }
            cout << endl;
        }
    }
    cout << endl;
    cout << "截至目前,一共有" << sum-n << "位听众预定参加。预定听众信息如下:" << endl;
    cout << endl;
    for (auto it = audience_info_list.begin(); it != audience_info_list.end(); it++) {
         it->print();
    }
}

输出结果如下:

 

 

 textcoder.hpp代码如下:

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP


#include<iostream>
#include<string>
using std::string;

class textcoder {
public:
    textcoder(string text0) :text(text0) {};
    string encoder();
    string decoder();
private:
    string text;
};

string textcoder::encoder() {
    using namespace std;

    for (auto& ch : text) {
        if (ch >= 'a' && ch <= 'z') {
            ch = 'a' + (ch - 'a' + 5) % 26;
        }
        else if (ch >= 'A' && ch <= 'Z') {
            ch = 'A' + (ch - 'A' + 5) % 26;
        }
    }
    return text;
}

string textcoder::decoder() {
    using namespace std;

    for (auto& ch : text) {
        if (ch >= 'a' && ch <= 'z') {
            ch = 'a' + (ch - 'a' + 21) % 26;
        }
        else if (ch >= 'A' && ch <= 'Z') {
            ch = 'A' + (ch - 'A' + 21) % 26;
        }
    }
    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输入英文文本: ";
    }
}

测试结果如下:

 

 

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