实验3 数组、指针与现代c++标准库

实验任务5

info.hpp

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

using namespace std;

class info {
public:
    info(string name, string contact, string city, int n);
    ~info() = default;

    void print();
    int get_total();

private:
    string nickname;
    string contact;
    string city;
    int n;
    static int total;
};

int info::total = 0;
info::info(string name, string contact, string city, int n) :nickname{ name }, contact{ contact }, city{ city }, n{ n }{
    total += n;
}

int info::get_total() {
    return total;
}

void info::print() {
    cout << setw(10) << "称呼:" << nickname << endl
        << setw(10) << "联系方式:" << contact << endl
        << setw(10) << "所在城市:" << city << endl
        << setw(10) << "预定人数:" << n << endl << endl;
}

task5.cpp

#include"info.hpp"
#include<vector>
#include<iomanip>

using namespace std;

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

int main() {
    vector<info>audience_info_list;
    string name, contact, city;
    int n;
    char ch;

    cout << "录入信息:" << endl<< endl;
    cout << left << "昵称\t"
        << "联系方式(邮箱/手机号)\t\t"
        << "所在城市\t"
        << "预定参加人数\t" << endl;
    while (cin >> name)
    {
        cin >> contact >> city >> n;
        if (i + n <= capacity)
        {
            info t(name, contact, city, n);
            audience_info_list.push_back(t);
            i += n;
            if (i == capacity)
                break;
        }
        else
        {
            cout << "对不起,只剩" << capacity - i << "个位置." << endl;
            cout << "1.输入u,更新(update)预定信息" << endl
                 << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            cin >> ch;
            if (ch == 'u')
                cout << "请重新录入信息:" << endl;
            else
            {
                cout << endl;
                break;
            }
        }
    }
    cout << endl;
    cout << "截至目前,一共有" << i << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto list : audience_info_list)
        list.print();
}

更换测试数据后的运行测试结果:

测试样例1

 

 测试样例2

 

 

实验任务6

 TextCoder.hpp

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

class TextCoder {
public:
    TextCoder(string t);
    string get_ciphertext();
    string get_deciphertext();

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

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

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

}
string text;
void TextCoder::encoder() {
        for (int i = 0; i < text.size(); ++i) {
                if ((text.at(i) >= 'a' && text.at(i) <= 'u') || (text.at(i) >= 'A' && text.at(i) <= 'U'))
                        text.at(i) += 5;
                else if ((text.at(i) >= 'v' && text.at(i) <= 'z') || (text.at(i) >= 'V' && text.at(i) <= 'Z'))
                        text.at(i) -= 21;
        
    }
}
void TextCoder::decoder() {
        for (int i = 0; i < text.size(); ++i) {
             if ((text.at(i) >= 'f' && text.at(i) <= 'z') || (text.at(i) >= 'F' && text.at(i) <= 'Z'))
                     text.at(i) -= 5;
             else if ((text.at(i) >= 'a' && text.at(i) <= 'e') || (text.at(i) >= 'A' && text.at(i) <= 'E'))
                        text.at(i) += 21;
    
    }
}

task6.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();
}

测试用例:

 

posted @ 2022-10-26 13:18  Molancci  阅读(40)  评论(0)    收藏  举报