C语言多级菜单框架

简介

主要用于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)();//执行当函数
        }
    }
}

实验效果

在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值