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

实验任务五

info.hpp

#ifndef info_hpp
#define info_hpp
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class info {
public:
    info(string _nickname_, string _contact_, string _city_, int _n_) :
        nickname { _nickname_ }, contact { _contact_ }, city { _city_ }, n { _n_ }
    {
    }
    void print()
    {
        cout.setf(ios::left);
        cout << setw(10) << "称呼:    " << setw(20) << nickname << endl;
        cout << setw(10) << "联系方式:" << setw(20) << contact << endl;
        cout << setw(10) << "所在城市:" << setw(20) << city << endl;
        cout << setw(10) << "预定人数:" << setw(20) << n << endl;        
    }
private:
    string nickname;
    string contact;
    string city;
    int n;
};
#endif

task5.cpp

#include <iostream>
#include <vector>
#include <string>
#include "info.hpp"
#define endl "\n";
using namespace std;
const int capacity = 100;
string name, tel, city;
int num;
int cnt;

int main()
{
    vector<info> audience_info_list;
    cout << "录入信息:" << endl;
    cout << endl;
    cout << "称呼/昵称,"
        << "联系方式(邮箱/手机号),"
        << "所在城市,"
        << "预定参加人数" << endl;
    while ( cin >> name >> tel >> city >> num) {
        if ( cnt + num > capacity ) {
            cout << "对不起,只剩"
                << capacity - cnt
                << "个位置" << endl;
            cout << "1.输入u,更新预订信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            char temp[5];
            cin >> temp;
            if ( temp[0] == 'q' ) {
                cout << endl;
                break;
            }
            else {
                cout << "请重新输入:" << endl;
                continue;
            }
        }
        cnt += num;
        info tt(name, tel, city, num);
        audience_info_list.push_back(tt);
    }
    cout << "截至目前,一共有" << cnt
        << "位听众预定参加。预定听众信息如下:" << endl;
    for ( int i = 0;i < (int)audience_info_list.size();i++ ) {
        audience_info_list[i].print();
    }
    return 0;
}

运行输出

实验任务六

textcoder.hpp

#ifndef textcoder_hpp
#define textcoder_hpp
#include <iostream>
#include <string>
using namespace std;

class TextCoder {
public:
    TextCoder(string _text) :text { _text }{}
    string encoder()
    {
        string ret = text;
        int lens = text.length();
        for ( int i = 0;i < lens;i++ ) {
            if ( (text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z') ) {
                if ( (text[i] > 'u' && text[i] <= 'z') || (text[i] > 'U' && text[i] <= 'Z') ) {
                    ret[i] = text[i] - 21;
                }
                else {
                    ret[i] = text[i] + 5;
                }
            }
        }
        return ret;
    }
    string decoder()
    {
        string ret = text;
        int lens = text.length();
        for ( int i = 0;i < lens;i++ ) {
            if ( (text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z') ) {
                if ( (text[i] <= 'e' && text[i] >= 'a') || (text[i] <= 'E' && text[i] >= 'A') ) {
                    ret[i] = text[i] + 21;
                }
                else {
                    ret[i] = text[i] - 5;
                }
            }
        }
        return ret;
    }
private:
    string text;
};
#endif

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输入英文文本: ";
    }
}

运行输出

posted @ 2021-11-03 11:56  胖头鱼鱼鱼鱼  阅读(17)  评论(2)    收藏  举报