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

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

时间:2022-10-23 23:55:34浏览次数:42  
标签:city string text void C++ 数组 include nickname 指针

实验任务5

Info.h

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

using namespace std;

class Info {
public:
    Info(){}
    Info(string nickname0, string contact0,string city0,int n0):
        nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ n0 } {
        
    }
    void print();

    void set_nickname(string nickname1) {
        nickname=nickname1;
    }
    void set_contact(string contact1) {
        contact = contact1;
    }
    void set_city(string city1) {
        city = city1;
    }
    void set_n(int n1) {
        n = n1;
    }
private:
    string nickname, contact, city;
    int n;
};

void Info::print() {
    cout<<"昵称:        " << nickname << endl
        <<"联系方式:    " << contact << endl
        <<"所在城市:    " << city << endl
        <<"预定人数:    " << n << endl;
}

task5.cpp

#include"Info.h"
#include<iostream>
#include<vector>
#include<iomanip>

using namespace std;

int main()
{
    string nickname, contact, city, my_choice;
    int i = 0, n, m = 1, sum = 0;
    const int capacity = 100;
    vector<Info> audience_info_list(100);
    cout << "录入信息:" << endl;
    cout <<left<< setw(15) << "昵称" << setw(30) << "联系方式(邮箱/手机号)" << setw(15) << "所在城市" << setw(15) << "预定参加人数" << endl;
    while (cin >> nickname >> contact >> city >> n) {
        if (n <= capacity - sum ) {
            audience_info_list[i].set_nickname(nickname);
            audience_info_list[i].set_contact(contact);
            audience_info_list[i].set_city(city);
            audience_info_list[i].set_n(n);
            sum += n;
            i++;
        }
        else {
            cout << endl << "对不起,只剩" << capacity - sum << "个位置." << endl;
            cout << "1.输出u,更新(update)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            while (cin >> my_choice) {
                if (my_choice == "q") {
                    i -= 1;
                    m = 0;
                    break;
                }
                else if (my_choice=="u") {
                    cin >> nickname >> contact >> city >> n;
                    if (n > capacity -sum) {
                        cout << endl << "对不起,只剩" << capacity - sum << "个位置." << endl;
                        cout << "1.输出u,更新(update)预定信息" << endl;
                        cout << "2.输入q,退出预定" << endl;
                        cout << "你的选择:";
                        continue;
                    }
                    audience_info_list[i].set_nickname(nickname);    
                    audience_info_list[i].set_contact(contact);
                    audience_info_list[i].set_city(city);
                    audience_info_list[i].set_n(n);
                    sum += n;
                    i++;
                    break;

                }    
                
            }
        }
        if (m == 0)
            break;
    }
    cout << endl << "截至目前,一共有" << n << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto p = 0; p<=i ; p++) {
        audience_info_list[p].print();
        cout << endl;
    }
    return 0;
}

运行测试结果:

 实验任务6

textcoder.hpp

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

using namespace std;

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

private:
    void encoder() {
        for (auto i = 0; i < text.length(); i++) {
            if (text.at(i) >= 'a' && text.at(i) <= 'z') {
                text.at(i) = (text.at(i) - 'a' + 5) % 26 + 'a';
            }
        }
        for (auto i = 0; i < text.length(); i++) {
            if (text.at(i) >= 'A' && text.at(i) <= 'Z') {
                text.at(i) = (text.at(i) - 'A' + 5) % 26 + 'A';
            }
        }
    }
    
    void decoder() {
        for (auto i = 0; i < text.length(); i++) {
            if (text.at(i) >= 'a' && text.at(i) <= 'z') {
                text.at(i) = (text.at(i) - 'a' + 21) % 26 + 'a';
            }
        }
        for (auto i = 0; i < text.length(); i++) {
            if (text.at(i) >= 'A' && text.at(i) <= 'Z') {
                text.at(i) = (text.at(i) - 'A' +21) % 26 + 'A';
            }
        }
    }

private:
    string text;
};
string TextCoder::get_deciphertext() {
    decoder();
    return text;
}
string TextCoder::get_ciphertext() {
    encoder();
    return text;
}

tast6.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 << "加密后英文文本:" << encoded_text << endl;
        decoded_text = TextCoder(encoded_text).get_deciphertext();
        cout << "解密后英文文本:" << decoded_text << endl;
        cout << "\n输入英文文本:";
    }
}
int main() {
    test();
}

运行测试结果:

实验总结:

在实验任务5中,在对类对象vector<Info>audience_info_list赋值时,最终通过定义函数来实现。

在实验任务6中,在解密时,利用 text.at(i) = (text.at(i) - 'A' - 5) % 26 + 'A'; 出现错误,修改为 text.at(i) - 'a' + 21) % 26 + 'a';在test函数中,若\t存在,无法与预期测试结果一致,故删除。

标签:city,string,text,void,C++,数组,include,nickname,指针
From: https://www.cnblogs.com/zxy0324/p/16819777.html

相关文章