实验3 数组,指针与现代c++标准库

任务5

实验代码

Info.hpp

#pragma once
#include<iostream>
#include<iomanip>
using namespace std;
class Info
{
private:
    string nickname, contact, city;
    int n;
public:
    Info(string nc, string co, string ci, int n1) :nickname(nc), contact(co), city(ci), n(n1) {
    }
    void print()const {
        cout << std::left << setw(16) << "昵称:   " << setw(16) << nickname << endl;
        cout << std::left << setw(16) << "联系方式:   " << setw(16) << contact << endl;
        cout << std::left << setw(16) << "所在城市:   " << setw(16) << city << endl;
        cout << std::left << setw(16) << "预定人数:   " << setw(16) << n << endl;
    }

};

task5.cpp

#include"Info.hpp"
#include<string>
#include<vector>
#include<iostream>
using namespace std;
vector<Info> audience_info_list;
int main() {
    cout << "录入信息:" << endl;
    cout << "昵称         联系方式(邮箱/手机号)     所在城市      预定参加人数" << endl;
    const int capacity = 100;
    int sum = 0;
    string s1, s2, s3;
    int num;
    int count = 0;
    while (cin >> s1 >> s2 >> s3 >> num) {
        sum += num;
        if (sum > capacity) {
            sum -= num;
            cout << "对不起,只剩" << capacity - sum << "个位置." << endl;
            cout << "1.输入u,更新(update)预定信息" << endl
                << "2.输入q, 退出预定"<<endl<<"你的选择: ";
            string
                f;
            cin >> f;
            if (f == "u")
            {
                cin >> s1 >> s2 >> s3 >> num;
                Info f(s1, s2, s3, num);
                audience_info_list.push_back(f);
                sum+=num;
            }
            else
                break;
        }

        else audience_info_list.push_back(Info(s1, s2, s3, num));
    }
    cout << "截至目前,一共有" << sum << "位听众预定参加。信息如下:" << endl;;
    for (int i = 0; i < audience_info_list.size(); i++)
    {
        audience_info_list.at(i).print();
        cout << endl;
    }
    return 0;
}

测试结果

1

2

任务6

Textcoder.hpp

#include<iostream>
#include<string>
using namespace std;
class TextCoder {
private:
    string text;
    void encoder() {
        int m = text.size();
        int i;
        for (i = 0; i < m; i++) {
            if (text[i] >='A' && text[i] <='Z')
                text[i] = (text[i] + 5 - 65) % 26 + 65;
            if (text[i] >='a' && text[i] <= 'z')
                text[i] = (text[i] + 5 - 97) % 26 + 97;
        }
    }
        void decoder( ) {
            int m = text.length();
            int i;
            for (i = 0; i < m; i++) {
                if (text.at(i) >= 'A' && text.at(i) <= 'Z')
                    text.at(i) = (text.at(i) -5 - 65+26) % 26 + 65;
                if (text[i] >= 'a' && text[i] <= 'z')
                    text.at(i) = (text.at(i) - 5 - 97+26) % 26 + 97;
            }
        }
public :
    TextCoder(string t) :text(t) {};

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

};

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();
}

实验总结:

string.at()的用法还需熟悉

vector的用法还未熟练。

 

posted @ 2022-10-19 20:26  akumanpower  阅读(31)  评论(0)    收藏  举报