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

发布时间 2023-11-27 16:35:09作者: 帽子戏法cyt

实验任务5
TextCoder.hpp

#pragma once
#include<iostream>
#include<string>

using namespace std;

class TextCoder {
public:
    TextCoder(string text0) : text{ text0 } {};

    string get_ciphertext();
    string get_deciphertext();

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

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

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

void TextCoder::encoder() {
    for (auto i = 0; i < text.size(); ++i) {
        if (isalpha(text[i])) {
            char base = islower(text[i]) ? 'a' : 'A';
            text[i] = char((text[i] - base + 7) % 26 + base);
        }
    }
}

void TextCoder::decoder() {
    for (auto i = 0; i < text.size(); ++i) {
        if (isalpha(text[i])) {
            char base = islower(text[i]) ? 'a' : 'A';
            text[i] = char((text[i] - base + 19) % 26 + base);
        }
    }
}
View Code

task5.cpp

#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(); 
}
View Code

 

实验任务6
Info.hpp

#pragma once 

#include<iostream>
#include<string>
#include <iomanip>

using namespace std;
class Info{
public:
    Info(string nickname0, string contact0, string city0, int n0);
    ~Info()=default;

    void print() const;
    
private:
    string nickname,contact,city;
    int n;
};

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

void Info::print() const
{
    cout << "昵称:     " << nickname << endl;
    cout << "联系方式:     " << contact << endl;
    cout << "所在城市:     " << city << endl;
    cout << "预定参加人数:     " << n << endl;
}
View Code

task6.cpp

#include "Info.hpp"
#include<iostream>
#include<string>
#include <vector>

using namespace std;

int main()
{
    const int capacity=100;
    vector<Info> audience_info_list;
    
    cout << "录入信息:" << endl;
    cout << endl;
    cout << "昵称     联系方式(邮箱/手机号)     所在城市     预定参加人数     " << endl;
    char choice;
    int sum=0;
    string name, contact, city;
    int n;
    while(cin >> name >> contact >> city >> n)
    {
        sum+=n;
        
        if(sum > capacity)
        {
            sum-=n;
            cout << "对不起,只剩" << capacity-sum << "个位置" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl
                 << "2.输入q, 退出预定" << endl
                 <<"你的选择: ";
            cin >> choice;
            
            if(choice=='u')
            {
                cin >> name >> contact >> city >> n;
                Info a(name,contact,city,n);
                audience_info_list.push_back(a);
                sum+=n;
            }
            else 
             break;
        }
        else audience_info_list.push_back(Info(name, contact, city, n));
    }
    cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
    for(auto &Info:audience_info_list)
    {
        Info.print();
    }
}
View Code