C语言中如何实现选项菜单的功能?
就是在一段简单的程序执行完以后让用户输入不同的字符以继续不同的步骤,比如:1.返回重新输入2.退出这样的功能用什么语句实现,谢谢...
就是在一段简单的程序执行完以后让用户输入不同的字符以继续不同的步骤,比如:
1.返回重新输入 2.退出
这样的功能用什么语句实现,谢谢 展开
1.返回重新输入 2.退出
这样的功能用什么语句实现,谢谢 展开
4个回答
展开全部
#include<stdio.h>
#include <stdlib.h>
void hello()
{
printf("hello world\n");
}
int main()
{
int x;
while (1)
{
printf("-------------操作选项-----------\n");
printf("1:返回菜单 \n");
printf("2:退出程序 \n");
printf("3:执行操作 \n");
printf("--------------------------------\n");
printf("按数字键选择要执行的操作: ");
scanf("%d",&x);
printf("\n");
//输入2跳出循环,退出程序
if(x==2)
break;
switch(x)
{
case 1: break; //输入1,跳出switch语句,进入下一次循环
case 3: hello();break;
default: //数字输入错误,跳出siwtch语句,进入下一次循环
printf("输入的数字不正确\n");
break;
}
}
return 0;
}
说明:有3个选项,1是用返回菜单,2是用于退出程序,3是用于执行相关的操作,这里只是一个示例,所以将要执行的操作,编写成了一个输出hello world的函数。
展开全部
#include<stdio.h>
#include <stdlib.h>
int runs();
int main()
{
int x;
for(;;)
{
printf("\n");
printf("\n");
printf(" Menu");
printf("\n");
printf("\n");
printf("1:返回 \n");
printf("\n");
printf("\n");
printf("2:退出 \n");
printf("\n");
printf("\n");
printf("3:显示 \n");
printf("\n");
printf("\n");
printf("please choice the number or press 0 to Exit: ");
scanf("%d",&x);
printf("\n");
printf("\n");
if(x==2)
break;
switch(x)
{
case 1: break;
case 3: runs();break;
default:
printf(" Error!!!\n");
break;
}
}
return 0;
}
int runs()
{
int i,j;
char a[7][7]={{' ',' ', ' ','*'},{' ',' ','*','*','*'},{' ','*','*','*','*','*'},{'*','*','*','*','*','*','*'},{' ','*','*','*','*','*'},{' ',' ','*','*','*'},{' ',' ',' ','*'}};
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
printf("%c",a[i][j]);
printf("\n");
}
printf("\n");
printf("press any key to return Menu : ");
system("pause");
printf("\n");
system("cls");
return 0;
}
#include <stdlib.h>
int runs();
int main()
{
int x;
for(;;)
{
printf("\n");
printf("\n");
printf(" Menu");
printf("\n");
printf("\n");
printf("1:返回 \n");
printf("\n");
printf("\n");
printf("2:退出 \n");
printf("\n");
printf("\n");
printf("3:显示 \n");
printf("\n");
printf("\n");
printf("please choice the number or press 0 to Exit: ");
scanf("%d",&x);
printf("\n");
printf("\n");
if(x==2)
break;
switch(x)
{
case 1: break;
case 3: runs();break;
default:
printf(" Error!!!\n");
break;
}
}
return 0;
}
int runs()
{
int i,j;
char a[7][7]={{' ',' ', ' ','*'},{' ',' ','*','*','*'},{' ','*','*','*','*','*'},{'*','*','*','*','*','*','*'},{' ','*','*','*','*','*'},{' ',' ','*','*','*'},{' ',' ',' ','*'}};
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
printf("%c",a[i][j]);
printf("\n");
}
printf("\n");
printf("press any key to return Menu : ");
system("pause");
printf("\n");
system("cls");
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-07-17
展开全部
#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 ;
}
#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 ;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
google终端转义字符 例如:\b 删除上一个字符
printf
剩下的就是死循环等输入了
printf
剩下的就是死循环等输入了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询