当前位置:编程学习 > C/C++ >>

C语言中如何实现选项菜单的功能?

就是在一段简单的程序执行完以后让用户输入不同的字符以继续不同的步骤,比如:
1.返回重新输入  2.退出
这样的功能用什么语句实现,谢谢
答案:
#include <process.h> 
#include <dos.h>
#include <conio.h>
#define Key_DOWN 0x5100
#define Key_UP 0x4900
#define Key_ESC 0x011b
#define Key_ALT_F 0x2100
#define Key_ALT_X 0x2d00
#define Key_ENTER 0x1c0d

void box(int startx,int starty,int high,int width);

main()
{
int i,key,x,y,l;
char *menu[] = {"File","Edit","Run","Option","Help","Setup","Zoom","Menu"};
char *red[] = { "F","E","R","O","H","S","Z","M" };
char *f[] = {"Load file", "Save file", "Print", "Modify ", "Quit A1t_x"};
char buf[16*10*2],buf1[16*2];
while(1)
{
textbackground(BLUE);
clrscr();
textmode(C80);
window(1,1,80,1);
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
gotoxy(5,1);
for(i=0,l=0;i<8;i++)
{
x=wherex();
y=wherey();
cprintf("%s",menu[i]);
l=strlen(menu[i]);
gotoxy(x,y);
textcolor(RED);
cprintf("%s",red[i]);
x=x+l+5;
gotoxy(x,y);
textcolor(BLACK);
}
gotoxy(5,1);
key=bioskey(0);
switch (key){
case Key_ALT_X:
exit(0);
case Key_ALT_F:
{
textbackground(BLACK);
textcolor(WHITE);
gotoxy(5,1);
cprintf("%s",menu[0]);
gettext(5,2,20,12,buf);
window(5,2,20,9);
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
box(1,1,7,16);
for(i=2;i<7;i++)
{ gotoxy(2,i);
cprintf("%s",f[i-2]);
}
gettext(2,2,18,3,buf1);
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,2);
cprintf("%s",f[0]);
gotoxy(2,2);
y=2;

while ((key=bioskey(0))!=Key_ALT_X)
{
if ((key==Key_UP)||(key==Key_DOWN))
{
puttext(2,y,18,y+1,buf1);
if (key==Key_UP)
y=y==2?6:y-1;
else
y=y==6?2:y+1;
gettext(2,y,18,y+1,buf1);
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,y);
cprintf("%s",f[y-2]);
gotoxy(2,y);
}
else
if (key==Key_ENTER)
{
switch ( y-1 ){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
exit(0);
default:
break;
}
break;
}
else
if (key==Key_ESC)
break;
}
if (key==Key_ALT_X) exit(0);
break;
}
}
}
}

void box(int startx,int starty,int high,int width)
{ int i;
gotoxy(startx,starty);
putch(0xda);
for (i=startx+1;i<width;i++) putch(0xc4);
putch(0xbf);
for( i=starty+1;i<high;i++)
{
gotoxy(startx,i);putch(0xb3);
gotoxy(width,i);putch(0xb3);
}
gotoxy(startx,high);
putch(0xc0);
for (i=startx+1;i<width;i++) putch(0xc4);
putch(0xd9);
return ;
}

用if语句。

1。需要一个清屏函数,这个单词我记不太清了可能是(setm("cty").

2.需要一个break函数;

system("pause");

在#include<stdlib.h>里面声明。

上一个:c语言中的语句有哪几类?
下一个:C语言程序设计,数据处理的综合应用。

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,