SA17225160_李伯威(libowei)_高级软工第三次实验报告
来源:互联网 发布:迷人的保姆 知乎 编辑:程序博客网 时间:2024/05/17 23:10
一、实验要求
实现内部模块化的命令行菜单小程序V2.0
注意代码的业务逻辑和数据存储之间的分离,即将系统抽象为两个层级:菜单业务逻辑和菜单数据存储
要求:
- 遵守代码风格规范,参考借鉴代码设计规范的一些方法;
- 代码的业务逻辑和数据存储使用不同的源文件实现,即应该有2个.c和一个.h作为接口文件。
1.clone并进入共享文件夹
2.建立指定的lab3文件夹
3.用vim编辑linklist.c文件
#include <stdio.h>#include <stdlib.h>#include "linklist.h"static tDataNode head[] ={ {"help","This is Help",help,&head[1]}, {"add","This is Add input 2 integers!",add,&head[2]}, {"sub","This is Sub input 2 integers!",sub,&head[3]}, {"multiple","This is Multiple input 2 integers!",multiple,&head[4]}, {"divide","This is Divide input 2 integers!",divide,&head[5]}, {"quit","This is Quit will quit from program!",quit,NULL}};tDataNode* FindCmd(tDataNode* head,char* cmd){ if(head==NULL||cmd==NULL) { return NULL; } tDataNode* p=head; while(p!=NULL&&strcmp(p->cmd,cmd)!=0) { p=p->next; } if(p==NULL) { return NULL; } return p;}int ShowAllCmd(tDataNode* head){ printf("Menu List:\n"); tDataNode*p=head; while(p!=NULL) { printf("%s - %s\n",p->cmd,p->desc); p=p->next; } return 0;}int Help(){ ShowAllCmd(head);}int Add(){ printf("please input two integers\n"); int x,y; scanf("%d%d",&x,&y); int result=x+y; printf("%d\n",result); return 0;}int Sub(){ printf("please input two integers\n"); int x,y; scanf("%d%d",&x,&y); int result=x-y; printf("%d\n",result); return 0;}int Multiple(){ printf("please input two integers\n"); int x,y; scanf("%d%d",&x,&y); int result=x*y; printf("%d\n",result); return 0;}int Divide(){ printf("please input two integers\n"); int x,y; scanf("%d%d",&x,&y); int result=x/y; printf("%d\n",result); return 0;}int Quit(){ exit(0);}
4.编辑linklist.h文件
#include <stdio.h>#include <stdlib.h>typedef struct DataNode{ char* cmd; char* desc; int(*handler)(); struct DataNode* next;}tDataNode;tDataNode* FindCmd(tDataNode* head,char* cmd);int ShowAllCmd(tDataNode* head);int help();int quit();int add();int sub();int multiple();int divide();5.编写menu.c文件
#include <stdio.h>#include <stdlib.h>#include "linklist.h"static tDataNode head[] ={ {"help","This is Help",help,&head[1]}, {"add","This is Add input 2 integers!",add,&head[2]}, {"sub","This is Sub input 2 integers!",sub,&head[3]}, {"multiple","This is Multiple input 2 integers!",multiple,&head[4]}, {"divide","This is Divide input 2 integers!",divide,&head[5]}, {"quit","This is Quit will quit from program!",quit,NULL}};int main(){ while(1) { char cmd[128]; printf("please put in a cmd\n"); scanf("%s",cmd); tDataNode *p=FindCmd(head,cmd); if(p==NULL) { printf("wrong command!\n"); continue; } printf("%s:%s\n",p->cmd,p->desc); if(p->handler!=NULL) { p->handler(); } }}
6.编译menu.c以及linklist.c文件
7.运行结果
8.上传github
9.github的上传情况
10.我的github地址
https://github.com/lili0001002/cs122/tree/master/lab3
总结:
- 学习了模块化的编程思想
- 提高了menu程序的重用性
- 掌握学习了c程序的编译调试方法
- KISS(keep it simple & stupid)
- using design to frame the code(matching design with implementation)
- including pseuducode
阅读全文
0 0
- SA17225160_李伯威(libowei)_高级软工第三次实验报告
- SA17225160_李伯威(libowei)_高级软工第一次实验报告
- SA17225160_李伯威(libowei)_高级软工第四次实验报告
- SA17225160_李伯威(libowei)_高级软工第五次实验报告
- SA17225160_李伯威(libowei)_高级软工第六次实验报告
- 第三次上机实验 报告
- 第三次上机实验报告
- 第三次上机实验报告
- 第三次上机实验报告
- 第三次实验报告
- 第三次上机实验报告
- 第三次上机实验报告
- 第三次上机实验报告
- 第三次实验报告
- 第三次实验报告
- 第三次上机实验报告
- 第三次上机实验报告
- 第三次实验报告
- 一起学Netty(三)之 SimpleChannelInboundHandler
- chapter3:协同过滤-隐式评级及基于物品的过滤
- java-反射机制-简单代码
- Tomcat配置,修改端口号,命令行启动
- c语言的三种数字排序方法
- SA17225160_李伯威(libowei)_高级软工第三次实验报告
- 拦截器的执行顺序
- gdb调试器
- 学习PHP静态化笔记
- Unity_DOTween动画的学习(八)_动画的生命周期和生命周期函数
- 团圆的中秋, 我一个人在code
- Ros_1
- Java 多线程(PART I)进程和线程
- WmS工作原理