实验二 数据、指针与C++标准库


 

#include <iostream>
#include <string>
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<< "称呼:    "<< nickname << endl;
        cout<< "联系方式:"<< contact << endl;
        cout<< "所在城市: "<< city << endl;
        cout<< "预定人数: "<< n << endl;
    }
private:
    string nickname;
    string contact;
    string city;
    int n;
};
#include "info.hpp"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int capacity = 100;

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

 

 


#include <iostream>
#include <string>
using namespace std;
class TextCoder {
public:
    TextCoder(string text) :text{ text } {}
    string encoder()
    {
        string res = text;
        for (int i = 0; i < text.length(); 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')) {
                    res[i] = text[i] - 21;
                }
                else {
                    res[i] = text[i] + 5;
                }
            }
        }
        return res;
    }
    string decoder()
    {
        string res = text;
        for (int i = 0; i < text.length(); 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')) {
                    res[i] = text[i] + 21;
                }
                else {
                    res[i] = text[i] - 5;
                }
            }
        }
        return res;
    }
private:
    string text;
};
#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 09:58  玖玖玖璐  阅读(30)  评论(3)    收藏  举报