实验4

任务5

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

using namespace std;

class TextCoder {
public:
    TextCoder(string text0) : text{ text0 } {};

    string get_ciphertext();
    string get_deciphertext();

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

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

string TextCoder::get_deciphertext() {
    decoder();
    return text;
}
void TextCoder::encoder() {
    for (int i = 0; i < text.length(); i++) {
        if (islower(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'a' + 7) % 26 + 'a');
        } else if (isupper(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'A' + 7) % 26 + 'A');
        }
    }
}

void TextCoder::decoder() {
    for (int i = 0; i < text.length(); i++) {
        if (islower(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'a' + 26 - 7) % 26 + 'a');
        } else if (isupper(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'A' + 26 - 7) % 26 + 'A');
        }
    }
}
View Code
#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();
}
View Code

任务6

#pragma once 

#include<iostream>
#include<string>
#include <iomanip>

using namespace std;

class Info {
    private:
        string nickname;
        string contact;
        string city;
        int n;
    public:
        Info(string&,string&,string&,int&);
        void print();
};
Info::Info(string &nickname,string &contact,string &city,int &n) {
    this->nickname = nickname;
    this->contact = contact;
    this->city = city;
    this->n = n;
}
void Info::print() {
    std::cout<<"昵称:\t\t"<<nickname<<std::endl;
    std::cout<<"联系方式:\t"<<contact<<std::endl;
    std::cout<<"所在城市:\t"<<city<<std::endl;
    std::cout<<"预定人数:\t"<<n<<std::endl;
}
void Info::print() const
{
    cout << "昵称:     " << nickname << endl;
    cout << "联系方式:     " << contact << endl;
    cout << "所在城市:     " << city << endl;
    cout << "预定参加人数:     " << n << endl;
}
View Code
#include "Info.hpp"
#include<iostream>
#include<string>
#include <vector>


using namespace std;

int main() {
    const int capacity = 100;
    vector<Info> audience_info_list;
    vector<Info> &v = audience_info_list;
    int sum = 0; 
    cout << "录入信息:" << endl;
    cout << endl;
    cout << "昵称     联系方式(邮箱/手机号)     所在城市     预定参加人数     " << endl;
    char choice;
    int sum=0;
    string nickname, contact, city;
    int n;
    while (cin >> nickname >> contact >> city >> n) {
        if (sum + n <= capacity) {
            Info a(nickname, contact, city, n);
            v.push_back(a);
            sum += n;
        }
        else {
            cout << "对不起,只剩" << capacity - sum << "个位置。\n";
            cout << "1.输入u,更新(updata)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";

            char c;
            cin >> c;
            if (c == 'q') break;
            else if (c == 'u') continue;
            else break;
        }
        if (sum == capacity) break;
    }
    cout << endl;

    cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
    for(auto &Info:audience_info_list)
    {
        Info.print();
    }
}
    }
}
View Code

 

posted @ 2023-12-01 01:36  音也  阅读(16)  评论(0)    收藏  举报