task6.cpp

#include"textcoder.hpp"
#include<iostream>
#include<string>

int main()
{
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = Textcoder(text).encoder();
        cout << "加密后的文本:\t" << encoded_text << endl;

        decoded_text = (Textcoder(encoded_text)).decoder();

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

textcoder.hpp

#pragma once
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Textcoder
{
public:
    Textcoder(string _text) { text = _text; }
    Textcoder(const Textcoder& T) { text = T.text; }
    ~Textcoder(){}

    string encoder();
    string decoder();


private:
     string text;
};

 string Textcoder::encoder()
 {
     string encoded_text = "";
     

     for (int i = 0; i < text.size(); i++)
     {
         char ch = text[i];
         if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z'))
         {
             if ((ch >= 'V' && ch <= 'Z') || (ch >= 'v'&&ch<='z')) {
                 ch -= 26;
             }
             encoded_text += ch + 5;
         }
         else
         {
             encoded_text += ch;
         }
     }
     return  encoded_text;
 }
string Textcoder::decoder()
{
    string decoded_text="";
    for (auto ch : text)
    {
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
        {
            if ((ch >= 'A' && ch <= 'E') || (ch >= 'a' && ch <= 'e'))
            {
                ch += 26;
            }
            decoded_text += ch - 5;
        }
        else
        {
            decoded_text += ch;
        }
    }
        return  decoded_text;
}

实验2 数组、指针与C++标准库_其他

 

 

 

task 5

task5.cpp

#include<iostream>
#include<string>
#include<vector>
#include"info.h"
using namespace std;
int main()
{
    const int capacity = 100;
    vector<Info>audience_info_list;
    string nickname, contact, city;
    int n;
    cout << "录入信息" << endl << endl<<endl;
    cout << "称呼、昵称, 联系方式(邮箱、手机号), 所在城市, 预定参见人数" << endl;
    while (getline(cin, nickname,' ') && getline(cin,city,' ') && getline(cin,contact,' ')) {
        {
            cin >> n;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            if (n + Info::getcount() <= capacity)
            {
                Info information(nickname, contact, city, n);
                audience_info_list.push_back(information);
            }
            else
            {
                cout << "对不起,只剩" << capacity - Info::getcount() << "个座位." << endl;
                cout << "1. 输入u,更新(update)预定信息" << endl;
                cout << "2. 输入q,退出预定" << endl;
                cout << "你的选择: " ;
                char ch;
                cin >> ch;
                if (ch == 'q' || ch == 'u')
                {
                    if (ch == 'q')
                    {
                        break;
                    }
                    if (ch == 'u')
                    {
                        continue;
                    }
                }

            }
                
        }
    }
    cout << "截至目前,一共有 " << Info::getcount() << "位听众预定参加。预定参加听众信息如下: " << endl;
     
    for (int i = 0; i < audience_info_list.size(); i++)
    {
        audience_info_list.at(i).print();
    }
}

info.h

#pragma once
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Info
{
public:
    Info(string _nickname, string _contact, string _city, int _n);
    Info(const Info& I);
    ~Info() { count -= n; }
    static int getcount() { return count; }
    void print();
private:
    static  int count;

    string nickname, city, contact;

    int n;
};

int Info::count = 0;

Info::Info(string _nickname, string _contact, string _city, int _n)
{
    nickname = _nickname;
    contact = _contact;
    city = _city;
    n = _n;
    count += n;
}
Info::Info(const Info& I)
{
    nickname = I.nickname;
    contact = I.contact;
    city = I.city;
    n = I.n;
    count += n;
}

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

实验2 数组、指针与C++标准库_其他_02

 

 实验2 数组、指针与C++标准库_i++_03