简介
主要用于UI界面切换中。
框架代码
//多级菜单
#include <stdio.h>
//定义结构体
typedef struct
{
unsigned char current;
unsigned char up;//上键
unsigned char down;//下键
unsigned char enter; //确认键
void (*current_operation)();//函数指针
}key_table;
//菜单1
void fun1()
{
printf("Menu1\n");
}
//菜单2
void fun2()
{
printf("Menu2\n");
}
//菜单3
void fun3()
{
printf("Menu3\n");
}
//定义菜单组合数组
key_table table[7]={
{0,2,1,0,(*fun1)},
{1,0,2,0,(*fun2)},
{2,1,0,0,(*fun3)}
};
char Key=0;//按键缓存
unsigned char func_index=0;//菜单页
void (*current_operation_index)();//定义指针变量
int main(){
Key=0;
current_operation_index=table[func_index].current_operation;
//获取函数菜单地址
(*current_operation_index)();
//执行当函数
while(1)
{
Key=0;//按键清零
Key=getchar();//获取按键输入
if(Key=='2')
{
func_index=table[func_index].up;
//向上翻
current_operation_index=table[func_index].current_operation;
//获取函数菜单地址
Key=0;
//按键清零
(*current_operation_index)();//执行当函数
}
if(Key=='8')
{
func_index=table[func_index].down;
//向下翻
current_operation_index=table[func_index].current_operation;//获取函数菜单地址
Key=0;//按键清零
(*current_operation_index)();//执行当函数
}
}
}