正在加载图片...
实验七选择结构( Switch语句) 预习内容 预习 switch语句的定义格式及执行过程。 二、实验目的 掌握 switch语句中条件与程序流程关系。 实验内容 程序(一)功能:用户键入任意两个操作数和“+、-、*、/”中一个运算 符,由计算机输出运行结果。 参考程序: #include<stdio. h> malI float result, x,y printf( \input x, y: scanf (%f, %f, &x, &y) printf( \input operator(+,,,/): " scanf("%",&ch) result=o switch(ch) result=xty: break case result=x-y: break result=x*y: break case ' if(y!=0. 0) result=x/y else printf("divide overflow") default printf( sorry you have make mistakes please do it again") printf(%6. 2f%c%6. 2f=, x, ch, y) printf(%6. 2f\n", result18 实验七 选择结构(Switch 语句) 一、预习内容 预习 switch 语句的定义格式及执行过程。 二、实验目的 掌握 switch 语句中条件与程序流程关系。 三、实验内容 程序(一) 功能:用户键入任意两个操作数和“+、-、*、/”中一个运算 符,由计算机输出运行结果。 参考程序: #include<stdio.h> main() { float result,x,y; char ch; printf("\ninput x,y:"); scanf("%f,%f",&x,&y); printf("\ninput operator(+,-,*,/):"); scanf(" %c",&ch); result=0; switch(ch) {case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': if(y!=0.0) result=x/y; else printf("divide overflow"); default: printf("sorry you have make mistakes please do it again"); } printf("%6.2f%c%6.2f=",x,ch,y); printf("%6.2f\n",result);
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有