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

实验任务5

info.hpp

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class info {
public:
    info(string ni, string c, string ci, int num) :nickname(ni), contact(c), city(ci), n(num) { count = count + num; };
    void print() const {
        cout.setf(ios::left);
        cout << "称呼:    " << setw(20)<<nickname << endl;
        cout << "联系方式:" << setw(20) << contact << endl;
        cout << "所在城市:" << setw(20) << city << endl;
        cout << "预定人数:" << setw(20) << n << endl;
    };
    int get_count();
private:
    string nickname,contact,city;
    int n;
    static int count;
};
int info::count = 0;
int info::get_count() { return count; }

task5.cpp

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include"info.hpp"
int main() {
    using namespace std;
    vector<info>audience_info_list;
    const int capcity = 100;
    cout << "录入信息:" << endl;
    string ni,c,ci;
    int num, count=0,y=0,x=0;
    cout << "称呼/昵称,联系方式(邮箱/手机号码),所在城市,预定参加人数" << endl;
    while (cin>>ni) {
        cin >> c;
        cin >> ci;
        cin >> num;
        y = count;
        count = count + num;
        x = capcity - count;
        if (x<=0) {
            cout << "对不起,只剩" << capcity-y << "个位置" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            char a;
            cin >> a;
            if (a == 'q')
                break;
            else
                if (a == 'u') {
                    count = y;
                    continue;
                }
        }
        else {
            info a(ni, c, ci, num);
            audience_info_list.push_back(a);
        }
    }
    cout << endl;
    if(x<=0)
        cout << "截至目前,一共有" << y << "位听众预定参加,预定听众信息如下" << endl;
    else
        cout << "截至目前,一共有" << count << "位听众预定参加,预定听众信息如下" << endl;
    for (auto const& i : audience_info_list) {
        i.print();
    }
}

运行结果:

 

 

 

 

 

 

实验任务6

TextCoder.hpp

#include<iostream>
#include<string>
using namespace std;
class TextCoder {
public:
    TextCoder(string t) :text(t) {};
    string encoder() {
        for (int i = 0; i < text.length(); i++) {
            if ((text[i] >= 'a' && text[i] <= 'u')|| (text[i] >= 'A' && text[i] <= 'U')) {
                text[i] = text[i] + 5;
            }
            else if ((text[i]>='v' && text[i]<='z') || (text[i]>= 'V' && text[i]<='Z')) {
                text[i] = text[i] - 21;
            }
        }
        return text;
    }
    string decoder(){
        for (int i = 0; i < text.length(); i++) {
            if ((text[i] >= 'f' && text[i] <= 'z') || (text[i] >= 'F' && text[i] <= 'Z')) {
                text[i] = text[i] - 5;
            }
            else if ((text[i] >='A' && text[i]<='E') || (text[i]>= 'a' && text[i]<='e')) {
                text[i] = text[i] + 21;
            }
        }
        return text;
    }
private:
    string 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 << "加密后英文文本:" << encoded_text << endl;
        decoded_text = TextCoder(encoded_text).decoder();
        cout << "解密后英文文本:" << decoded_text << endl;
        cout << "\n输入英文文本:";
    }
}

运行结果:

 

posted @ 2021-10-29 15:01  Rougesss  阅读(22)  评论(3)    收藏  举报