课程来源: 【网易云课堂昵称 :风清扬pty+ 《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006 】
一. 实验内容
- 观看了孟老师的视频课件,学习了相关的代码规范,如KISS原则(简单易懂),不和陌生人说话原则(一个函数、一个方法、一个代码块只做一件事),goto 的使用方法,使用合适的数据结构可以使程序更加简单高效,还有一定要有错误处理。
- 学习体会内部模块化编程的思想和方法。
二. 实验过程
linklist.h
#include<stdio.h>
#include<stdlib.h>
typedef struct DataNode
{
char* cmd;
char* desc;
int (*handler)();
struct DataNode *next;
}tDateNode;
tDateNode *findCmd(tDateNode *head,char *cmd);
int showAllCmd(tDateNode *head);
linklist.c
#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
tDateNode *findCmd(tDateNode *head, char *cmd)
{
if(head == NULL || cmd == NULL)
{
return NULL;
}
tDateNode *p = head;
while(p != NULL)
{
if( !strcmp(p->cmd, cmd))
{
return p;
}
p = p->next;
}
return NULL;
}
int showAllCmd(tDateNode *head)
{
printf("Menu List:\n");
tDateNode *p = head;
while(p != NULL)
{
printf(" %s\n",p->cmd);
p = p->next;
}
return 0;
}
menu.c
#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
#define CMD_MAX_LEN 128
#define DESC_LEN 1024
#define CMD_NUM 10
int help();
int quit();
static tDateNode head[] =
{
{"help", "This is help infomation!", help, &head[1]},
{"version", "Menu Program for lab3", help, &head[2]},
{"setting", "This is setting function", help, &head[3]},
{"quit","Quit", quit, NULL}
};
int main()
{
while(1)
{
char cmd[CMD_MAX_LEN];
printf("please input the cmd > ");
scanf("%s", cmd);
tDateNode *p = findCmd(head, cmd);
if(p == NULL)
{
printf("ERROR:cmd not found!\n");
continue;
}
printf("%s : %s \n", p->cmd,p->desc);
if(p->handler != NULL)
{
p->handler();
}
}
}
int help()
{
showAllCmd(head);
return 0;
}
int quit()
{
exit(0);
}