如何制作带结构数组的菜单(将lua代码转换为c ++代码)

时间:2015-11-22 11:14:24

标签: c++ c lua

我使用了lua,我编写了一个菜单,我想在c ++中做同样的事情

function printMenu( menu )
  for i = 1,#menu do
    if i == selectedItem then
     print(">> "..menu[i].text)
    else
     print("   "..menu[i].text)
    end
  end
end
-- Handler Method --
function onKeyPressed( key, menu )
    if key == keys.enter then
        onItemSelected(menu)
    elseif key == keys.up then
        if selectedItem > 1 then
        selectedItem = selectedItem - 1
        end
    elseif key == keys.down then
        if selectedItem < #menu then
        selectedItem = selectedItem + 1
        end
    end
end
function onItemSelected( menu )
    menu[selectedItem].handler()
end
-- Main Method --
function main()
    while inMainMenu do
        term.clear()
        term.setCursorPos(1,1)
        printMenu(mainMenu)

        event, key = os.pullEvent("key")
        onKeyPressed(key,mainMenu)
    end
end

数组如下:

mainMenu = {
 [1] = { text = "Physics", handler = PhysicsMenu },
 [2] = { text = "Math [01-12]", handler = MathMenu1 },
 [3] = { text = "Math [12-24]", handler = MathMenu2 },
 [4] = { text = "Update", handler = Update },
 [5] = { text = "Exit", handler = Exit }
}

所以我基本上想把这段代码变成c ++ 我试过的是下面的

char* menuname[]
= { "calc", "internet", "painter", "movie", "update", "exit" };

void SelectItem(char* menu[])
{
    fflush(stdin);

    int key = _getch();

    if (key == 13)

    {
        system("CLS");

        switch (selecteditem)
        {
        case 1:Calcmenu(); break;
        case 2:CtoF(); wait(); break;
        case 3:painter(); break;
        case 4:printf("Work In Progress\n"); wait(); break;
        case 5:printf("Work In Progress\n"); wait(); break;
        case 6:exit(0); wait(); break;
        default: mainmenu(); break;
        }
    }

    switch (key)
    { 
        case 72: if(selecteditem > 1) selecteditem -= 1; break;
        case 80: if (selecteditem < 6) selecteditem += 1; break;
    }
}

void printmenu(char* menu[])
{
    gotoxy(20, 10);
    int menusize = strlen(*menu);

    for (int i = 0; i < menusize; i++)
    {
        if (i == selecteditem - 1)
        {
            gotoxy(30, 9 + i);
            printf(">>[%d]%s\n", i + 1, menu[i]);
        }
        else
        { 
            gotoxy(30, 9 + i);
            printf("  [%d]%s\n", i + 1, menu[i]);
        }
    }
}


void main()
{
    while (1)
    {
        system("CLS");
        printmenu(menuname);
        SelectItem(menuname);
    }

}
事实是,在lua中,我能够创建一个数组,只使用3个函数来制作任何菜单。但在c ++中我认为我必须使用struct并在lua中创建一个新数组。

1 个答案:

答案 0 :(得分:0)

你的编码非常好。我的意思是,Lua。经过一些练习,你也将成为一名优秀的C程序员。

以下是如何在C中实现它(它不是真正的C ++):

#include <stdio.h>

/* ---------------------- type -------------------------*/

/* Define type 'menu_item' as a menu item. */
typedef struct {
  char *text;
  void (*handler)();  /* a pointer to a function */
} menu_item;

/* --------------------- handlers ----------------------*/

void ItemCalc()
{
   printf("Launching the calculator...\n");
}

void ItemPainter()
{
   printf("Launching the painter...\n");
}

void ItemMovie()
{
   printf("Launching the movie...\n");
}

/* --------------------- data --------------------------*/

menu_item my_menu[] = {
  { "calc", ItemCalc },
  { "painter", ItemPainter },
  { "movie", ItemMovie },
};

int my_menu_size = sizeof(my_menu) / sizeof(menu_item);

int selecteditem = 1;

/* -------------------- I/O-----------------------------*/

/*
  Doing I/O is very platform-dependent. On my system there
  isn't _getch(). So to make it possible to run this program
  everywhere we simulate an I/O system:
*/

int _key = 0;

void simulate_key_press(int key)
{
    _key = key;
}

int read_key()
{
    return _key;
}

/* -----------------------------------------------------*/


void SelectItem(menu_item menu[], int menu_size)
{
    fflush(stdin);

    int key = read_key();

    if (key == 13)
    {
        printf("I'm executing item %d:\n", selecteditem);
        menu[selecteditem].handler();
    }
    else
    {
       int num = (key - '0'); // convert characters '1'..'9' to integers 1..9.

       if (num >= 1 && num <= menu_size) {
          selecteditem = num;
          printf("item %d selected.\n", num);
       }
       else
          printf("Invalid key. Either ENTER or digits are allowed\n");
    }
}

void printmenu(menu_item menu[], int menu_size)
{
    int i;

    for (i = 0; i < menu_size; i++)
    {
        if (i == selecteditem - 1)
        {
            printf(">>[%d]%s\n", i + 1, menu[i].text);
        }
        else
        { 
            printf("  [%d]%s\n", i + 1, menu[i].text);
        }
    }
}

void main()
{
    printmenu(my_menu, my_menu_size);
    simulate_key_press('2');
    SelectItem(my_menu, my_menu_size);
    printmenu(my_menu, my_menu_size);
    simulate_key_press(13);
    SelectItem(my_menu, my_menu_size);
}

至于使用数组:有两种方法可以找出它们的大小。上面我明确告诉函数数组的大小。另一种方法是使用“sentry”值来标记数组的结尾:

menu_item my_menu[] = {
  { "calc", ItemCalc },
  { "painter", ItemPainter },
  { "movie", ItemMovie },
  { NULL, NULL },
};

然后在你的循环中,当你到达这个元素时停止:

void printmenu(menu_item menu[])
{
    int i;

    for (i = 0; menu[i].text != NULL; i++)
    {

像Lua这样的“脚本”语言与像C(或C ++)这样的低级语言之间存在很多差异。我希望我的代码会激励你学习更多知识。即使您最终不会使用C,也值得学习它以了解机器的实际工作方式。