程序自动执行流程的实现

⌚Time: 2024-05-06 01:35:00

👨‍💻Author: Jack Ge

程序可以实现多个功能,但是我并不想立刻执行,需要审核或者按照顺序触发。

实现方法:

  1. 一个描述操作信息的结构体
  2. 解析各种操作的功能函数
  3. 存放各种操作的容器/链表
  4. 从文件保存和读取操作功能接口

1.定义结构体

struct MS{
    int id;//操作id
    time_t time;//添加操作的时间
    int type;//操作类型
    ...//其它的信息
    int x,y,z;
    int pos;
};

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){
...
}

使用方法:

添加操作

add_operat(op);

执行操作

struct MS op = get_operat_by_id(id);
run_operation(op);

保存操作到文件

save_operation_to_file("bk", vMV);

从文件恢复操作

load_operation_from_file("bk", vMV);

获取操作描述

char buff[1024];
puts(say_operation(buff, vMV));

对于一个程序,如果让他顺序执行操作,只需要顺序取出容器/链表中的操作,依次交给run_operation函数去解析执行就可以了。