实验3.5

#pragma once
#include<iostream>
#include<iomanip>
using namespace std;
class Info {
public:
    Info() {};
    Info(string nickname0, string contact0, string city0, int N) :nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ N }{};
    ~Info() {};
    void print() const;
private:
    string nickname, contact, city;
    int n;
};
//print()函数接口
void Info::print() const {
    cout << setw(12) << left
    <<setw(12)<< "昵称:" << nickname << endl
    <<setw(12)<< "联系方式:" << contact << endl
    <<setw(12)<< "所在城市:" << city << endl
    <<setw(12)<< "预定人数:" << n << endl
    << endl;
}

#include<iostream>
#include<vector>
#include<string>
#include<iomanip>
#include"info.h"
using namespace std;
const int capacity = 100;//最大人数

//输入信息接口
void input(vector<Info>&audience_info_list) {
    cout << "录入信息:" << endl<<endl;
    cout << setw(12) << left << "昵称";
    cout << setw(30)<<"联系方式(邮箱/手机号)";
    cout << setw(20)<<"所在城市";
    cout<<"预定参加人数" << endl;
    string nickname0, contact0, city0;
    int N,sum=0;
    while(cin>>nickname0){
        cin>> contact0 >> city0 >> N;
        sum += N;
        if (sum > capacity) {
            sum -= N;
            string option;
            cout << "对不起,只剩" << 100-sum << "个位置"<<endl;
            cout << "1.输入u,更新(update)预定位置" << endl;
            cout << "2.输入q,退出(quit)预定" << endl;
            cout << "你的选择:";
            cin >> option;
            if (option == "u") {
                continue;
            }
            if (option == "q") {
                break;
            }
        }
        else {
            Info persons(nickname0, contact0, city0, N);
            audience_info_list.push_back(persons);
        }
    }
    cout << endl << "截止目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
}

//输入信息接口
void output(vector<Info>& audience_info_list) {
    for (auto &info :audience_info_list) {
        info.print();
    }
}
int main() {
    vector<Info> audience_info_list;
    input(audience_info_list);
    output(audience_info_list);
    return 0;
}

实验3.6

#pragma once
#include<string>
#include<iostream>
using namespace std;
class TextCoder {
public:
    TextCoder(string text0):text{ text0 }{};
    ~TextCoder() {};
    string get_ciphertext();
    string get_deciphertext();
private:
    string text;
    string encoder_text,decoder_text;
    const int move = 5;
    void encoder();
    void decoder();
};

//函数get_ciphertext()接口
string TextCoder::get_ciphertext() {
    encoder();
    return encoder_text;
}

//函数get_deciphertext()接口
string TextCoder::get_deciphertext() {
    decoder();
    return decoder_text;
}

//函数encoder()接口
void TextCoder::encoder() {
    encoder_text = text;
    for (int i = 0; i < encoder_text.length(); i++) {
        if (encoder_text[i] >= 65 && encoder_text[i] <= 90) {
            encoder_text[i] = (encoder_text[i] - 65 + move) % 26 + 65;
        }
        else if (encoder_text[i] >= 97 && encoder_text[i] <= 122) {
            encoder_text[i]= (encoder_text[i] - 97 + move) % 26 + 97;
        }
    }
}

//函数decoder()接口
void TextCoder::decoder() {
    decoder_text = text;
    for (int i = 0; i < decoder_text.length(); i++) {
        if (decoder_text[i] >= 65 && decoder_text[i] <= 90) {
            decoder_text[i] = 90 - (90 - decoder_text[i] + move) % 26;
        }
        else if (decoder_text[i] >= 97 && decoder_text[i] <= 122) {
            decoder_text[i] = 122 - (122 - decoder_text[i] + move) % 26;
        }
    }
}

#include "textcoder.h"
#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 on 2022-10-19 21:40  都说了没有名字  阅读(24)  评论(0编辑  收藏  举报