C/C++教程

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

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

Info.hpp

#ifndef INFO_HPP
#define INFO_HPP
#include <iostream>
#include <iomanip> 
#include <string>
using namespace std;
class Info
{
    public:
        Info(string a, string b, string c, int d) : nickname {a}, contact {b}, city {c}, n {d} {}
        ~Info() {}
        void print()
        {
            cout.setf(ios::left);
            cout << setw(8) << "称呼:    " << setw(15) << nickname << endl;
            cout << setw(8) << "联系方式:" << setw(15) << contact << endl;
            cout << setw(8) << "所在城市:" << setw(15) << city << endl;
            cout << setw(8) << "预订人数:" << setw(15) << n << endl;
        }
    private:
        string nickname;
        string contact;
        string city;
        int n;    
};
#endif

 

Info.cpp

#include"Info.hpp"
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
int main()
{
    using namespace std;
    const int capacity = 100;
    string na;
    string co;
    string ci;
    int nu;
    int n=0;
    vector<Info> audience_info_list;
    cout << "录入信息:" <<endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; 
    while(cin >> na)
    {
        cin >> co;
        cin >> ci;
        cin >> nu;
        if(n + nu > capacity)
        {
            cout << "对不起,只剩" << capacity - n << "个位置" << endl;
            cout << "1.输入u,更新预定信息" << endl;
            cout << "2.输入q,退出预定" << endl; 
            char s;
            cin >> s;
            if(s=='u')
            {
                continue;
            }
            else
            {
                break;
            }
        }
        n+=nu;
        audience_info_list.push_back(Info(na, co, ci, nu));
    }
    cout << "截至目前,一共有" << n << "位听众预定参加。预定听众信息如下:" <<endl;
    for(auto it=audience_info_list.begin();it!=audience_info_list.end();++it)
    {
        it -> print();
    } 
}

 

运行结果:

 

 

TextCoder.hpp

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP
#include <iostream>
#include <string>
using namespace std;
class TextCoder
{
    public:
        TextCoder(string oldtext) : text {oldtext} {}
        string encoder()
        {
            int i;
            for(i = 0 ; i < text.length() ; i++)
            {
                if(( text[i] >= 'a' && text[i] <= 'z' ) || ( text[i] >= 'A' && text[i] <= 'Z' ))
                {
                    if(( text[i] >= 'v' && text[i] <= 'z' ) || ( text[i] >= 'V' && text[i] <= 'Z' ))
                    {
                        text[i] -= 21;
                    }
                    else
                    {
                        text[i] += 5;
                    }
                }
            }
            return text;
        }
        string decoder()
        {
            int i;
            for(i = 0 ; i < text.length() ; i++)
            {
                if(( text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
                {
                    if((text[i]>='a' && text[i] <= 'e') || (text[i] >='A' && text[i] <= 'E'))
                    {
                        text[i] += 21;
                    }
                    else
                    {
                        text[i] -= 5;
                    }
                }
            }
            return text;
        }     
        private:
            string text;
};
#endif

 

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

 

运行结果:

 

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