资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言case函数菜单,c中case用法

C语言,用switch语句写个选择菜单,case后面怎么写函数调用?调用下面的程序?

学过调用函数吧,就是那么调用的,当然也可以自己直接写。举个例子吧。数据a b,如果我想操作加减乘除,那么可以这么写:

成都创新互联是一家专业从事网站制作、网站设计的网络公司。作为专业网站建设公司,成都创新互联依托的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、网络营销推广及网站设计开发服务!

#includestdio.h

void jiafa(int,int);/*函数声明*/

void main()

{

int a,b,n;

scanf("%d%d",a,b);

scanf("%d",n);

switch(n)

{

case 1:

jiafa(a,b); /*这里可以写成调用函数,也可以像下面一样直接写*/

break;

case2:printf("%d",a-b); break;

case3:printf("%d",a*b); break;

case3:printf("%d",a/b); break;

}

}

void jiafa(int a,int b)/* 函数定义*/

{

printf("%d",a+b);

}

求一个C语言菜单函数的程序?

1、对于窗口组件菜单,需要根据不同平台,通过图形编程接口,进行菜单的编制。

例程:

#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(); 

}

2、对于命令行菜单,直接通过不断刷新输出来模拟菜单行为。

例程:

#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");

}

c语言中case的意思和用法

c语言中case是和switch一起使用的,构成switch—case语句,进行判断选择,case是用来表示选择结构的。

switch语句的一般形式为:

switch(表达式){

case 常量表达式1:  语句1;

case 常量表达式2:  语句2;

case 常量表达式n:  语句n;

default:  语句n+1;}

其语义是:计算表达式的值。 并逐个与其后的常量表达式值相比较,当表达式的值与某个常量表达式的值相等时, 即执行其后的语句,然后不再进行判断,继续执行后面所有case后的语句。如表达式的值与所有case后的常量表达式均不相同时,则执行default后的语句。

扩展资料

任何switch语句都必须遵循以下规则:

1、只能针对基本数据类型中的整型类型使用switch,这些类型包括int、char等。对于其他类型,则必须使用if语句。

2、switch()的参数类型不能为实型 。

3、case标签必须是常量表达式(constantExpression),如42或者"42"。

4、case标签必须是惟一性的表达式;也就是说,不允许两个case具有相同的值。

参考资料来源:百度百科——switch


分享名称:c语言case函数菜单,c中case用法
分享URL:http://cdkjz.cn/article/dsepsgd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220