c - 如何在 ANSI/ISO C 中编写控制台菜单?

标签 c

我正在尝试用 c 语言创建一个简单的程序,用户必须在其中的几个选项之间进行选择:

char command = '1';
while(command!='0') {
    printf("Menu:\n");
    printf("1. First option\n");
    printf("2. Second option\n");
    printf("0. Exit\n");
    printf("Choose: 0,1,2?: ");
    command = getchar();
    while(getchar()!='\n');     
    switch(command) {
        case '0': break;
        case '1': functionCall1(); break; 
        case '2': functionCall2(); break;
    }
}

我的代码的问题是,每当我输入 1,2 或 0 时,什么也没有发生,只有菜单再次打印出来。通过调试器,我可以看到,command 的值在 command = getchar() 之后等于 '',每隔一次。我以为吃换行符就够了?

最佳答案

试试我的自定义菜单,我实现它是为了让我在处理包含多项选择操作的程序时更轻松。 菜单是可导航的(箭头:上、下、左、右),只需按回车键即可进行选择,菜单方向可以垂直或水平设置,填充可以设置为一组项目( child ), child 的起始位置,并延迟更新。

菜单调用示例(垂直):

int response = menu("OPTIONS","[*]","->",
                    1,3,3,0,5,
                    "PROFILES","ACTIVITY","VIDEO","SOUND","GAMEPLAY");

最重要的是因为功能实现只需要60行代码。

菜单实现:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <windows.h>

// LXSoft
// mod: cui/menu_021
// stdarg.h  -> used for variable list of arguments (va_list, va_start ...)
// windows.h -> used for Sleep function, for *nix use unistd.h

typedef unsigned short int usint_t;
// Menu function prototype
int menu(char* name, char* prefix, char* cursor, usint_t orientation,
         usint_t padding, usint_t start_pos, usint_t delay,
         usint_t num_childs, ...);

int main()
{
    int response = menu("OPTIONS","[*]","->",1,3,3,0,5,
                        "PROFILES","ACTIVITY","VIDEO","SOUND","GAMEPLAY");
    switch(response)
    {
        case 1:
            // doSomethingFoo1();
        break;
        case 2:
            //doSomethingFoo2();
        break;
        /*
         * .
         * .
         * .
         * case n:
         * break;
         */
    }
    printf("\nYour choice is: %d", response);
    return 0;
}

// Menu implementation
int menu
(
    char *name,        // Menu name (eg.: OPTIONS)
    char *prefix,      // Menu prefix (eg.: [*])
    char *cursor,      // Menu cursor (eg.: ->)
    usint_t orient,    /*
                        * Menu orientation vertical or horzontal.
                        * 0 or false for horizontal
                        * 1 or true for vertical
                        */
    usint_t padding,   // Menu childrens padding (eg.: 3)
    usint_t start_pos, // Menu set active child (eg.: 1)
    usint_t delay,     // Menu children switch delay
    usint_t childs,    // Number of childrens
    ...                /*
                        * Variable list of arguments char* type.
                        * Name of the childrens.
                        */
)
{
    va_list args;
    int tmp=0,pos;
    char chr;
    usint_t opt=start_pos;
    char* format=malloc
    (
        (
            strlen(name)+strlen(prefix)+strlen(cursor)+
            3+ /* menu suffix (1 byte) and backspace (2 bytes) */
            (2*childs)+ /* newline (2 bytes) times childs */
            (padding*childs)+ /* number of spaces times childs */
            childs*15 /* children name maxlen (15 bytes) times childs*/
        )*sizeof(char)
    );
    do
    {
        if(tmp!=0)chr=getch();
        if(chr==0x48||chr==0x4B)
            (opt>1&&opt!=1)?opt--:(opt=childs);
        else if(chr==0x50||chr==0x4D)
            (opt>=1&&opt!=childs)?opt++:(opt=1);
        else {/* do nothing at this time*/}
        strcpy(format,"");
        strcat(format,prefix);
        strcat(format,name);
        strcat(format,":");
        va_start(args,childs);
        for (tmp=1;tmp<=childs;tmp++)
        {
            (orient)?strcat(format,"\n"):0;
            pos=padding;
            while((pos--)>0) strcat(format," ");
            if(tmp==opt)
            {
                strcat(format,"\b");
                strcat(format,cursor);
            }
            strcat(format,va_arg(args,char*));
        }
        /*if(tmp!=childs)
        {
            fprintf(stderr,"%s: recieved NULL pointer argument,"
                           " child not named", __func__);
            return -1;
        }*/
        Sleep(delay);
        system("cls");
        printf(format);
        va_end(args);
    }while((chr=getch())!=0x0D);
    return opt;
}

关于c - 如何在 ANSI/ISO C 中编写控制台菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10571127/

相关文章:

任何人都可以详细解释以下 c 中的指针程序吗?

c - 在 C 中访问多维数组的等效语法

c - 如何通过C-Linux向串口发送AT命令

c - 任意概率分布方式的随机数生成器

c - c中的while循环无限期地进行

c - 遍历C指针列表: weird printf behaviour

c - 我需要在 C 中生成随机数

C - 在 Visual Studio 2012 中使用 scanf_s 打印字符时出错

c - 浮点到字符串表示

c++ - 无符号整数如何工作