对于窗口组件菜单,需要根据不同平台,通过图形编程接口,进行菜单的编制。
我们提供的服务有:网站建设、网站制作、微信公众号开发、网站优化、网站认证、徐州ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的徐州网站制作公司
例程:
#includestdio.h
#includegraphics.h
#includeconio.h
void main()
{
char str;
int i,k,choice=1;
int gd=DETECT,gm;
initgraph(gd,gm," ");
setbkcolor(2);
settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
outtextxy(140,150,"B. The Digital Clock.");
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)
{ case 1: setlinestyle(0,0,3);
rectangle(170,115,400,145);
settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
settextstyle(3,0,3);
outtextxy(140,150,"B. The Digital Clock.");
outtextxy(140,180,"C. Exit.");
break;
case 2: setlinestyle(0,0,3);
rectangle(170,145,400,175);
settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
settextstyle(3,0,3);
outtextxy(140,150,"B. The Digital Clock.");
settextstyle(3,0,3);
outtextxy(140,180,"C. Exit.");
break;
case 3: settextstyle(3,0,3);
outtextxy(140,120,"A. The Mock Clock.");
outtextxy(140,150,"B. The Digital Clock.");
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)/*这里引用函数,实现所要的功能*/
{
case 1: cleardevice();
setbkcolor(4);
settextstyle(3,0,4);
outtextxy(160,120,"No.1 have not built."); break;
case 2: cleardevice();
setbkcolor(4);
settextstyle(3,0,4);
outtextxy(160,150,"No.2 have not built.");
break;
case 3: exit(0);
}
getch();
closegraph();
}
对于命令行菜单,直接通过不断刷新输出来模拟菜单行为。
例程:
#include stdio.h
#include stdlib.h
#include string.h
int n,t,k;
int m;
char s1[20],s2[20],c;
char **l;
char *num[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
void menu()
{
printf("\n\n\t\t*******************************************************\n");
printf("\t\t** 1.查找字符串S1中S2出现的次数 **\n");
printf("\t\t** 2.统计字符串中大小写字母,数字出现的次数 **\n");
printf("\t\t** 3.将数字翻译成英语 **\n");
printf("\t\t** 4.结束 **\n");
printf("\t\t*******************************************************\n");
printf("\t\t 您的输入:");
fflush(stdin);
scanf("%d",n);
}
void check()
{
char a[20],b[20];
int j=0,k,m,l=0;
int t=0,n=0;
printf("请输入主字符串:\n");
scanf("%s",a);
k=strlen(a);
printf("请输入子字符串:\n");
scanf("%s",b);
m=strlen(b);
for(n=0;nk;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]!='\0');/*查找完字符数组a结束*/
}
printf("子字符串出现次数:\n%d\n",l);
}
void cout()
{
int n=0,t=0,k=0;
printf("请输入一个字符串:\n");
fflush(stdin);/*清除缓冲*/
while((c=getchar())!='\n')
{
if(c='a'c='z')
n++;
if(c='A'c='Z')
t++;
if(c='0'c='9')
k++;
}
printf("有大写字母:\n%d\n",t);
printf("有小写字母:\n%d\n",n);
printf("有数字:\n%d\n",k);
}
void number()
{
l=num;
printf("请输入一个数字:(0-10)\n");
fflush(stdin);
scanf("%d",m);
printf("%d对应的英文是:\n%s\n",m,*(l+m-1));
}
void main()
{
while(1)
{
system("cls");
menu();
switch(n)
{
case 1:system("cls");check();system("pause");break;
case 2:system("cls");cout();system("pause");break;
case 3:system("cls");number();system("pause");break;
case 4:system("cls");break;
default:system("cls");break;
}
if(n==4) break;
}
printf("感谢使用\n");
}
#include iostream
#include stdlib.h
#include conio.h
#include ctype.h
using namespace std;
void xuanzepaixu() //选择排序函数
{
printf("待添加,请按任意键继续...");
getchar();
}
void erfenpaixu() //二分查找函数
{
printf("待添加,请按任意键继续...");
getchar();
}
void menu() //主菜单
{
system("cls"); //清屏
printf("\n\t\t\t 菜单\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t# 1选择排序 #\n");
printf("\t\t\t# 2二分查找 #\n");
printf("\t\t\t# 3返回 #\n");
printf("\t\t\t# 4---关闭 #\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t 请选择(1-4) =:");
}
void main()
{
char select;
while(1)
{
menu();
system("COLOR 9f");
scanf("%c",select);
if(select=='3')
break; //返回上级菜单
else
{
getchar(); //读入回车符
if(!isdigit(select)) //如果不是数字字符
{
printf("\n\7Your select may be wrong, must enter the digit!\n");_getch();
}
else
{
switch (select)
{
case '1': xuanzepaixu();break;
case '2': erfenpaixu();break;
case '4':exit(0); //直接退出
default:
{
printf("\n\7\7Your selected digit may be wrong, select again!\n");
_getch();
break;
}
}
}
}
}
}
C语言中的函数的作用是可以将一段经常需要使用的代码封装起来,在需要使用时可以直接调用。
示例:
语言的函数定义和函数声明
#includestdio.h
//使用函数前,需要在main函数前对使用的函数进行声明
int getMax(int, int);
void main() {
int t=getMax(12, 21);
printf("%d\n", t);
getchar();
}
int getMax(int a, int b) {
if (a b) {
return a;
}
else {
return b;
}
}
扩展资料
scanf 函数
函数原型:int scanf(char *format [,argument,...])
功能:从标准输入设备(一般指键盘)实现格式化输入
返回值:成功返回读入的数据项个数
函数参数:format 格式化字符串
argument 输入数据项的地址列表
常用格式类型参考printf函数的个格式类型
用法举例:
从标准输入设备中输入一个整数到整形变量a中,输入一个浮点数到浮点型变量b中
int a=0;
float b=0;
scanf("%d%f",a,b);
上面程序执行后,从键盘输入: 19 18.6 ,程序将 19赋值给变量a, 18.6赋值给变量b 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#includestdio.h
#includewindows.h
//\n1.增加 2.查询 3.删除 4.退出\n");
//声明函数
void addnd();
void searchnd();
void printnd();
void charund();
//含 枚举类型 分支switch
enum mmenu{/*定义一个名为mmenu的枚举类型
可以让常量有一定的含义(如实现菜单)*/
addn=1,//默认是0,定义为1后 后面的成员也会自动变换(排序)
searchn,
printn,
quitn
};
void main(){
enum mmenu input;
while(1){
printf("菜单\n1.增加 2.查询 3.打印 4.退出\n");
scanf("%d",input);
Sleep(500);
system("cls");
switch(input){//case内的情况 只能是整型或字符型的常量
case addn:printf("选中了1.增加\n");addnd();break;
case searchn:printf("选中了2.查询\n");searchnd();break;
case printn:printf("选中了3.打印\n");printnd();break;
case quitn:printf("选中了4.退出\n");exit(0);break;
default :printf("不在选项内,请重新输入!\n");break;}
};
}
void addnd(){//增加
printf("//请自定义addnd()函数代码//\n");
}
void searchnd(){
printf("//请自定义searchnd()函数代码//\n");
}
void printnd(){//打印
printf("//请自定义printnd()函数代码//\n");
}
void charund(){
printf("//请自定义charund函数代码//\n");
}
#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;i8;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;i7;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;iwidth;i++) putch(0xc4);
putch(0xbf);
for( i=starty+1;ihigh;i++)
{
gotoxy(startx,i);putch(0xb3);
gotoxy(width,i);putch(0xb3);
}
gotoxy(startx,high);
putch(0xc0);
for (i=startx+1;iwidth;i++) putch(0xc4);
putch(0xd9);
return ;
}