实验三 数组、指针与现代c++标准库

task5

#pragma once

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

using namespace std;

class Info {
    public:
    Info(string nickname0, string contact0, string city0, int n0);
    void print();
    string nickname, contact, city;
    int n;
};

Info::Info(string nickname0, string contact0, string city0, int n0) :nickname{nickname0}, contact{contact0}, city{city0}, n{n0} {}

void Info::print(){
    cout << "昵称:     " << left << setw(10) << nickname << endl;
    cout << "联系方式: " << left << setw(10) << contact << endl;
    cout << "所在城市: " << left << setw(10) << city << endl;
    cout << "预订人数: " << left << setw(10) << n << "\n" << endl; 
}
#include "实验三task5_info.hpp"
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

class Show{
    public:
        void Show_get();
        void Show_put();
        const int capacity = 100;
        int k;
        vector<Info> audience_info_list;
        
};

void Show::Show_get() {
    string name,con,city;
    int num;
    k = 0;
    while (cin >> name)
    {
        cin >> con >> city >> num;
        k += num;
        if(k > capacity)
        {
            k -= num;
            cout << "对不起,只剩" << capacity - k << "个位置。" << endl;
            cout << "1.输入u,更新(update)预定信息\n" << "2.输入q,退出预定\n"  << "你的选择:";
            char a;
            cin >> a;
            if(a == 'q')
            break;
            else if(a == 'u')
            continue;
        } 
        Info info(name, con, city, num);
        audience_info_list.push_back(info); 
    }
}

void Show::Show_put() {
    for(auto information:audience_info_list)
    information.print();
}

int main()
{
    Show s;
    cout << "录入信息: " << endl;
    cout << endl;
    cout << left << setw(15) << "昵称" << right << setw(15) << "联系方式(邮箱、手机号)" << setw(15) << "所在城市" << setw(15) << "预定参加人数" << endl;
    s.Show_get();
    cout << "截至目前,一共有" <<  s.k << "位听众预定参加。预定听众信息如下:" << endl; 
    s.Show_put() ;
}

 

 

 

 task6

#pragma once

#include <iostream>
#include <string>

using namespace std;
 
class TextCoder {
    public:
        TextCoder(string t): text{t} {};
        string get_ciphertext() { 
        encoder();
        return text; };
        string get_deciphertext() { 
        decoder();
        return text; };
        
    private:
        string text;
        void encoder();
        void decoder();
};

void TextCoder::encoder(){
     for(auto i = 0; i < text.size(); ++i)
     {
             if(text[i] >= 'v' && text[i] <= 'z' || text[i] >= 'V' && text[i] <= 'Z')
                text[i] = char(text[i] - 21);
             else if(text[i] >='A' && text[i] <= 'z')
                text[i] = char(text[i] + 5);  
     }
}

void TextCoder::decoder(){
     for(auto i = 0; i < text.size(); ++i)
     {
             if(text[i] >= 'a' && text[i] <= 'e' || text[i] >= 'A' && text[i] <= 'Z')
                text[i] = char(text[i] + 21);
             else if(text[i] >='A' && text[i] <= 'z')
                text[i] = char(text[i] - 5 );  
     }
}
#include "实验三test6_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 << "加密后英文文本:" << encoded_text << endl;
        
        decoded_text = TextCoder(encoded_text).get_deciphertex

 

 

        cout << "解密后英文文本:" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    } 
}

int main() {
    test();
}

 

posted @ 2022-10-23 10:46  .木枝  阅读(33)  评论(0)    收藏  举报