实验三

试验任务五

头文件

#pragma once
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
using namespace std;
class Info {
public:
    Info() {}
    Info(string a, string b, string c, int d) :nickname{ a }, contrat{ b }, city{ c }, n{ d } {}

    void print();


private:
    string nickname;
    string contrat;
    string city;
    int n;
};

void Info::print() {
    cout << left << setw(16) << "昵称:" << setw(16) << nickname << endl;
    cout << setw(16) << "联系方式:" << setw(16) << contrat << endl;
    cout << setw(16) << "所在城市:" << setw(16) << city << endl;
    cout << setw(16) << "预定人数:" << setw(16) << n << endl << endl;
}

源码

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

using namespace std;



const int capacity = 100;


int main() {
    Info po("", "", "", 125);
    int all=0;
    vector<Info> audience_info_list;
    cout << "昵称" <<" \t\t" << "联系方式 (邮箱/手机号)" << "\t\t" << "所在城市" << "\t\t" << "预定参加人数" << "\t\t" << endl;
    string name, con, ci;
    int k=0;
    char pmx;
    while (cin >> name)
    {
         cin >> con>>ci>>k;
        all = all + k;
        if (all <= 100) {
            Info p(name, con, ci, k);
            audience_info_list.push_back(p);
        }
        else
        {
            cout << "对不起只剩" <<capacity-all+k<< "个位置" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            cin >> pmx;
            if (pmx == 'u')
            {
                all = all - k;
                continue;
            }
            else
            {
                all = all - k;
                break;
            }


        }

    }
    
    cout << "截至目前,共有" << all << "位听众参与。预定听众信息如下:" << endl;

    for (int i = 0; i < audience_info_list.size(); i++) {
        audience_info_list.at(i).print();
        cout << endl;
    }

}

 

 

 

 

 

试验任务六

头文件

#pragma once
#include<string>
using namespace std;
class TextCoder {
public:
    TextCoder(string h) :text(h) {};
    string get_ciphertext();
    string get_deciphertext();
private:
    string text;
    void encoder();
    void decoder();

};

void  TextCoder::encoder() {
     
    int num = text.length();
        for (int i = 0; i < num; i++)
        {
            if (text.at(i) >= 'a' && text.at(i) <= 'u')
            {
                text.at(i) =text.at(i)+5;
            }
            else if (text.at(i) >= 'v' && text.at(i) <= 'z')
            {
                text.at(i) = text.at(i) - 21;
            }
            else if(text.at(i) >= 'A' && text.at(i) <= 'U')
            {
                text.at(i) = text.at(i) + 5;
            }
            else if (text.at(i) >= 'V' && text.at(i) <= 'Z')
            {
                text.at(i) = text.at(i) - 21;
            }
        }

}

void  TextCoder::decoder() {

    int num = text.length();
    for (int i = 0; i < num; i++)
    {
        if (text.at(i) >= 'f' && text.at(i) <= 'z')
        {
            text.at(i) = text.at(i) -5;
        }
        else if (text.at(i) >= 'a' && text.at(i) <= 'e')
        {
            text.at(i) = text.at(i) + 21;
        }
        else if (text.at(i) >= 'F' && text.at(i) <= 'Z')
        {
            text.at(i) = text.at(i) - 5;
        }
        else if (text.at(i) >= 'A' && text.at(i) <= 'E')
        {
            text.at(i) = text.at(i) +21;
        }
    }

}

string TextCoder::get_ciphertext() {

    encoder();
    return text;


}
string TextCoder::get_deciphertext() {

    decoder();
    return text;


}

源码

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

 

 

 

实验总结

试验任务六 老师的test可以完全实验 但如果改变test中加密解密的顺序 答案就会有问题

 

posted @ 2022-10-20 17:08  zwygyyds  阅读(49)  评论(0编辑  收藏  举报