实验二 数组与指针

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

info.hpp代码

#ifndef INFO_HPP
#define INFO_HPP

#include <iostream>
#include <string>

using namespace std;

class info{
    PRivate:
        string nickname;
        string contact;
        string cITy;
        int n;
    public:
        info (string nickname0,string contact0,string city0,int n0);
        void print()const;
};
info::info(string nickname0,string contact0,string city0,int n0):nickname(nickname0),contact(contact0),city(city0),n(n0){
}
void info::print()const
{
    cout<<"称呼:"<<nickname<<endl;
    cout<<"联系方式:"<<contact<<endl;
    cout<<"所在城市:"<<city<<endl;
    cout<<"预定人数:"<<n<<endl;
}

#endif

task5.cpp

#include"info.hpp"
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main()
{
    vector<info> audience_info_list;
    const int capacity=100;
    string a,b,c,choose;
    int d,s;
    cout<<"称呼/昵称,联系方式(邮箱/手机号码),所在城市,预定参加人数"<<endl; 
    while(cin>>a>>b>>c>>d)
    {
        audience_info_list.push_back(info(a,b,c,d));
        s+=d;
        if(s>capacity)
        {
            s-=d;
            audience_info_list.pop_back();
            cout<<"对不起,只剩"<<capacity-s<<"个位置." <<endl; 
            cout<<"1.输入u,更新预定信息"<<endl;
            cout<<"2.输入q,退出预定"<<endl;
            cout<<"你的选择:";
            cin>>choose;
            if(choose=="u") continue;
            if(choose=="q") break;
        }
    }
    cout<<"截至目前,一共有"<<s<<"位听众预定参加。预定观众信息如下:" <<endl;
    for(auto &amp;i:audience_info_list)
    {
        i.print();
     } 
}

运行截图

实验二 数组与指针

 

 

实验二 数组与指针

 

 textcoder.hpp源代码

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP

#include <iostream>
#include <string> 

using namespace std;
class TextCoder{
    private:
        string text;
    public:
        string encoder();
        string decoder();
        TextCoder(string text0):text(text0){};
};
string TextCoder::encoder()
{
    for(int i=0;i<text.length();i++)
    {
        if(text[i]>='a'&&text[i]<='u'||text[i] >= 'A'&&text[i] <= 'U')
        {
            text[i]+=5;
        }
        else if(text[i]>='v'&&text[i]<='z'||text[i] >='V'&&text[i] <= 'Z')
        text[i]=text[i]-26+5;
    }
    return text; 
}
string TextCoder::decoder()
{
    for(int i=0;i<text.length();i++)
    {
        if(text[i]>='f'&&text[i]<='z'||text[i] >= 'F'&&text[i] <= 'Z')
        text[i]-=5;
        else if(text[i]>='a'&&text[i]<='e'||text[i] >= 'A'&&text[i] <= 'E')
        text[i]=text[i]+26-5;
    }
    return text;
}
#endif

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输入英文文本: ";
    }
}

运行截图

实验二 数组与指针

 

 实验小结

1,for循环用(auto &i:v)非常方便

2,''单引号中的字符相当于一个数字

3,对string直接初始化用双引号

 

脚本宝典总结

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

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

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