一个简单封装用户命令的C++程序

在写测试程序的时候,重复编写重控制台读取命令的函数是令人乏味的,那么我们是否可以通过C++的封装性来完成一些简单的功能,以便日后的子类再次利用呢?这片文章就是在种需求的促使下,给出了一个简单的解决方案,希望网友对此有所启发
/**
 * @file Seminar1.cpp
 * @author Wang yanqing
 */

#include <iostream>
#include <memory>

using namespace std;

#define    SHOW_ME( name )                    cout << #name"'s size = " << sizeof( name ) << endl
#define SHOW_PROMPT()                    cout << "$ "
#define SHOW_INV_CMD( executor, cmd )    cout << #executor" Executor: " << char(cmd) << ": command not found" << endl
#define    HELP_ME( cmd, desc )            cout << #cmd"/t/t---/t"desc << endl
#define SHOW_OFFSET( type, member )        cout << #member"'s offset is " << /
        (unsigned long) (&((type *) 0)->member) << " in the struct "#type << endl
#define SHOW_LINE()                    do {/
        for ( int i = 0; i < 80; ++ i )    /
            cout << "-";                /
        cout << endl;                    /
    } while ( 0 )

#define SHOW_STRUCT( name )            do {/
        SHOW_LINE();                    /
        SHOW_OFFSET( name, c );            /
        SHOW_OFFSET( name, s );            /
        SHOW_OFFSET( name, i );            /
        SHOW_LINE();                    /
    } while ( 0 )

class Executor
{
    bool running;

protected:
    virtual bool OnCommand( int cmd )
    {
        bool consumed = true;

        switch ( cmd )
        {
        case 'q':
        case 'Q':
            running  = false;
            break;

        case 'h':
        case 'H':
            help();
            break;

        default:
            consumed = false;
            break;
        }

        return consumed;
    }

    virtual void DoRun() = 0;

    virtual void help() const
    {
        HELP_ME( q, "Quit" );
        HELP_ME( h, "Help" );
    }

public:
    Executor(): running( true )
    {
    }

    virtual ~Executor()
    {
    }

    void Run()
    {
        help();
        SHOW_PROMPT();
        while ( running )
        {
            DoRun();
        }
    }
};

class WYQ: public Executor
{
    struct WasteMemory
    {
        char  c;
        int   i;
        short s;
    };

    struct RationalMemory
    {
        char  c;
        short s;
        int   i;
    };

    struct CarfullMemory
    {
        char  c;
        char  unused;
        short s;
        int   i;
    };

    int GetCommand()
    {
        char cmd;

        for ( ; ; )
        {
            //cin >> cmd;
            cmd = getchar();
            if ( cmd != '/n' )
                break;
            SHOW_PROMPT();
        }
       

        return cmd;
    }
   
    void ShowSize( void )
    {
        SHOW_ME( WasteMemory );
        SHOW_ME( RationalMemory );
        SHOW_ME( CarfullMemory );
    }

    void ShowStruct( void )
    {
        SHOW_STRUCT( WasteMemory );
        SHOW_STRUCT( RationalMemory );
        SHOW_STRUCT( CarfullMemory );
    }

protected:
    virtual bool OnCommand( int cmd )
    {
        bool consumed = true;
       
        switch ( cmd )
        {
        case 'a':
        case 'A':
            ShowSize();
            break;

        case 'b':
        case 'B':
            ShowStruct();
            break;
       
        default:
            consumed = Executor::OnCommand( cmd );
            break;
        }

        return consumed;
    }
   
    virtual void DoRun()
    {
        int cmd = GetCommand();
        if ( !OnCommand( cmd ) )
        {
            SHOW_INV_CMD( WYQ, cmd );
            help();
        }
    }

    virtual void help() const
    {
        HELP_ME( a, "Show structures' size" );
        HELP_ME( b, "Show structures' distribution" );
        Executor::help();
    }

public:
    virtual ~WYQ()
    {
        cout << "WYQ: byebye!" << endl;
    }

};

int main( void )
{
    auto_ptr<Executor> wyq( new WYQ );

    wyq->Run();

    return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值