㈠ 怎样用C语言编写菜单
对于窗口组件菜单,需要根据不同平台,通过图形编程接口,进行菜单的编制。
例程:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
voidmain()
{
charstr;
inti,k,choice=1;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(2);
settextstyle(3,0,3);
outtextxy(140,120,"A.TheMockClock.");
outtextxy(140,150,"B.TheDigitalClock.");
outtextxy(140,180,"C.Exit.");
setlinestyle(0,0,3);
rectangle(170,115,370,145);
/*按上下键选择所需选项*/
for(i=1;i<=100;i++)
{
str=getch();
if(str==72)
{
--choice;
if(choice==0)choice=3;
}
if(str==80)
{
++choice;
if(choice==4)choice=1;
}
if(str==13)break;/*按回车键确认*/
/*画图做菜单*/
cleardevice();
switch(choice)
{case1:setlinestyle(0,0,3);
rectangle(170,115,400,145);
settextstyle(3,0,3);
outtextxy(140,120,"A.TheMockClock.");
settextstyle(3,0,3);
outtextxy(140,150,"B.TheDigitalClock.");
outtextxy(140,180,"C.Exit.");
break;
case2:setlinestyle(0,0,3);
rectangle(170,145,400,175);
settextstyle(3,0,3);
outtextxy(140,120,"A.TheMockClock.");
settextstyle(3,0,3);
outtextxy(140,150,"B.TheDigitalClock.");
settextstyle(3,0,3);
outtextxy(140,180,"C.Exit.");
break;
case3:settextstyle(3,0,3);
outtextxy(140,120,"A.TheMockClock.");
outtextxy(140,150,"B.TheDigitalClock.");
settextstyle(3,0,3);
outtextxy(140,180,"C.Exit.");
setlinestyle(0,0,3);
rectangle(170,175,400,205);
break;
}
}
if(i>=100)exit(0);/*如果按键超过100次退出*/
switch(choice)/*这里引用函数,实现所要的功能*/
{
case1:cleardevice();
setbkcolor(4);
settextstyle(3,0,4);
outtextxy(160,120,"No.1havenotbuilt.");break;
case2:cleardevice();
setbkcolor(4);
settextstyle(3,0,4);
outtextxy(160,150,"No.2havenotbuilt.");
break;
case3:exit(0);
}
getch();
closegraph();
}
对于命令行菜单,直接通过不断刷新输出来模拟菜单行为。
例程:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intn,t,k;
intm;
chars1[20],s2[20],c;
char**l;
char*num[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
voidmenu()
{
printf(" ******************************************************* ");
printf(" **1.查找字符串S1中S2出现的次数** ");
printf(" **2.统计字符串中大小写字母,数字出现的次数** ");
printf(" **3.将数字翻译成英语** ");
printf(" **4.结束** ");
printf(" ******************************************************* ");
printf(" 您的输入:");
fflush(stdin);
scanf("%d",&n);
}
voidcheck()
{
chara[20],b[20];
intj=0,k,m,l=0;
intt=0,n=0;
printf("请输入主字符串: ");
scanf("%s",a);
k=strlen(a);
printf("请输入子字符串: ");
scanf("%s",b);
m=strlen(b);
for(n=0;n<k;n++)
if(a[n]==b[0])
{
j++;/*记录相同的字符数*/
do
{
if(a[++n]==b[++t])
{
j++;
if(j==m)
{
l++;/*子字符串相同数*/
j=0;/*判断后相同字符数归零*/
t=-1;/*判断中if中++t;t将会归零*/
}
}
else
{
j=0;
t=0;
break;/*如果不同跳出while循环让for使n+1继续判断*/
}
}while(a[n]!='