C/C++教程

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

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

task5 :

 

 "info.h"

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

class info {
private:
    string nickname;
    string contact;
    string city;
    int n;
public:
    info(string nickname="null", string contact="null", string city="null", int n=0) :nickname{nickname}, contact{contact}, city{city}, n{n}{}
    void print();
};
void info::print() {
    cout<< "Name:    " << nickname << endl << "Contact: " << contact << endl << "City:    " << city << endl << "NumOfpp: " << n << endl<<endl;
}

 

 

 

"task5.cpp"

#include<iostream>
#include<string>
#include<vector>
#include"info.h"
using namespace std;

int num = 0;

vector<info> audience_info_list;

void enter(string &bname,const int capacity) {
    string bcontact;
    string bcity;
    int bn;
    cin  >> bcontact >> bcity >> bn;
    info book(bname,bcontact,bcity,bn);
    if (bn < capacity - num) {
        audience_info_list.push_back(book);
        num += bn;
    }
    else if (bn == capacity - num) {
        cout << "Seat full!" << endl;
        audience_info_list.push_back(book);
        num += bn;
        cout << "Till now , " << num << " audiences have booked tickets.Infomation as follows :" << endl;
        for (auto it = audience_info_list.begin(); it != audience_info_list.end(); ++it)
            (*it).print();
    }
    else
    {
        char choice;
        cout << "Sorry!There are only " << capacity - num << " seats left!" << endl
            << "1. enter u to update book infomation" << endl << "2. enter q to quit"
            << endl << "Please enter your choice:";
        cin >> choice;
        if (choice == 'u') {
            cin >> bname;
            enter(bname, capacity);
        }
        else
        {
            cout << "Till now , " << num << " audiences have booked tickets.Infomation as follows :" << endl;
            for (auto it = audience_info_list.begin(); it != audience_info_list.end(); ++it)
                (*it).print();
        }
    }
}





int main() {
    cout << "Enter info:" << endl << endl;
    cout << "name , contact (tellphone/email) , city , numofpeople" << endl;
    const int capacity = 5;
    string bname;
    while (cin >> bname)
        enter(bname, capacity);
    cout << "Till now , " << num << " audiences have booked tickets.Infomation is as follows :" << endl;
    for (auto it = audience_info_list.begin(); it != audience_info_list.end(); ++it)
        (*it).print();
}

 

结果截图:

 

 

 

 

 

task6:

"textcoder.hpp"

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

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

string Textcoder::encoder() {
    for (auto& i : text)
        if (i >= 'a' && i <= 'u')
            i = i + 5;
        else 
            switch (i)
            {
            case 'v':i = 'a'; break;
            case 'w':i = 'b'; break;
            case 'x':i = 'c'; break;
            case 'y':i = 'd'; break;
            case 'z':i = 'e'; break;
            default:
                break;
            }
    return text;
}

string Textcoder::decoder() {
    for (auto& i : text)
        if (i >= 'f' && i <= 'z')
            i = i - 5;
        else
            switch (i)
            {
            case 'a':i = 'v'; break;
            case 'b':i = 'w'; break;
            case 'c':i = 'x'; break;
            case 'd':i = 'y'; break;
            case 'e':i = 'z'; break;
            default:
                break;
            }
    return text;
}

 

 

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++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!