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

实验五

代码:

info.hpp:

#pragma once
#include<iostream>
using namespace std;

class info{
    
    public:
        info(string ni="",string co="",string ci="",int n0=0):nickname{ni},contact{co},city{ci},n{n0} {}
        ~info()=default;
        
        void set_nickname(string n1){ nickname=n1; }
        void set_contact(string c1) { contact=c1; }
        void set_city(string c2) { city=c2; }
        void set_n(int n1) { n=n1; }
        void print();
        
    private:
        string nickname,contact,city;
        int n;
};

void info::print()
{
    cout << "昵称:        " << nickname << endl;
    cout << "联系方式:    " << contact << endl;
    cout << "所在城市:    " << city << endl;
    cout << "预订人数:    " << n << endl;
}

main:

 

#include<iostream>
#include<vector>
#include "info.hpp"
using namespace std;

const int capacity=100;

int main()
{
    int i,n0,sum=0;
    string    name,contact0,city0;
    vector<info> audience_info_list;
    cout << "录入信息:" << endl << endl;
    cout << "昵称      " << "联系方式(邮箱/手机号)      " << "所在城市      " << "预定参加人数      " << endl;
    while(cin >> name)
    {
        cin >> contact0 >> city0 >> n0;
        sum+=n0;
        if(capacity>=sum)
        {
            info p=info(name,contact0,city0,n0);
            audience_info_list.push_back(p);
        }
        else
        {
            char choice;
            sum-=n0;
            cout << "对不起,只剩" << (capacity-sum) << "个位置." << endl;
            cout << "1. 输入u,更新(update)预定信息" << endl;
            cout << "2. 输入q,退出预定" << endl;
            cout << "你的选择: ";
            cin >> choice;
            if(choice='q')
            {
                cout << endl;
                break;
            }    
            else if(choice='u')
            {
                cout << "录入信息: " << endl;
                continue;
            }
        } 
        if(sum==capacity)
            break;
    }
    cout << "截至目前,一共有" << sum << "位听众参加预定,预定信息如下:" << endl;
    for(i=0;i<audience_info_list.size();i++)
    {
        audience_info_list.at(i).print();
        cout << endl;
    } 
}

 

 

运行结果截图:

 

 

实验六

代码:

TextCoder.hpp:

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

class TextCoder{
    
    public:
        TextCoder(string text0=""):text{text0} {}
        
        string get_ciphertext() {encoder();return text;}
        string get_deciphertext() {decoder();return text;}
        
    private:
        string text;
        void encoder();
        void decoder();
}; 
void TextCoder::encoder()
{
    int i;
    for(i=0;i<text.length();i++)
    {
        if(text.at(i)>='a' && text.at(i)<='u')
            text.at(i)+=5;
        else if(text.at(i)>='v' && text.at(i)<='z')
            text.at(i)-=21;
        else if(text.at(i)>='A' && text.at(i)<='U')
            text.at(i)+=5;
        else if(text.at(i)>='V' && text.at(i)<='Z')
            text.at(i)-=21;
    }
}
void TextCoder::decoder() 
{
    int i;
    for(i=0;i<text.length();i++)
    {
        if(text.at(i)>='f' && text.at(i)<='z')
            text.at(i)-=5;
        else if(text.at(i)>='a' && text.at(i)<='e')
            text.at(i)+=21;
        else if(text.at(i)>='F' && text.at(i)<='Z')
            text.at(i)-=5;
        else if(text.at(i)>='A' && text.at(i)<='E')
            text.at(i)+=21;
    }
}

main:

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

运行结果截图:

 

posted @ 2022-10-25 23:19  lovessr  阅读(10)  评论(0编辑  收藏  举报