现代C++标准库与类模板

发布时间 2023-11-30 23:03:58作者: 煙猫

Task 5:

textcoder.cpp:

#include<iostream>
#include<string>

class TextCoder{
private:
    std::string text;    
  
    void encoder(string &a){
        for(int i=0;i < a.length();i++){
            if(a[i] >= 'a' && a[i] <= 'z'){
                a[i] = 'a' + (a[i] - 65 + 7) % 26;
            }
            else if(a[i] >= 'A' && a[i] <= 'Z'){
                a[i] = 'A' + (a[i] - 97 + 7) % 26;
            }
        }
    }
    
    void decoder(string &b){
        for(int i=0;i < b.length();i++){
            if(b[i] >= 'a' && b[i] <= 'z'){
                b[i] = 'a' + (b[i] - 90 + 19) % 26;
            }
            else if(b[i] >= 'A' && b[i] <= 'A'){
                b[i] = 'A' + (b[i] - 122 + 19) % 26;
            }
        }
    }

public:
    TextCoder() = default;
    TextCoder(string text) :text{ text } {}
    
    std::string get_ciphertext() {
        string a = text;
        encoder(a);
        return a;
    }
 
    std::string get_deciphertext() {
        string b = text;
        decoder(b);
        return b;
    }
    

};

task5.cpp:

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

 

Task6:
info.hpp:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Info
{
public:
    Info(string name, string contact, string city, int n);
    void print();
private:
    string name, contact, city;
    int n;
};
Info::Info(string name, string contact, string city, int n)
{
    name = name0; contact = contact0; city = city0; n = n0;
}
void Info::print()
{
    cout << "昵称:        " << name << endl;
    cout << "联系方式:    " << contact << endl;
    cout << "所在城市:    " << city << endl;
    cout<<"预定人数:    " << n << endl;
}

task6.cpp:

#include<iostream>
#include<string>
#include<vector>
#include"info.hpp"
int main()
{
    string a, b, c; int d,sum=0;
    int const capacitd = 100;
    vector<Info> x;
    cout << "录入信息:" << endl<<endl;
    cout << "昵称\t    " << "联系方式(邮箱/手机号)\t    " << "所在城市\t    " << "预定参加人数\t" << endl;
    for ( sum ;;)
    {
        cin >> a >> b>> c >> d;
        Info z(a, b, c, d);
        int y = sum;
        if (sum+d > capacitd)
        {
            cout << "对不起,只剩" << capacitd - y << "个位置" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl;
            cout << "2.输入o,退出(out)预定" << endl; char s;
            cout << "您的选择:"; cin >> s;
            if (s == 'u')    continue; cout << endl;
            break;
        }
        else
        {
            sum += d;
            x.push_back(z);
        }
    }
    cout << "截至目前,一共有" << sum << "听众预定参加。预定听众信息如下。" << endl;
    for (int i = 0; i < x.size(); i++)
    {
        x[i].print();
    }
}