C/C++教程

实验2 数组、指针与C++标准库

本文主要是介绍实验2 数组、指针与C++标准库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实验任务5

info.hpp

#ifndef INFO_HPP
#define INFO_HPP
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class info{
public:
    info(string a,string b,string c,int d);
    void print();
private:
    string nickname;
    string contact;
    string city;
    int n;
};
info::info(string a,string b,string c,int d):nickname(a),contact(b),city(c),n(d) {}
void info::print()
{
    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 name,contact,city;
    int n;
    static int count=0;
    char choice;
    cout<<"录入信息:"<<endl
        <<endl;
    cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl;
    while(cin>>name)
    {
        cin>>contact>>city>>n;
        count+=n;
        if(count>capacity)
        {
            count-=n;
            cout<<"对不起,只剩"<<capacity-count<<"个位置。"<<endl;
            cout<<"1.输入u,更新(update)预定信息"<<endl;
            cout<<"2.输入q,退出预定"<<endl;
            cout<<"你的选择:";
            cin>>choice;
            if(choice=='u')
            continue;
            if(choice=='q')
            {
                cout<<endl;
                break;
             } 
        }
        else
        {
            info a(name,contact,city,n);
            audience_info_list.push_back(a);
        }
    }
    cout<<"截至目前,一共有"<<count<<"位听众预定参加。预定信息如下:"<<endl;
    for(auto it=audience_info_list.begin();it!=audience_info_list.end();it++)
        it->print();
    return 0;
}

 

 

实验任务6

textcoder.hpp

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP
#include<iostream>
#include<string>
using namespace std;
class textcoder
{
public:
    textcoder(string t):text(t) {}
    string encoder(){
        int i;
    for(i=0;i<text.length();i++)
    {
        if((text[i]>='a'&&text[i]<='u')||(text[i]>='A'&&text[i]<='U'))
        text[i]=text[i]+5;
        else if((text[i]>='v'&&text[i]<='z')||(text[i]>='V'&&text[i]<='Z'))
        text[i]=text[i]-21;
    }
    return text;
    }
    string decoder(){
        
    int i;
    for(i=0;i<text.length();i++)
    {
        if((text[i]>='f'&&text[i]<='z')||(text[i]>='F'&&text[i]<='Z'))
        text[i]=text[i]-5;
        else if((text[i]>='a'&&text[i]<='e')||(text[i]>='A'&&text[i]<='E'))
        text[i]=text[i]+21;
    }
    return text;
    }
private:
    string 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输入英文文本: ";
    }
}

 

 

这篇关于实验2 数组、指针与C++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!