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

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

时间:2022-10-21 21:14:11浏览次数:42  
标签:&& Info string text void C++ 数组 include 指针

一.实验结论:

1.实验任务5:

Info.hpp:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Info
{
public:
    Info() {}
    Info(string x, string y, string z, int num) : nickname{x}, contact{y}, city{z}, n{num} {}
    ~Info() {}
    void set_nickname(string m)
    {
        nickname = m;
    }
    void set_contact(string r)
    {
        contact = r;
    }
    void set_city(string p)
    {
        city = p;
    }
    void set_n(int e)
    {
        n = e;
    }
    void print()
    {
        cout << left << setw(8) << "昵称:" << nickname << endl;
        cout << left << setw(8) << "联系方式:" << contact << endl;
        cout << left << setw(8) << "所在城市:" << city << endl;
        cout << left << setw(8) << "预定人数:" << n << endl;
    }

private:
    string nickname, contact, city;
    int n;
};

 

task5.cpp:

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

int main()
{
    int i, d, sum = 0, number, t = 1;
    string a, b, c, my_choice;
    const int capacity = 100;
    vector<Info> audience_info_list(100);
    cout << left << setw(20) << "昵称" << left << setw(50) << "联系方式(邮箱/手机号)" << left << setw(20) << "所在城市" << left << setw(20) << "预定参加人数" << endl;
    i = 0;
    while (cin >> a >> b >> c >> d)
    {
        if (d <= capacity - sum)
        {
            audience_info_list[i].set_nickname(a);
            audience_info_list[i].set_contact(b);
            audience_info_list[i].set_city(c);
            audience_info_list[i].set_n(d);
            sum += d;
            i++;
        }
        else
        {
            cout << "对不起,只剩" << capacity - sum << "个位置" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            while (cin >> my_choice)
            {
                if (my_choice == "q")
                {
                    i = i - 1;
                    t = 0;
                    break;
                }
                else if (my_choice == "u")
                {
                    cin >> a >> b >> c >> d;
                    if (d > capacity - sum)
                    {
                        cout << "对不起,只剩" << capacity - sum << "个位置" << endl;
                        cout << "1.输入u,更新(update)预定信息" << endl;
                        cout << "2.输入q,退出预定" << endl;
                        cout << "你的选择:";
                        continue;
                    }
                    audience_info_list[i].set_nickname(a);
                    audience_info_list[i].set_contact(b);
                    audience_info_list[i].set_city(c);
                    audience_info_list[i].set_n(d);
                    sum += d;
                    i++;
                    break;
                }
            }
        }
        if (t == 0)
            break;
    }
    number = i;
    cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
    for (i = 0; i <= number; i++)
        audience_info_list[i].print();
}

运行结果:

 

2.实验任务6:

textcoder.hpp:

#include <iostream>
#include <string>

using namespace std;
class TextCoder
{
public:
    TextCoder() {}
    TextCoder(string x) : text{x} {}
    
    string get_ciphertext()
    {
        encoder();
        return text;
    }

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

private:
    string text;
    void encoder()
    {
        for (int i = 0; i < text.length(); i++)
        {
            if (text.at(i) >= 'a' && text.at(i) <= 'z')
            {
                if (text.at(i) >= 'a' && text.at(i) <= 'u')
                    text.at(i) += 5;
                else if (text.at(i) > 'u' && text.at(i) <= 'z')
                    text.at(i) -= 21;
            }
            if (text.at(i) >= 'A' && text.at(i) <= 'Z')
            {
                if (text.at(i) >= 'A' && text.at(i) <= 'U')
                    text.at(i) += 5;
                else if (text.at(i) > 'U' && text.at(i) <= 'Z')
                    text.at(i) -= 21;
            }

 

        }
    }
    void decoder()
    {
        for (int j = 0; j < text.length(); j++)
        {
            if (text.at(j) >= 'a' && text.at(j) <= 'z')
            {
                if (text.at(j) >= 'f' && text.at(j) <= 'z')
                    text.at(j) -= 5;
                else if (text.at(j) >= 'a' && text.at(j) < 'f')
                    text.at(j) += 21;
            }

            if (text.at(j) >= 'A' && text.at(j) <= 'Z')
            {
                if (text.at(j) >= 'F' && text.at(j) <= 'Z')
                    text.at(j) -= 5;
                else if (text.at(j) > 'A' && text.at(j) < 'F')
                    text.at(j) += 21;
            }
        }
    }
};

task6.cpp:

#include "task6textcoder.hpp"
#include <iostream>

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

运行结果:

 

二.实验总结:

1.在书写Info类时,最重要的是判断多组输入以及退出的条件。一开始,当剩余容量小于预定量时,我只进行了一次判断,导致在再次更新时如果剩余容量仍然不够时便不能跳出“对不起,只剩...你的选择..”,之后,我又在循环体内嵌套一次判断,并采用continue进行退出,问题解决。

2.实验任务6,主要是注意题中所指出的是私有还是公有成员函数。

标签:&&,Info,string,text,void,C++,数组,include,指针
From: https://www.cnblogs.com/jyksblog/p/16814762.html

相关文章

  • 正则表达式(C、C++、Python、Shell)
    撰写本文档的初衷本来是想介绍正则表达式怎么写,但是百度一搜,正则表达式的教程的质量已经相当高,我便不在班门弄斧了。正则表达式是一种方法,在不同的语言中,它的应用样式可能......
  • 【C语言】数组
    ......
  • Visual Code配置C/C++
    1.前言VSCode和以前的Visualstudio开发环境不一样,只是代码编辑器,如果需要运行代码C/C++,需要额外安装编译器。在Linux环境下,一般系统自带了gcc编译器,但是windows环境没......
  • C/C++停车场模拟(栈和队列)
    C/C++停车场模拟(栈和队列)【讨论问题2】栈和队列的应用[问题描述]设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的......
  • C/C++single
    #include<iostream>#include<unistd.h>#include<csignal>#include<string.h>usingnamespacestd;voidsignal_handler(intsignal){ cout<<"Caughtsignalnum......
  • 求两个数组之和
    如题:求两个数组之和。例如:输入一个2,2(2行2列)    元素为:1,2,3,4          输入第二个2 2                       元素为:2,3,4,5......
  • 动态创建数组,一维和二维
    #include<stdio.h>#include<stdlib.h>voidOneDimensionVector(){//用来创建一维数组intn,i;int*arr;scanf("%d",&n);arr=(int......
  • Demo39_java数组05_后半段
    //数组与增强for循环packagecom.HuanXin.array_6;publicclassDemo04{publicstaticvoidmain(String[]args){int[]A={1,2,3,4,5};//增强for......
  • 实验3 数组、指针与现代c++标准库
    1.实验任务1程序源码task1_1#include<iostream>usingstd::cout;usingstd::endl;classA{public:A(intx0,inty0):x{x0},y{y0}{}voidshow......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:寻找两个正序数组的中位数
    题目:给定两个大小分别为m和n的正序(从小到大)数组 nums1和 nums2。请你找出并返回这两个正序数组的中位数。算法的时间复杂度应该为O(log(m+n))。 示例1:输入:num......