实验标题:实现内部模块化的命令行菜单小程序
实验内容:源程序转写为go程序
实验结果:menu.c代码
/**************************************************************************************************/ /* 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 * Edited by Zhouyan, 2017/5/7 */ package main import "fmt" func Help() func Quit() const ( CMD_MAX_LEN = 128 DESC_LEN = 1024 DESC_LEN = 1024 ) var cmd [CMD_MAX_LEN] char /* data struct and its operations */ type DataNode struct { tLinkTableNode *pNext char* cmd char* desc int (*handler)() } func SearchCondition bool(tLinkTableNode * pLinkTableNode) { var * pNode DataNode pNode = tDataNode *(pLinkTableNode) if(strcmp(pNode->cmd, cmd) == 0) { return 1 } return 0 } /* find a cmd in the linklist and return the datanode pointer */ func ShowAllCmd int(tLinkTable * head) { var * pNode DataNode pNode = tDataNode*(GetLinkTableHead(head)) for pNode != NULL { fmt.Printf("%s - %s\n", pNode.cmd, pNode.desc) pNode = tDataNode*(GetNextLinkTableNode(head,tLinkTableNode *(pNode))) } return 0 } func InitMenuData int(tLinkTable ** ppLinktable) { *ppLinktable = CreateLinkTable() var *pNode DataNode pNode = tDataNode*(malloc(sizeof(tDataNode))) pNode.cmd = "help" pNode.desc = "Menu List:" pNode.handler = Help AddLinkTableNode(*ppLinktable,tLinkTableNode *(pNode)) pNode = tDataNode*(malloc(sizeof(tDataNode))) pNode.cmd = "version" pNode.desc = "Menu Program V1.0" pNode.handler = NULL AddLinkTableNode(*ppLinktable,tLinkTableNode *(pNode)) pNode = tDataNode*(malloc(sizeof(tDataNode))) pNode.cmd = "quit" pNode.desc = "Quit from Menu Program V1.0" pNode.handler = Quit AddLinkTableNode(*ppLinktable,tLinkTableNode *(pNode)) return 0 } /* menu program */ tLinkTable * head = NULL func main() int { InitMenuData(&head) /* cmd line begins */ for true { fmt.Printf("Input a cmd number > ") fmt.Scanf("%s", cmd) var *p DataNode p = FindCmd(head, cmd) if p==NULL { fmt.Printf("This is a wrong cmd!\n ") continue } fmt.Printf("%s - %s\n", p->cmd, p->desc) if p.handler != NULL { p.handler() } } } func Help() int { ShowAllCmd(head) return 0 } func Quit() bool { exit(0) }实验心得:
转写要点:1、函数定义,类型后置
2、I/O函数
3、类型转换,括号位置变更
4、不使用分号
5、部分结构(for)等删去括号
