还是用C++简单,map<string,command>....
程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct menu_item
{
char title[50];
char key;
void (*pf) (void);
};
int ShowMenu(const menu_item* menu);
void Fun1(void);
void Fun2(void);
void Fun3(void);
void Pause(void);
void main(void)
{
const menu_item menu[] = {
{ "0.Exit", '0', 0 },
{ "1.Call Function1", '1', Fun1 },
{ "2.Call Function2", '2', Fun2 },
{ "3.Call Function3", '3', Fun3 },
{ "" }
};
int choice;
do
{
choice = ShowMenu(menu);
if (choice != 0)
{
menu[choice].pf ();
}
} while (choice != 0);
}
int ShowMenu(const menu_item menu[])
{
system("CLS");
for (int index = 0; strlen(menu[index].title) != 0; ++index)
{
printf_s("%s\n", menu[index].title);
}
printf_s("\nSelect: ");
int choice = -1;
do
{
int keypress = _getch();
for (int index = 0; strlen(menu[index].title) != 0; ++index)
{
if (keypress == menu[index].key)
{
printf_s("%c\n\n", keypress);
choice = index;
break;
}
}
} while (choice == -1);
return choice;
}
void Fun1(void)
{
printf_s("Now in Function1\n\n");
Pause();
}
void Fun2(void)
{
printf_s("Now in Function2\n\n");
Pause();
}
void Fun3(void)
{
printf_s("Now in Function3\n\n");
Pause();
}
void Pause(void)
{
printf_s("keypress any key to continue...");
_getch();
}
