C/C++教程

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

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

实验任务5

"info.hpp"

#ifndef INFO_HPP
#define INFO_HPP

#include <bits/stdc++.h>
using namespace std;

class info
{
    private:
        string name;
        string contact;
        string city;
        int n;
    public:
        info(){}
        info(string a = "", string b = "", string c = "", int d = 0): name(a), contact(b),city(c),n(d){}
        void print();
};

void info::print()
{
    cout << "称呼:      "<< name << endl;
    cout << "联系方式:  "<< contact << endl;
    cout << "所在城市:  "<< city << endl;
    cout << "预定人数:  "<< n <<endl;
}

#endif

 

"task5.cpp"

#include "info.hpp"
#include <bits/stdc++.h>
using namespace std;
const int N = 100;

void update(vector<info> &a, int &count,int t)
{
    a.pop_back(); count -= t;
    string t1,t2,t3;
    int t4;
    char chose;
    cout << "对不起,只剩下" << N - count << "个座位."<< endl;
    cout << "1. 输入u,更新预定信息" << endl;
    cout << "2. 输入q,退出预定" << endl;
    cin >> chose;
    cout << "你的选择: " << chose <<endl;
    if(chose == 'q')
    {
        return ;
    }
    else if( chose == 'u')
    {
        cout << "姓名/昵称  联系方式(邮箱/手机号)  所在城市  预定参加人数" << endl;;
        cin >> t1 >> t2 >> t3 >> t4;
        info temp(t1,t2,t3,t4);
        count += t4;
        a.push_back(temp);
        if(count > N)
        {
            update(a, count, t4);
            return ;
        }
        else
        {
            return ;
        }
    }
    else
    {
        cout << "ERROR!" << endl;
        return ;
    }
}
int main()
{
    vector<info>  audience_info_list;
    string t1,t2,t3;  //name contact place
    int t4=0; //number;
    int count=0;
    cout << "姓名/昵称  联系方式(邮箱/手机号)  所在城市  预定参加人数" << endl;
    while(cin>>t1>>t2>>t3>>t4)
    {
        count += t4;
        info temp(t1,t2,t3,t4);
        audience_info_list.push_back(temp);
        if(count == N) break;
        else if(count > N)
        {
            update(audience_info_list, count, t4);
            break;
        }
    }
    cout << endl;
    cout << "截至目前,一共有" << count << "位听众预定参加,预定观众信息如下:" << endl; 
    for(auto i : audience_info_list)
    {
        i.print();
    }
    return 0;
}

"运行截图"

<< 人数不满 >>

 

<< 人数满,更新信息 >>

 

<< 人数满,直接退出 >>

 

实验任务6

"textcoder.hpp"

 

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP

#include <bits/stdc++.h>
using namespace std;

class TextCoder 
{
    private:
        string text;
    public:
        TextCoder(string a = ""): text(a){}
        string encoder();  //A 65-90 a 97-122
        string decoder();
};
string TextCoder::encoder()
{
    string res;
    for(auto i:text)
        if(i >= 'a' && i <= 'z')
          res.push_back((i - 'a' +5)%26 + 'a');
        else if(i >= 'A' && i <= 'Z')
          res.push_back((i - 'A' +5)%26 + 'A');
        else 
          res.push_back(i);
    return res;

}
string TextCoder::decoder()
{
    string res;
    for(auto i : text)
       if(i >= 'a' && i <= 'z')
          res.push_back('z' - ('z' -i +5)%26);
        else if(i >= 'A' && i <= 'Z')
          res.push_back('Z' - ('Z'-i +5)%26);
        else
          res.push_back(i);
    return res;
}

#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++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!