实验二

任务五

info.hpp

#ifndef INFO_hpp
#define INFO_hpp
#include <string>
#include <iostream>
#include<limits>
using namespace std;
class Info {
public:
    Info()=default;
    Info(string nickname0, string contact0, string city0 , int n0) :nickname{nickname0}, contact{contact0}, city{city0}, n{n0}{}
    void print() const;
    int get_n() { return n; }
private:
    string nickname;
    string contact;
    string city;
    int n;
};
void Info::print()const {
    cout << "称呼:" << nickname << endl;
    cout << "联系方式:\t" << contact << endl;
    cout << "所在城市:\t" << city << endl;
    cout << "预定人数:\t" << n << endl;
}
#endif

cpp

#include<iostream>
#include<string>
#include<vector>
#include"info.hpp"
int main()
{
    int m = 0,i=0;
    string nickname, contact, city;
    int n,n1;
    using namespace std;
    vector<Info> audience_info_list;
    const int capacity = 100;
    cout << "录入信息:" << endl;
    cout << endl;
    cout<< "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
    while (cin >> nickname >> contact >> city >> n) {
        Info A(nickname, contact, city, n);
        n1 = A.get_n();
        if (n1 <= (capacity - m)) {
            audience_info_list.push_back(A);
            m += n;
            continue;
        }
        else {
            cout << "对不起,只剩" << capacity - m << "个位置" << endl;
            cout << "1.输入u,更新预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            char x;
            cin >> x;
            if (x == 'u') {
                continue;
            }
            else if (x == 'q') {
                break;
            }
        }
        i++;
    }
    cout << endl << "截至目前,一共有" << m << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto it = audience_info_list.begin(); it != audience_info_list.end(); it++)
        (*it).print();
}

 

 

 任务六

textcoder.hpp

 

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP
#include<iostream>
#include<string>
using namespace std;
class TextCoder {
public:
    TextCoder(string text0):text{text0}{}
    string encoder();
    string decoder();
private:
    string text;
};
string TextCoder::encoder() {
    string s1 = "abcdefghijklmnopqrstuvwxyzabcde";
    string text1 = text;
    for (unsigned int i = 0; i < text.size(); i++) {
        for (int j = 0; j < 26; j++) {
            if (text[i] == s1[j])
                text1[i] = s1[j + 5];
        }
    }
    string s2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE";
    for (unsigned int i1 = 0; i1 < text.size(); i1++) {
        for (int j1 = 0; j1 < 26; j1++) {
            if (text[i1] == s2[j1])
                text1[i1] = s2[j1 + 5];
        }
    }
    return text1;
}
string TextCoder::decoder() {
    string text1 = text;
    string s3 = "abcdefghijklmnopqrstuvwxyzabcde";
    for (unsigned int h = 0; h < text.size(); h++) {
        for (int m = 5; m < 31; m++) {
            if (text[h] == s3[m])
                text1[h] = s3[m- 5];
        }
    }
    string s4= "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE";
    for (unsigned int p = 0; p < text.size(); p++) {
        for (int m= 5; m< 31; m++) {
            if (text[p] == s4[m])
                text1[p] = s4[m - 5];
        }
    }
    return text1;
}
#endif 

 

cpp

 

#include "textcoder.hpp"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = TextCoder(text).encoder();  // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;

        decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

 

 

 

posted @ 2021-11-03 13:01  向胃猛冲  阅读(23)  评论(3)    收藏  举报