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

实验任务5

#include"Info.hpp"
#include<iostream>
#include<string>
#include<vector>
int main()
{
    string s1,s2= "continue";
    int i = 0, n = 0;
    int const capacity = 100;
    vector<Info> audience_info_list(100);
    cout << "录入信息:" << endl;
    cout << "昵称" << "        " << setw(20) << "联系方式(邮箱/手机号)" << setw(20) << "所在城市" << setw(20) << "预定参加人数" << endl;
    while (s2=="continue")
    {
        audience_info_list[i].get_info();
        n += audience_info_list[i].back_n();
        if (n > capacity)
        {
            n -= audience_info_list[i].back_n();
            cout << "对不起,只剩" << capacity - n << "个位置." << endl;
            cout << "1.输入u,更新预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            cin >> s1;
            if(s1=="u")
                audience_info_list[i].back_n();
            else
                break;
        }
        else
        {
            i++;
            cin >> s2;
        }
    }
    cout << endl;
    cout << "截至目前,一共有" << n << "位听众预定参加,预定听众信息如下:" << endl;
    for (auto j=0;j<i;j++)
    {
        audience_info_list[j].print();
        cout << endl;
    }
}
#pragma once
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Info
{
public:
    Info() = default;
    Info(string name, string cont, string place, int count) :nickmane{ name }, contact{ cont }, city{ place }, n{ count } {}
    void print();
    int back_n() { return n; }
    void get_info();
private:
    int n;
    string  nickmane, contact, city;
};
void Info::print()
{
    cout << "昵称:" <<setw(6) << nickmane << endl;
    cout << "联系方式(邮箱/手机号):" << setw(6) <<contact << endl;
    cout << "所在城市:" << setw(6) << city << endl;
    cout << "预定参加人数:" << setw(6) << n << endl;
}
void Info::get_info()
{
    cin >> nickmane>> contact>> city>> n;
}

 实验任务6

#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();
}
#pragma once
#include<iostream>
#include <string>
using namespace std;
class TextCoder
{
public:
    TextCoder(string s):text{s}{}
    string get_ciphertext() { encoder(); return text; }
    string get_deciphertext() { decoder(); return text; }
private:
    void encoder();
    void decoder();
    string text;
};
void TextCoder::encoder()
{
    for (auto i = 0; i < text.size(); i++)
    {
        if ('a' <= text[i] && text[i] <= 'z' || 'A' <= text[i] && text[i] <= 'Z')
        {
            if ('v' <= text[i] && text[i] <= 'z' || 'V' <= text[i] && text[i] <= 'Z')
                text[i] = text[i] - 21;
            else
                text[i] = text[i] + 5;
        }
    }
}
void TextCoder::decoder()
{
    for (auto i = 0; i < text.size(); i++)
    {
        if ('a' <= text[i] && text[i] <= 'z' || 'A' <= text[i] && text[i] <= 'Z')
        {
            if ('a' <= text[i] && text[i] <= 'e' || 'A' <= text[i] && text[i] <= 'E')
                text[i] = text[i] + 21;
            else
                text[i] = text[i] - 5;
        }
    }
}

实验总结:

1.对C++标准库里的“string”类和“vector”类有了更深刻的认识,了解了二者的区别及其各自的优劣,可以较熟练的应用这两类的基本用法包括查找、插入、和删除以及简单的换序操作。

2.掌握了得到从键盘上输入的带空格的字符串的方法即“getline”函数,区别于“cin”。

3.掌握了三种访问容器类成员的方法即通过使用容器类自带的“at”函数的下标索引法,使用迭代器,通过“auto for”范围访问。

posted @ 2022-10-25 21:51  z2y  阅读(20)  评论(0)    收藏  举报