首页 > 编程语言 >实验三 数组、指针与现代C++标准库

实验三 数组、指针与现代C++标准库

时间:2022-10-24 13:22:06浏览次数:47  
标签:ch string int text number C++ 数组 include 指针

实验任务五

Info.hpp

#pragma once
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

class Info {
private:
    string nickname;
    string contact;
    string city;
    int n;
    static int total_number;
public:
    Info(string _nickname, string _contact, string _city, int _n);
    void print();
    static int get_total_number();
};

int Info::total_number = 0;

Info::Info(string _nickname, string _contact, string _city, int _n) : nickname(_nickname), contact(_contact), city(_city), n(_n) {
    total_number += n;
}

int Info::get_total_number() {
    return total_number;
}
void Info::print() {
    cout << setiosflags(ios::left);
    cout << setw(10) << "称呼:" << nickname << endl;
    cout << setw(10) << "联系方式:" << contact << endl;
    cout << setw(10) << "所在城市:" << city << endl;
    cout << setw(10) << "预定人数:" << n << endl;

}

task5.cpp

#include "info.h"
#include <vector>

void say_book();
void show_select();

int main() {
    vector<Info> audience_info_list;
    const int capacity = 100;
    string id, contract, city;
    int attend_number;

    cout << "录入信息:\n" << endl;
    say_book();

    while (getline(cin, id, ' ') && getline(cin, contract, ' ') && getline(cin, city, ' ')) {
        cin >> attend_number;

        if (Info::get_total_number() + attend_number <= capacity) {
            Info order(id, contract, city, attend_number);
            audience_info_list.push_back(order);
        }
        else {
            cout << "对不起,只剩" << capacity - Info::get_total_number() << "个位置" << endl;
            show_select();
            char ch;
            cin >> ch;
            while (ch != 'q' && ch != 'u') {
                cout << "输入错误,请重试" << endl;
                show_select();
                cin >> ch;
            }
            if (ch == 'q') {
                break;
            }
            else {
                cout << "继续预定" << endl;
                say_book();
            }
        }
    }
    cout << "截至目前,一共有" << Info::get_total_number() << "位观众预订参加。预订听众信息如下:" << endl;

    for (int i = 0; i < audience_info_list.size(); i++) {
        audience_info_list[i].print();
    }
}

void say_book() {
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
}

void show_select() {
    cout << "1.输入u,更新(update)预定信息" << endl;
    cout << "2.输入q,退出预订" << endl;
    cout << "你的选择:" << endl;
}

image


实验任务六

TextCoder.hpp

#pragma once
#include <iostream>
#include <string>
using namespace std;

class TextCoder {
private:
    string text;
public:
    TextCoder(string _text);
    string get_ciphertext();
    string get_deciphertext();
};

TextCoder::TextCoder(string _text) : text(_text) {

}

string TextCoder::get_ciphertext() {
    string answer;

    for (auto ch : text) {
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
            ch += 5;
            if ((ch < 'a' && ch > 'U') || ch > 'z') {
                ch -= 26;
            }
        }
        answer += ch;
    }
    return answer;
}

string TextCoder::get_deciphertext() {
    string answer;

    for (auto ch : text) {
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
            ch -= 5;
            if (ch < 'A' || (ch > 'Z' && ch < 'a')) {
                ch += 26;
            }
        }
        answer += ch;
    }
    return answer;
}

task6.cpp

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

void test() {
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text)) {
        encoded_text = TextCoder(text).get_ciphertext();  // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;

        decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

int main() {  
    test(); 
}

image

标签:ch,string,int,text,number,C++,数组,include,指针
From: https://www.cnblogs.com/Cali-AKA/p/16806936.html

相关文章