无聊写了个控制台程序用的菜单函数
发布时间:2011-12-28 作者:李卿 分类:代码整理 标签: 无标签

都期末了学校突然加课,3周上完32课时的所谓程序设计实训,
所谓程序设计实训,就是要做一个所谓的项目出来(学生管理系统之类的),
觉得实在无聊,就偷偷玩儿别的喽,正好想起以前写的控制台程序的菜单简陋到爆,
要输入数字然后回车,巨麻烦,实在是弱爆了。
百度了一下找到了conio.h这个头文件和getch()这个函数,果断很无聊的写个了菜单函数出来。
此函数特点如下:
- 可以用键盘操作(上下移动光标,右进入菜单)
- 可以直接输入数字
- 使用时只需要修改菜单内容和调用函数即可
- 效果很无聊
直接上代码,其实很简单,就是图个好玩儿,嘎嘎
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
//各种常量、全局变量声明
//TODO:修改对应的菜单说明
char menuList[][255]={ //菜单内容
{"第一个选项"},
{"第二个选项"},
{"第三个选项"},
{"第四个选项"},
{"第五个选项"},
{"第六个选项"},
{"第七个选项"},
{"第八个选项"},
{"第九个选项"},};
const int menuNum=sizeof(menuList)/sizeof(menuList[0]); //菜单个数
int menuN=1; //被选中的菜单位置
//各种函数声明
void showMenu();
void runMenu(int choose);
//显示主菜单
void showMenu()
{
system("cls");
cin.sync();
if (menuN==0) menuN=menuNum;
int i;
cout<<"\n"<<right<<setw(43)<<"主菜单"<<"\n\n";
for (i=0;i<menuNum;i++)
{
if (menuN==i+1) cout<<right<<setw(30)<<"->"<<i+1<<"、"<<menuList[i]<<endl<<endl;
else cout<<right<<setw(30)<<i+1<<"、"<<menuList[i]<<endl<<endl;
}
int a1,a2;
a1=getch();
if (a1==13) {runMenu(menuN);return;}
else if (a1>='0' && a1<='9') {menuN=a1-'0';runMenu(menuN);return;}
else if(a1==224) a2=getch();
if (a2==72) {menuN=(menuN+menuNum-1)%menuNum;showMenu();return;}
if (a2==80) {menuN=(menuN+1)%menuNum;showMenu();return;}
if (a2==77) {runMenu(menuN);return;}
return;
}
//执行主菜单中的对应项
void runMenu(int choose)
{
switch (choose)
{
//TODO:修改对应的函数
case 1:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 2:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 3:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 4:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 5:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 6:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 7:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 8:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
case 9:
cout<<"This is NO."<<choose<<" option.\n";system("pause");break;
default:return;
}
}
int main()
{
while (1) showMenu();
return 0;
}
January 3rd, 2012 at 16:27 回复
这个太厉害了,完全看不懂
January 5th, 2012 at 10:06 回复
嘿嘿,小卿子,拿来用啦~