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

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

时间:2022-10-26 13:23:02浏览次数:54  
标签:info && string int text c++ 数组 include 指针

实验任务5

info.hpp

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

using namespace std;

class info {
public:
    info(string name, string contact, string city, int n);
    ~info() = default;

    void print();
    int get_total();

private:
    string nickname;
    string contact;
    string city;
    int n;
    static int total;
};

int info::total = 0;
info::info(string name, string contact, string city, int n) :nickname{ name }, contact{ contact }, city{ city }, n{ n }{
    total += n;
}

int info::get_total() {
    return total;
}

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

task5.cpp

#include"info.hpp"
#include<vector>
#include<iomanip>

using namespace std;

static int i = 0;
const int capacity = 100;

int main() {
    vector<info>audience_info_list;
    string name, contact, city;
    int n;
    char ch;

    cout << "录入信息:" << endl<< endl;
    cout << left << "昵称\t"
        << "联系方式(邮箱/手机号)\t\t"
        << "所在城市\t"
        << "预定参加人数\t" << endl;
    while (cin >> name)
    {
        cin >> contact >> city >> n;
        if (i + n <= capacity)
        {
            info t(name, contact, city, n);
            audience_info_list.push_back(t);
            i += n;
            if (i == capacity)
                break;
        }
        else
        {
            cout << "对不起,只剩" << capacity - i << "个位置." << endl;
            cout << "1.输入u,更新(update)预定信息" << endl
                 << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            cin >> ch;
            if (ch == 'u')
                cout << "请重新录入信息:" << endl;
            else
            {
                cout << endl;
                break;
            }
        }
    }
    cout << endl;
    cout << "截至目前,一共有" << i << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto list : audience_info_list)
        list.print();
}

更换测试数据后的运行测试结果:

测试样例1

 

 测试样例2

 

 

实验任务6

 TextCoder.hpp

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

class TextCoder {
public:
    TextCoder(string t);
    string get_ciphertext();
    string get_deciphertext();

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

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

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

}
string text;
void TextCoder::encoder() {
        for (int i = 0; i < text.size(); ++i) {
                if ((text.at(i) >= 'a' && text.at(i) <= 'u') || (text.at(i) >= 'A' && text.at(i) <= 'U'))
                        text.at(i) += 5;
                else if ((text.at(i) >= 'v' && text.at(i) <= 'z') || (text.at(i) >= 'V' && text.at(i) <= 'Z'))
                        text.at(i) -= 21;
        
    }
}
void TextCoder::decoder() {
        for (int i = 0; i < text.size(); ++i) {
             if ((text.at(i) >= 'f' && text.at(i) <= 'z') || (text.at(i) >= 'F' && text.at(i) <= 'Z'))
                     text.at(i) -= 5;
             else if ((text.at(i) >= 'a' && text.at(i) <= 'e') || (text.at(i) >= 'A' && text.at(i) <= 'E'))
                        text.at(i) += 21;
    
    }
}

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,指针
From: https://www.cnblogs.com/mohaolan/p/16827573.html

相关文章

  • 考试 数组,指针变换易混淆
    #define_CRT_SECURE_NO_WARNINGS1//将数组作为参数传给函数#include<stdio.h>//数组名是什么?//数组名是数组首元素的地址//但是又两个例外://1.......
  • 指针初阶001
    1.指针是什么?2.指针和指针类型3.野指针4.指针运算5.指针和数组6.二级指针7.指针数组++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++......
  • ACWing 4480 -- 二分&&双指针&&思维
    题目描述倒垃圾思路其实就是思维题,题意很简单,找到一个居民左侧和右侧(如果存在的话)的垃圾桶中最近的那个垃圾桶,然后让那个垃圾桶的计数器加一。从题目中挖掘的性质:左......
  • LeetCode 566重塑矩阵 指针操作理解笔记
    LeetCode566重塑矩阵指针操作理解笔记题目来源:力扣题库(LeetCode)566.重塑矩阵前言:本来刚看到题目的时候,我是很开心的,这题不就是把二维数组“排扁”成一维数组,然后......
  • Demo48_还原稀疏数组
    //还原稀疏数组packagecom.HuanXin.array_6;publicclassDemo09_01{publicstaticvoidmain(String[]args){int[][]AA=newint[7][7];AA[1][1]=......
  • 力扣561(java&python)-数组拆分(简单)
    题目:给定长度为 2n 的整数数组nums,你的任务是将这些数分成 n对,例如(a1,b1),(a2,b2),...,(an,bn),使得从1到 n的min(ai,bi)总和最大。返回该最大......
  • [C++]linux下实现ls()函数遍历目录
    intls(std::stringpath,std::string&ret){DIR*dirp=opendir(path.c_str());if(!dirp){return-1;}structstatst;structdirent*di......
  • 数组扁平化 超简单
    letarr=[1,2,[99,100],3,4,[1,2,{name:'hkq'},[[1,2,3],2]],5,6,7,8]letc= arr.flat(Infinity)  //Infinity表示展开任意胜读的......
  • 2 方法、数组、IDEA使用、面向对象
    五、方法1.方法定义和调用2.方法重载OverLoad六、数组1.数组的三种初始化方式:静态、动态、默认2.Arrays工具类3.二维数组七、IDEA介绍1.IDEA常用设置2.IDEA常用快......
  • C++中#和##
    转自:https://blog.csdn.net/YhL_Leo/article/details/488790931.介绍 C/C++的宏中, #的功能是将其后面的宏参数进行字符串化操作;## 功能是在带参数的宏定义中将两......