实验四

实验任务5

#pragma once 

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

class TextCoder {
public:
    TextCoder() = default; 
    TextCoder(string str);
    string get_ciphertext();
    string get_deciphertext();
    ~TextCoder() = default;

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

TextCoder::TextCoder(string str) {
    text = str;
}

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');
        }
    }
}
TextCoder.hpp
#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(); 
}
task5.cpp

 

实验任务6

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

class info{
private:
    string nickname;
    string contact;
    string city;
    int n;
public:
    info(string n1,string c,string city1,int n2){
        nickname=n1;
        contact=c;
        city=city1;
        n=n2;
        
    }
    void print(){
    cout<<"昵称:\t\t"<<nickname<<endl;
    cout<<"联系方式:\t"<<contact<<endl;
    cout<<"所在城市:\t"<<city<<endl;
    cout<<"预定人数:\t"<<n<<endl;
    }
};
info.hpp
#include<iostream>
#include<string>
#include<vector>
using namespace std;

class info{
private:
    string nickname;
    string contact;
    string city;
    int n;
public:
    info(string n1,string c,string city1,int n2){
        nickname=n1;
        contact=c;
        city=city1;
        n=n2;
        
    }
    void print(){
    cout<<"昵称:\t\t"<<nickname<<endl;
    cout<<"联系方式:\t"<<contact<<endl;
    cout<<"所在城市:\t"<<city<<endl;
    cout<<"预定人数:\t"<<n<<endl;
    }
};
task6.cpp

实验结果测试

 

posted @ 2023-11-30 22:16  grcvafg  阅读(23)  评论(0)    收藏  举报