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

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

时间:2022-10-23 12:01:38浏览次数:44  
标签:info string int text C++ 数组 include TextCoder 指针

5.实验任务5

info.hpp

#pragma once

#include <iostream>
#include <string>
#include <iomanip>
using std::string;
using std::cout;
using std::endl;

class info
{
private:
    string nickname;
    string contact;
    string city;
    int n;

public:
    info(string Ni = "", string Co = "", string Ci = "", int N = 0);
    ~info();
    void print();
};

info::info(string Ni, string Co, string Ci, int N) : nickname(Ni), contact(Co), city(Ci), n(N) {}

info::~info() {}

void info::print()
{
    cout << std::setw(20) << std::left << "昵称: " << nickname << endl;
    cout << std::setw(20) << std::left << "联系方式: " << contact << endl;
    cout << std::setw(20) << std::left << "所在城市: " << city << endl;
    cout << std::setw(20) << std::left << "预订人数: " << n << endl;
}

task5.cpp

#include "info.hpp"
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    const int capacity = 100;
    vector<info> audience_info_list;
    string nickname, contact, city;
    int n, sum = 0;
    
    cout << "录入信息: " << endl << endl;
    cout << "昵称\t" << "联系方式(邮箱/手机号)\t" << "所在城市\t" << "预定参加人数\t" << endl;
    while (cin >> nickname >> contact >> city >> n)
    {
        sum += n;
        if (sum <= capacity)
        {
            audience_info_list.push_back(info(nickname, contact, city, n));
        }
        else
        {
            sum -= n;
            cout << "对不起, 只剩" << capacity - sum << "个位置。" << endl;
            cout << "1. 输入u, 更新(update)预定信息" << endl;
            cout << "2. 输入q, 退出预定" << endl;
            cout << "你的选择: ";
            string S;
            cin >> S;
            if (S == "q")
            {
                break;
            }else if (S == "u")
            {
                continue;
            }
        }
    }
    cout << endl;
    cout << "截止目前,一共有" << sum << "位听众预定参加。" << "预定听众信息如下: " << endl;

    for (int i = 0; i < audience_info_list.size(); i++)
    {
        audience_info_list.at(i).print();
        cout << endl;
    }
}

测试1:

测试2:

6.实验任务6

TextCoder.hpp

#pragma once

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

#include <algorithm>

class TextCoder
{
private:
    string text;

public:
    TextCoder(string T);
    ~TextCoder();

    string get_ciphertext();
    string get_deciphertext();

private:
    void encoder();
    void decoder();
};

TextCoder::TextCoder(string T) : text(T) {}

TextCoder::~TextCoder() {}

void TextCoder::encoder()
{
    int n = text.length();
    for (int i = 0; i < n; i++)
    {
        if (text[i] >= 'a' && text[i] <= 'z')
        {
            if (text[i] + 5 <= 'z')
            {
                text[i] = text[i] + 5;
            }else {
                text[i] = text[i] - 21;
            }
            
        }else if (text[i] >= 'A' && text[i] <='Z')
        {
            if (text[i] + 5 <= 'Z')
            {
                text[i] = text[i] + 5;
            }else {
                text[i] = text[i] - 21;
            }      
        }
    } 
}

void TextCoder::decoder()
{
    int n = text.length();
    for (int i = 0; i < n; i++)
    {
        if (text[i] >= 'a' && text[i] <= 'z')
        {
            if (text[i] - 5 >= 'a')
            {
                text[i] = text[i] - 5;
            }else {
                text[i] = text[i] + 21;
            }
        }else if (text[i] >= 'A' && text[i] <='Z')
        {
            if (text[i] - 5 >= 'A')
            {
                text[i] = text[i] - 5;
            }else {
                text[i] = text[i] + 21;
            }
        }
    }
}

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

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

测试:

 

标签:info,string,int,text,C++,数组,include,TextCoder,指针
From: https://www.cnblogs.com/Sr-duel/p/16818274.html

相关文章

  • 数据结构 玩转数据结构 3-5 数组队列
    0课程地址https://coding.imooc.com/lesson/207.html#mid=13422 1重点关注1.1队列的特性FIFO,先进先出,水管 1.2队列的实现参考......
  • C++基础
    C++基础VS快捷键Ctrl+或-跳转到上次鼠标焦点位置CtrlKF按住Ctrl然后按K然后按FCtrlJ代码提示变量声明方式:数据类型变量名=变量初始值#include<iostre......
  • C/C++ 中的几种调试技巧(待续)
    1、利用#define宏定义作为调试开关include<stdio.h>defineDEBUG//可以注释或打开输出intmain(void){inti,sum;for(i=1,sum=0;i<=5;i++){......
  • C++ 函数返回指针
    C++中返回值为指针或者引用的时候,不可以返回局部变量的指针或者引用,因为当此段代码块执行完之后,相应的局部变量,就会被系统释放,指针所指向的那块内存会被操作系统用来做其......
  • 树状数组3区间查询区间修改
    Description要求使用树状数组完成区间之和查询,区间加上某一相同数值的操作。Solution树状数组是用来单点加,查前缀和的。若要实现区间加,可以将原数列差分,然后在l位置处+v......
  • 实验三 数组、指针与现代c++标准库
    task5#pragmaonce#include<iostream>#include<string>#include<vector>#include<iomanip>usingnamespacestd;classInfo{public:Info(string......
  • PHP array_multisort 多维数组排序的理解
    array_multisort(array1,sortingorder,sortingtype,array2,array3...) 1.数组从前往后,依次排序;前一组数中值相同时,才考虑后一个数组中的值排序;2.任一数组排序变......
  • 数组与结构体
    前言先考虑这样的问题:当你被要求定义十个变量时,你会怎么办?可以像a1,a2,a3···这样一个一个的定义出来。但是,当你被要求定义一千个,一万个变量的时候呢?肯定就不能一个一......
  • 数组,指针与现代c++标准
    #include<iostream>#include<algorithm>#include<math.h>#include<string>usingnamespacestd;classInfo{public:Info(stringnickname,stringcon......
  • 后台管理系统 数组去重 避免踩坑!
    不要把数组push放进循环中,后果很严重!!!findIndexfiltersome......