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

实验任务五:

info.cpp源文件:

#ifndef UNTITLED11_INFO_H
#define UNTITLED11_INFO_H
#include<iostream>
#include<vector>
#include<string>

using namespace std;
class info{
private:
    string nickname;
    string contact;
    string city;
    int n;
public:
    info(string nick,string con,string c,int ni){
        nickname=nick;
        contact=con;
        city=c;
        n=ni;
    };
    ~info()=default;
    void print(){
        cout<<"称呼:"<<nickname<<endl;
        cout<<"联系方式:"<<contact<<endl;
        cout<<"所在城市:"<<city<<endl;
        cout<<"预定人数:"<<n<<endl;
    };
};


#endif

task5.cpp源码:

#include <iostream>
#include <string>
#include <vector>
#include "info.h"


int main() {

    using namespace std;
    vector<info> audience_info_list;
    string nickname, contact, city, b;
    int n = 0, a = 0;
    const int capacity = 100;

    cout << "录入信息:" << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
    while (a < capacity) {
        cin >> nickname;
        if (nickname == "over")break;
        cin >> contact >> city >> n;

        if (a + n > capacity) {
            cout << "对不起,只剩" << capacity - a << "个位置。" << endl;
            cout << "1.输入u,更新预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:" << endl;
            cin >> b;
            if (b == "u")continue;
            if (b == "q")break;
        } else {
            a += n;
            info c(nickname, contact, city, n);
            audience_info_list.push_back(c);
        }

    }
    cout << "截至目前,一共有" << a << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto it = audience_info_list.begin(); it != audience_info_list.end(); ++it)
        it->print();
}

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

 

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

 实验任务六:

TextCoder.hpp文件源码:
#ifndef UNTITLED10_TEXTCODER_H
#define UNTITLED10_TEXTCODER_H
#include<iostream>
#include<string>
#include "ctype.h"
using namespace std;
class TextCoder {
private:
    string text;
public:
    TextCoder(string t){
        text=t;
    };
    ~TextCoder()=default;
    string encoder(){
        for(int i=0;text[i];i++)
            if(isupper(text[i]))
                text[i]=(text[i]-'A'+5)%26+'A';
            else if(islower(text[i]))
                text[i]=(text[i]-'a'+5)%26+'a';
        return text;
    };
    string decoder(){for(int i=0;text[i];i++)
            if(isupper(text[i]))
                text[i]=(text[i]-'A'-5)%26+'A';
            else if(islower(text[i]))
                text[i]=(text[i]-'a'-5)%26+'a';
        return text;
    };
};


#endif
task6.cpp源码:
#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=="over")break;
        encoded_text = TextCoder(text).encoder();  // 这里使用的是临时无名对象
        cout << "Encrypted English text:\t" << encoded_text << endl;

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

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

 

上一篇:javaweb大文件上传


下一篇:上传视频加进度条总结