程序可以实现多个功能,但是我并不想立刻执行,需要审核或者按照顺序触发。
实现方法:
- 一个描述操作信息的结构体
- 解析各种操作的功能函数
- 存放各种操作的容器/链表
- 从文件保存和读取操作功能接口
1.定义结构体
2.功能函数
//功能函数
void move(int x, int y, int z){
...
}
void install(int pos){
...
}
void uninstall(int pos){
...
}
//解析操作类型
void run_operation(struct MS arg){
switch(arg.type){
case 1:
move(arg.x,arg.y,arg.z);
break;
case 2:
install(arg.pos);
break;
case 3:
uninstall(arg.pos);
break;
case 4:
sleep(1000);
break;
default:
break;
}
}
//操作描述函数
char* say_operation(char buff[], struct MS arg){
memset(buff,0,sizeof(buff));
switch(arg.type){
case 1:
strcat(buff,"行走到...");
break;
case 2:
strcat(buff,"到x位置取货");
break;
case 3:
strcat(buff,"到x位置卸货");
break;
case 4:
strcat(buff,"等待1秒");
break;
}
return buff;
}3.容器和对应的增删查操作函数
vector<struct MS> vMV;
void add_operat(struct MS arg){
...
}
void del_operat_by_id(int id){
...
}
void get_operat_by_id(int id){
...
}4.将操作保存到文件,或从文件恢复的函数接口
void save_operation_to_file(char *fileName, vector<struct MS> arg){
...
}
void load_operation_from_file(char *fileName, vector<struct MS> &arg){
...
}使用方法:
添加操作
执行操作
保存操作到文件
从文件恢复操作
获取操作描述
对于一个程序,如果让他顺序执行操作,只需要顺序取出容器/链表中的操作,依次交给run_operation函数去解析执行就可以了。