实验二 :数组、指针与c++标准库

<task5.cpp>

#include"info.hpp"
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
const int capacity = 100;
int main()
{
    cout << "录入信息:" << endl
        << "称呼, 联系方式, 所在城市 ,参加人数" << endl;
    vector<info>audinece_info_list;
    string a, b, c;
    int d, t = 1, num = 0;//t用来判断更新,还是退出
    while (t && num<100 && cin >> a >> b >> c >> d)
    {
        if (num + d > capacity)
        {
            cout << "对不起,只剩" << capacity - num << "个位置。" << endl
                << "1.输入u,更新预定信息" << endl
                << "2.输入q,退出预定" << endl
                << "你的选择:";
            getchar();//吞回车
            while (char c = getchar())
            {
                getchar();//吞回车
                if (c == 'u')
                {
                    t = 1;
                    cout << "称呼, 联系方式, 所在城市 ,参加人数" << endl;
                    break;
                }
                else if (c == 'q')
                {
                    t = 0;
                    break;
                }
                else 
                {
                    cout << "输入错误,重新选择:";
                    continue;
                }
            }
        }
        else
        {
            num += d;
            info l(a, b, c, d);
            audinece_info_list.push_back(l);
        }
    }
    cout << "目前一共" << num << "位听众预定,信息如下:" << endl;
    for (auto it = audinece_info_list.begin(); it != audinece_info_list.end(); ++it)
        it->print();
}

“info.hpp”

#ifndef INFO_HPP
#define INFO_HPP
#include<iostream>
#include<string>
using namespace std;
class info {
private:
    string nickname, contact, city;
    int n;
public:
    info(string a,string b,string c,int d):nickname(a),contact(b),city(c),n(d){}
    ~info(){}
    void print()
    {
        cout << "称呼:" << nickname << endl 
            << "联系方式:" << contact << endl 
            << "所在城市:" << city << endl
            << "预定人数:" << n << endl;
    }
};

#endif 

测试结果1:

 

 结果2:

 

 

<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"

#include<iostream>
#include<string>
using namespace std;
class TextCoder {
private:
    string text;
public:
    TextCoder(string a):text(a){}
    ~TextCoder(){}
    string encoder() 
    {
        for (int i = 0; i < text.size(); i++)
            if (text[i] >= 'a' && text[i] <= 'z')
                text[i] = (text[i] - 'a' + 5) % 26 + 'a';
            else if (text[i] >= 'A' && text[i] <= 'Z')
                text[i] = (text[i] - 'A' + 5) % 26 + 'A';
        return text;
    }
    string decoder() 
    {
        for (int i = 0; i < text.size(); i++)
            if (text[i] >= 'a' && text[i] <= 'z')
                text[i] = (text[i] - 'a' - 5 + 26) % 26 + 'a';//+26防止为负数
            else if (text[i] >= 'A' && text[i] <= 'Z')
                text[i] = (text[i] - 'A' - 5 + 26) % 26 + 'A';
        return text;
    }
};

测试结果:

 

posted @ 2021-10-30 12:55  KIDX2  阅读(43)  评论(3编辑  收藏  举报