#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef void(*CMD)(void);
void buyCommand(){
printf("Buy something\n");
}
void sellCommand(){
printf("Sell something\n");
}
#define CACHE_LEN 5
CMD cmdCache[CACHE_LEN]={};
void excuteCMD(){
static int pos=0;
if(cmdCache[pos]!=NULL){
(cmdCache[pos])();
pos++;
if(pos==CACHE_LEN)pos=0;
}else{
printf("No command excuted!\n");
}
}
void placeCMD(CMD cmd){
static int pos=0;
cmdCache[pos]=cmd;
pos++;
if(pos==CACHE_LEN)pos=0;
}
int main(){
excuteCMD();
placeCMD(buyCommand);
placeCMD(sellCommand);
excuteCMD();
excuteCMD();
return 0;
}
typedef struct {
void (*execute)(void);
} Command;
void executeA() { printf("Command A\n"); }
void executeB() { printf("Command B\n"); }
Command cmdA = { .execute = executeA };
Command cmdB = { .execute = executeB };
// 使用
cmdA.execute(); // 输出 "Command A"