1 / 31

语言程序设计

福州职业技术学院 计算机系. 21 世纪高职高专计算机系列规划教材. 语言程序设计. 中国铁道出版社. 第一节 问题的提出. ( 1 )在做选择的时候一般都会要求给出一个条件,根据这个条件是否满足来作出判断并决定程序的走向。因此在设计时要注意给出一个条件,且条件应该能够得到一个确定的真假值,这个条件可以用我们学过的关系表达式、逻辑表达式来构成。 ( 2 )在 C 语言中实现选择结构用什么语句,在 C 语言中,一般用 if 语句或 switch 语句来实现选择结构 ( 3 )选择一般可以是两种可能,即二重分支,但也可以是多重分支,即进行多重选择。. 第二节 if 语句.

wanda-ortiz
Download Presentation

语言程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 福州职业技术学院 计算机系 21世纪高职高专计算机系列规划教材 语言程序设计 中国铁道出版社

  2. 第一节 问题的提出 • (1)在做选择的时候一般都会要求给出一个条件,根据这个条件是否满足来作出判断并决定程序的走向。因此在设计时要注意给出一个条件,且条件应该能够得到一个确定的真假值,这个条件可以用我们学过的关系表达式、逻辑表达式来构成。 • (2)在C语言中实现选择结构用什么语句,在C语言中,一般用if语句或switch语句来实现选择结构 • (3)选择一般可以是两种可能,即二重分支,但也可以是多重分支,即进行多重选择。

  3. 第二节 if语句 • 一、单分支语句 if()~ else ~ • if(表达式) 语句组1 else 语句组2 • 功能:如果表达式值为1,则执行语句组1,否则执行语句组2

  4. 【例1】关于求符号函数画流程图分析下面四个程序的对错。【例1】关于求符号函数画流程图分析下面四个程序的对错。 • 【程序1】 • #include "stdio.h" • void main() • { • int x,y; • scanf("%d",&x); • if(x<0) y=-1; • else if(x==0) y=0; • else y=1; • printf("x=%d,y=%d\n",x,y); • }

  5. 【程序2】 • #include "stdio.h" • void main() • { • int x,y; • scanf("%d",&x); • if(x>=0) • if(x>0) y=1; • else y=0; • else y=-1; • printf("x=%d,y=%d\n",x,y); • }

  6. 【程序3】 • #include "stdio.h" • void main() • { • int x,y; • scanf("%d",&x); • y=-1; • if(x!=0) • if(x>0) y=1; • else y=0; • printf("x=%d,y=%d\n",x,y); • }

  7. 【程序4】 • #include "stdio.h" • void main() • { • int x,y; • scanf("%d",&x); • y=-1; • if(x!=0) • if(x>0) y=1; • else y=0; • printf("x=%d,y=%d\n",x,y); • }

  8. 【例2】输入3个数,按照从小到大的顺序输出 • #include "stdio.h" • void main() • { int a,b,c,t; • scanf("%d%d%d",&a,&b,&c); • if(a>b){t=a;a=b;b=t;} • if(a>c){t=a;a=c;c=t;} • if(b>c){t=b;b=c;c=t;} • printf("\n%2d<%2d<%2d\n",a,b,c); • }

  9. 二、分支语句的嵌套问题 • 【例3】 • 输入一个0~100分范围内的一个成绩,显示相应的等级:90~100 优 80~89 良70~79 中 60~69 及格0~59 不及格 其他数字 输入错误

  10. 【程序段】 • #include "stdio.h" • void main() • { int sco; • printf("请输入学生的成绩"); • scanf("%d",&sco); • if(sco>100||sco<0) printf("Input error\n"); • else if(sco>=90) printf("Excellent\n"); • else if(sco>=80) printf("good\n"); • else if(sco>=70) printf("medium\n"); • else if(sco>=60) printf("pass\n"); • else printf("failure\n"); • }

  11. 【例4】 • 已知一个数学函数为f(x)=0 xx+10-x-x-10x<00<=x<1010<=x<2020<=x<30x>=30

  12. 【答案】 • #include "stdio.h" • void main() • { • float x,y; • printf("请输入自变量x的值"); • scanf("%f",&x); • if(x<0) y=0; • else if(x<10) y=x; • else if(x<20) y=x+10; • else if(x<30) y=-x; • else y=-x-10; • printf("分段函数f(x)的值为%.2f\n",y); • }

  13. 【例5】判断从键盘输入字符的种类,将字符分为五类:(1)控制字符(ASCII值小于32) (2)数字字符 (3) 大写字母字符 (4) 小写字母字符 (5) 其他字符

  14. #include "stdio.h" • void main() • { • char ch; • printf("请输入一个字符\n"); • scanf("%c",&ch); • if(ch<=32) printf("控制字符\n"); • else if(ch>='0'&&ch<='9') printf("数字字符\n"); • else if(ch>='a'&&ch<='z') printf("小写字母字符\n"); • else if(ch>='A'&&ch<='Z') printf("大写字母字符\n"); • else printf("其他字符\n"); • }

  15. 【例6】输入一个字符,判别如果大写字母,则转换成小写,如果是小写字母则转换成大写,否则不转换【例6】输入一个字符,判别如果大写字母,则转换成小写,如果是小写字母则转换成大写,否则不转换 • 【#include "stdio.h" • void main() • { • char ch; • printf("请从键盘上输入一个字符\n"); • scanf("%c",&ch); • if(ch>='a'&&ch<='z') ch-=32; • else if(ch>='A'&&ch<='Z') ch+=32; • printf("转换后的字符是%c\n",ch); • }

  16. [例题] 程序A: if(a>b&&a>c) max=___; else if(b>c) max=____; else max=____;

  17. 程序B: if(a>b) if(a>c) max=____; else max=___; else if(b>c) max=___; else max=___;

  18. <<C语言程序设计>> p62/ 程序分析题1。 void main() {int x=100,a=200,b=50; int v1=25,v2=20; if(a<b) if(b!=50) if(!v1) x=11; else if(v2) x=12; x=______; printf("%d",x);}

  19. 第三节switch语句 C语言提供了switch多路选择语句。格式如下: switch(表达式) { case 常量表达式1 : 语句组1 ;[break;] case 常量表达式2 : 语句组2 ; [break;] … case 常量表达式n : 语句组n ;[break;] default : 语句组n+1 ; }

  20. 分析多分支语句的执行过程 执行完一个case语句后面的语句后,流程控制转移到下一个case语句继续执行。 ’case常量表达式’只起到语句标号的作用,并不是在该处进行条件判断。在执行switch语句时,根据switch后面表达式的值找到匹配的入口标号,就从此标号开始执行下去,不再进行判断。 每一个case的常量表达式的值必不相同,各个case和default语句的出现次序不影响结果。多个case语句可以共用一组执行语句。

  21. 例题1 :输入一个表示星期的数字(0表示星期天,1表示星期一,。。。6表示星期六),显示对应的英文单词。(须与break联合使用)

  22. int day;scanf("%d",&day); switch(day) { case 0: printf("Sunday!");break; case 1: printf("Monday!");break; case 2: printf("Tuesday!");break; case 3: printf("Wednesday!");break; case 4: printf("Thursday!");break; case 5: printf("Friday!");break; case 6: printf("Saturday!");break; default: printf("What you input is wrong!"); } printf("\n");}

  23. 例题2:输入一个0~100分范围内的一个成绩,显示相应的等级:90~100 优 80~89 良70~79 中 60~69 及格0~59 不及格 其他数字输入错误

  24. int score; scanf("%d",&score); if(score>100||score<0) printf("What you input is wrong!"); else { printf("Your score is "); switch(score/10) {case 10: case 9:printf("Excellent!\n");break; case 8:printf("Good!\n");break; case 7:printf("Medium!\n");break; case 6:printf("Pass!\n");break; case 5: case 4: case 3: case 2: case 1: case 0:printf("failure!\n");break; } }

  25. 例题3 :给出年、月、日,判断该天是该年的第几天。(不必使用break语句)【教材p63编程题1。】

  26. int year,month,day,cou=0,flag; if(year%4==0&&year%100!=0||year%400==0) flag=29; else flag=28; switch(month) { case 12:cou+=30; case 11:cou+=31; case 10:cou+=30; case 9:cou+=31; case 8:cou+=31; case 7:cou+=30; case 6:cou+=31;case 5:cou+=30; case 4:cou+=31; case 3:cou+=flag; case 2:cou+=31; case 1:cou+=0; }cou+=day;

  27. 【嵌套switch语句程序示例】教材p62/程序分析题4.【嵌套switch语句程序示例】教材p62/程序分析题4.

  28. #include "stdio.h" void main() { int a=-1,b=1,c=5; switch(a>0) { case 1: switch(b-2<0) { case 1: printf("&");break; case 2: printf("*");break; } case 0: switch(c==5) { case 0: printf("!");break; case 1: printf("#");break; default: printf("%%");} default:printf(" @ ");} }

  29. 总结 对p63/3.的讨论 作业: P60/习题

More Related