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

info.h文件源码

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

task5.cpp源码

#include<iostream>
#include<vector>
#include<string>
#include "info.h"
using namespace std;
int main() {
    cout << "录入信息:\n";
    cout << endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
    vector<info> audience_info_list;
    const int capacity=100;
    int nn;
    int sum = 0;
    char choice;
    string name, ncontact, ncity;
    while (cin>>name >> ncontact >> ncity >> nn) {
        info v(name, ncontact, ncity, nn);
        audience_info_list.push_back(v);
        sum += nn;
        if (sum == capacity)break;
        if (sum > capacity) {
            cout << "对不起,只剩" << capacity - sum + nn << "个位置." << endl;
            audience_info_list.pop_back();
            sum -= nn;
            cout << "1.输入u,更新(update)预定信息\n2.输入q,退出预定\n你的选择:";
            cin >> choice;
            if (choice == 'q')break;
        }
    }
    cout << endl;
    cout << "截至目前,一共" << sum << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto & i : audience_info_list)
        i.print();
}

运行结果

 

 

TextCoder.h文件源码

#ifndef TEXTCODER_H
#define TEXTCODER_H
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
class TextCoder {
private:
    string text;
public:
    string encoder();
    string decoder();
    TextCoder(string m) :text(m) {};
};
string TextCoder::encoder() {
    string s = text; int i = 0;
    for (auto& ch : text) {
        if (isalpha(ch)) {
            if (ch >= 'A' && ch <= 'U' || ch >= 'a' && ch <= 'u')
                ch += 5;
            else
                ch -= 21;
            s[i] = ch;
        }
        i++;
    }
    return s;
}
string TextCoder::decoder() {
    string s=text; int i = 0;
    for (auto& ch : text) {
        if (isalpha(ch)) {
            if (ch >= 'F' && ch <= 'Z' || ch >= 'f' && ch <= 'z')
                ch -= 5;
            else
                ch += 21;
            s[i] = ch;
        }
        i++;
    }
    return s;
}
#endif

task6.cpp源码

#include "TextCoder.h"
#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-10-30 21:05  Asll321  阅读(25)  评论(3)    收藏  举报