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

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

时间:2022-10-25 22:02:24浏览次数:36  
标签:string int text void cout c++ 数组 include 指针

实验任务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”范围访问。

标签:string,int,text,void,cout,c++,数组,include,指针
From: https://www.cnblogs.com/zlaym/p/16813123.html

相关文章

  • vue2- style 和class - 条件渲染 - 列表渲染 - v-for 循环,数组,循环字符串,数字,对象 -
    style和class条件渲染列表渲染v-for循环,数组,循环字符串,数字,对象数组的检测与更新双向数据绑定事件处理过滤案例事件修饰符按键修饰符1.style和class<!DOCT......
  • C++ 面向对象高级开发 基础篇(二)
    操作符重载C2就是this传递者不用知道是否returnbyreference 非成员函数(全局函数)的操作符重载(有几种用法就写几种重载)不能使用returnbyreference因为他们得......
  • C++ STL库_vector
    1.vector的初始化方式vectora(10);定义10个整形元素的向量(每个元素的初值为0)vectora(10,1);定义10个整形元素的向量(每个元素的初值为1)vectora(b);用b向量创建a向量,整体......
  • C++服务器开发精髓 电子书 pdf
    作者:张远龙出版社:电子工业出版社 链接:C++服务器开发精髓  本书从操作系统原理角度讲解进行C++服务器开发所需掌握的技术栈。全书总计9章,第1~2章讲解C++11/14/......
  • AcWing107 超快速排序(树状数组找逆序对)
    原题链接思路求到底要和相邻元素交换几次,其实就是求逆序对的数量,有几对逆序对就要交换几次,因为只能相邻的之间交换(超快速排序?冒泡排序!)利用树状数组求逆序对大概想法......
  • C++ 面向对象高级开发 基础篇(一)
    C与C++的结构 C++举例   基本结构:   C与C++的输出    防御式声明   头文件声明   Class的声明 模板   访问......
  • 二维数组--JAVA
    一.输出二维数组publicclassvar{publicstaticvoidmain(String[]args){int[][]arr={{1,0,0},{0,1,0},{0,0,1}};for(inti=0;i......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:删除有序数组中的重复项
    题目:给你一个升序排列的数组nums,请你原地删除重复出现的元素,使每个元素只出现一次,返回删除后数组的新长度。元素的相对顺序应该保持一致。由于在某些语言中不能......
  • 二维数组
    二位数组的创建和初始化,二维数组的使用,二维数组在内存中的存储intarr[3][4]; //表示3行4列,每个元素都是整形#include<stdio.h>intmain(){//intarr[3][4];/......
  • JS数组针对某键的值进行升序和降序
    dictArraySort(dictArray,sortKey,sortType="ascending",isTime=false){if(!isTime){if(sortType=='ascending'){dictArray......