实验三:数组、指针与现代C++

实验任务五

task.cpp

#include"info.h"
#include<iostream>
#include<string>
#include<vector>

using namespace std;

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

info.hpp

#pragma once
#include<iostream>
#include<iomanip> 
#include<string>
using namespace std;
class Info{
    public:
        Info(string name0,string contact0,string city0,int n):nickname{name0},contact{contact0},city{city0},n{n}{}
        void print();
    private:
        string nickname,contact,city;
        int n;
};
void Info::print()
{
    cout<<setw(10)<<"昵称:\t"<<nickname<<endl;
    cout<<setw(10)<<"联系方式:\t "<<contact<<endl;
    cout<<setw(10)<<"所在城市:\t "<<city<<endl;
    cout<<setw(10)<<"预定人数:\t "<<n<<endl;
}

 

 

 

 

 

 实验任务六

task6.cpp

#include"textcoder.h"
#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();
}

textcoder.hpp

#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();
};
void TextCoder::encoder(){
    int length=text.length();
    for(int i=0;i<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 length=text.length();
    for(int i=0;i<length;i++){
        if(text.at(i)>='f'&&text.at(i)<='z')
        text.at(i)-=5;
        else if(text.at(i)>='a'&&text.at(i)<='f')
        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)<='F')
        text.at(i)+=21;
    }
}

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

string TextCoder::get_deciphertext(){
    decoder();
    return text;
}

 

posted @ 2022-10-23 16:14  熊二想吃蜂蜜  阅读(40)  评论(0)    收藏  举报