实验三

task5:

Info.hpp

#pragma once

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

class Info {
public:
    Info(string nick,string cont,string cit,int n0) :nickname{nick},contact{cont},city{cit},n{n0}{}
    void print() const;

private:
    string nickname, contact, city;
    int n;
};
void Info::print() const{
    cout << left << setw(8) << "昵称:" << nickname<<endl;
    cout << left << setw(8) << "联系方式:" << contact<<endl;
    cout << left << setw(8) << "所在城市:" << city<<endl;
    cout << left << setw(8) << "预定人数:" << n<<endl;
    cout << endl;
}

task5.cpp

#include"Info.hpp"
#include<iostream>
#include<vector>
using namespace std;

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

测试结果1:

测试结果2:

task6

Textcoder.hpp

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

class Textcoder {
public:
    Textcoder(string t): text{t}{}
    string get_ciphertext() { encoder(); return text; }
    string get_deciphertext() { decoder(); return text; }
private:
    string text;
    void encoder();
    void decoder();
 };
void Textcoder::encoder() {
    for (int i = 0; i < text.size(); i++) {
        if (('a' <= text[i] && text[i] <= 'z') || ('A' <= text[i] && text[i] <= 'Z')) {
            if (('v' <= text[i] && 'z' >= text[i]) || ('V' <= text[i] && 'Z' >= text[i])) {
                text[i] -= 21;
            }
            else {
                text[i] += 5;
            }
        }
    }
}
void Textcoder::decoder() {
        for (int i = 0; i < text.size(); i++) {
            if (('a' <= text[i] && text[i] <= 'z') || ('A' <= text[i] && text[i] <= 'Z')) {
                if (('f' <= text[i] && 'z' >= text[i]) || ('F' <= text[i] && 'Z' >= text[i])) {
                    text[i] -= 5;
                }
                else {
                    text[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 00:02  只是LL  阅读(32)  评论(0)    收藏  举报