实验4 现代C++标准库与类模板

发布时间 2023-11-27 16:10:24作者: ~Lucky

实验任务5

#pragma once 

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

class TextCoder {
public:
    TextCoder() = default; 
    TextCoder(string str);
    string get_ciphertext();
    string get_deciphertext();
    ~TextCoder() = default;

private:
    string text;
    void encoder();
    void decoder();
};

TextCoder::TextCoder(string str) {
    text = str;
}

string TextCoder::get_ciphertext() {
    encoder();
    return text;
}

string TextCoder::get_deciphertext() {
    decoder();
    return text;
}

void TextCoder::encoder() {
    for (int i = 0; i < text.length(); i++) {
        if (islower(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'a' + 7) % 26 + 'a');
        } else if (isupper(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'A' + 7) % 26 + 'A');
        }
    }
}

void TextCoder::decoder() {
    for (int i = 0; i < text.length(); i++) {
        if (islower(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'a' + 26 - 7) % 26 + 'a');
        } else if (isupper(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'A' + 26 - 7) % 26 + 'A');
        }
    }
}
TextCoder.hpp
#include "textcoder.hpp"
#include <iostream>
#include <string>

void test() {
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text)) {
        encoded_text = TextCoder(text).get_ciphertext(); 
        cout << "加密后英文文本:\t" << encoded_text << endl;

        decoded_text = TextCoder(encoded_text).get_deciphertext(); 
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

int main() {  
    test(); 
}
task5.cpp

 

实验任务6

#pragma once

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

class Info {
public:
    Info(const string nickname_, const string contact_, const string city_, int n_);
    ~Info() = default;
    void print() const;
      
private:
    string nickname;
    string contact;
    string city;
    int n;
};

Info::Info(const string nickname_, const string contact_, const string city_, int n_)
        : nickname{nickname_}, contact{contact_}, city{city_}, n{n_} {}

void Info::print() const {
        cout << left << endl
             << "昵称:" << setw(15) << nickname << endl
             << "联系方式:" << setw(15) << contact << endl
             << "所在城市:" << setw(15) << city << endl
             << "预定人数:" << setw(15) << n << endl;
}
Info.hpp
#include "Info.hpp"
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int capacity = 100;
static int total = 0;

int main() {
    vector<Info> audience_info_list;
    string nickname_, contact_, city_;
    int n_;
    cout << "录入信息:" << endl << "\n";
    cout << "昵称   联系方式(邮箱/手机号)   所在城市   预定参加人数" << endl;
    while (cin >> nickname_)
    {
        cin >> contact_ >> city_ >> n_;
        if (total + n_ <= capacity) {
            Info tmp(nickname_, contact_, city_, n_);
            audience_info_list.push_back(tmp);//将新的听众信息存入list 
            total += n_;
            if (total == capacity)
                break;
        }
        else
        {
            char option;
            cout << "对不起,只剩" << capacity - total << "个位置."
                 << "\n1. 输入u,更新(update)预定信息"
                 << "\n2. 输入q,退出预定"
                 << "\n你的选择:";
            cin >> option;
            if (option == 'u')
            {
                cout << "请重新输入:" << endl;
            }
            if (option == 'q')
            {
                cout << endl;
                break;
            }
        }
    }
    cout << "截至目前,一共有" << total << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto& list : audience_info_list)
        list.print();
    return 0;
}
task6.cpp