第八节 模块化思想

vim中的拷贝和粘贴:v 可视化选择;y拷贝,p粘贴

先按一个v,然后按上下左右键的右键,按y 就拷贝完成

到合适的位置粘贴

课程第八节

要求写一个可以实现命令行的小程序

自己实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void dothis(char * cmd)
{
   printf("this is %s",cmd);
   if(strcmp(cmd,"help")==0)
   {
        printf("haha");
   }
   else if(strcmp(cmd,"help")==0)
   {
        printf("sisi");
   }
    
}
int main()
{
    char cmd[1024];
    while(1)
    {
    scanf("%s",cmd);
    if(strcasecmp(cmd,"null")!=0)
    {
        dothis(cmd);  
    }
    }
    return 0;
}
课上老师实现:

 


/********************************************************************/
/*Copyright (C) mc2lab.com,SSE@USTC,2014-2015                       */
/*                                                                  */
/*File NAME           :menu.c                                       */
/*PRINCIPAL AUTHOR    :Mengning                                     */
/*SUBSYSTEM NAME      :menu                                         */
/*MODULE NAME         :menu                                         */
/*LANGUAGE            :C                                            */
/*TARGET ENVIRONMENT  :ANY                                          */
/*DATE OF FIRST RELEASE :2014/08/31                                 */
/*DESCRIPTION         :This is a menu program                       */
/********************************************************************/

/*
 * Revision log:
 *
 * Created by Mengning,2014/08/31
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Help();

#define CMD_MAX_LEN   128
#define DESC_LEN      1024
#define CMD_NUM       10

typedef struct DataNode
{
    char *cmd;
    char *desc;
    int (*handler)();
    struct DataNode *next;
}tDataNode;

/*此处实现了多态,即运行时产生不一样的实现,包容变化,一个命令行程序,经常变化的内容,模块化思想

加入再加入一条命令,直接在tDataNode 中加入,变为

static tDataNode head[]=
{
    {"help","this is help cmd!",Help,&head[1]},
    {"version","menu program v1.0",NULL,&head[2]}

    {"quit","this is help quit!",Quit,NULL},
};

再加入Quit()函数

*/

static tDataNode head[]=
{
    {"help","this is help cmd!",Help,&head[1]},
    {"version","menu program v1.0",NULL,NULL}
};


int main()
{
    /*cmd line begins */
    while(1)
    {
        char cmd[CMD_MAX_LEN];
        printf("Input a cmd number>");
        scanf("%s",cmd);
        tDataNode *p=head;
//        printf("this is head data :%s",head->cmd);
        while(p!=NULL)
        {
            if(0==strcmp(p->cmd,cmd))
            {
                printf("%s-%s\n",p->cmd,p->desc);
                if(p->handler!=NULL)
                {
                    p->handler();
                }
                p=p->next;
            }
            if(p==NULL)
            {
                printf("This is a wrong cmd!\n");
            }
        }
    }
    return 0;
}

int Help()
{
    printf("Menu List:\n");
    tDataNode *p=head;
    while(p!=NULL)
    {
        printf("%s-%s\n",p->cmd,p->desc);
        p=p->next;
    }

    return 0;
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值