实验二 :数组、指针与c++标准库

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了实验二 :数组、指针与c++标准库脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

<task5.cpp>

#include"info.hpp"
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
const int capacITy = 100;
int main()
{
    cout << "录入信息:" << endl
        << "称呼, 联系方式, 所在城市 ,参加人数" << endl;
    vector<info>audinece_info_list;
    string a, b, c;
    int d, t = 1, num = 0;//t用来判断更新,还是退出
    while (t && num<100 &amp;& cin >> a >> b >> c >> d)
    {
        if (num + d > capacity)
        {
            cout << "对不起,只剩" << capacity - num << "个位置。" << endl
                << "1.输入u,更新预定信息" << endl
                << "2.输入q,退出预定" << endl
                << "你的选择:";
            getchar();//吞回车
            while (char c = getchar())
            {
                getchar();//吞回车
                if (c == 'u')
                {
                    t = 1;
                    cout << "称呼, 联系方式, 所在城市 ,参加人数" << endl;
                    break;
                }
                else if (c == 'q')
                {
                    t = 0;
                    break;
                }
                else 
                {
                    cout << "输入错误,重新选择:";
                    continue;
                }
            }
        }
        else
        {
            num += d;
            info l(a, b, c, d);
            audinece_info_list.push_back(l);
        }
    }
    cout << "目前一共" << num << "位听众预定,信息如下:" << endl;
    for (auto it = audinece_info_list.begin(); it != audinece_info_list.end(); ++it)
        it->PRint();
}

“info.hpp”

#ifndef INFO_HPP
#define INFO_HPP
#include<iostream>
#include<string>
using namespace std;
class info {
private:
    string nickname, contact, city;
    int n;
public:
    info(string a,string b,string c,int d):nickname(a),contact(b),city(c),n(d){}
    ~info(){}
    void print()
    {
        cout << "称呼:" << nickname << endl 
            << "联系方式:" << contact << endl 
            << "所在城市:" << city << endl
            << "预定人数:" << n << endl;
    }
};

#endif 

测试结果1:

实验二 :数组、指针与c++标准库

 

 结果2:

实验二 :数组、指针与c++标准库

 

 

<task6.cpp>

#include "textcoder.hpp"
#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string text, encoded_text, decoded_text;
    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象
        cout << "加密后英文文本:t" << encoded_text << endl;
        decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无
            cout << "解密后英文文本:t" << decoded_text << endl;
        cout << "n输入英文文本: ";
    }
}

"textcoder.hpp"

#include<iostream>
#include<string>
using namespace std;
class TextCoder {
private:
    string text;
public:
    TextCoder(string a):text(a){}
    ~TextCoder(){}
    string encoder() 
    {
        for (int i = 0; i < text.size(); i++)
            if (text[i] >= 'a' && text[i] <= 'z')
                text[i] = (text[i] - 'a' + 5) % 26 + 'a';
            else if (text[i] >= 'A' && text[i] <= 'Z')
                text[i] = (text[i] - 'A' + 5) % 26 + 'A';
        return text;
    }
    string decoder() 
    {
        for (int i = 0; i < text.size(); i++)
            if (text[i] >= 'a' && text[i] <= 'z')
                text[i] = (text[i] - 'a' - 5 + 26) % 26 + 'a';//+26止为负数
            else if (text[i] >= 'A' && text[i] <= 'Z')
                text[i] = (text[i] - 'A' - 5 + 26) % 26 + 'A';
        return text;
    }
};

测试结果:

实验二 :数组、指针与c++标准库

 

脚本宝典总结

以上是脚本宝典为你收集整理的实验二 :数组、指针与c++标准库全部内容,希望文章能够帮你解决实验二 :数组、指针与c++标准库所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。