4 include stdio h include math h void

  • Slides: 56
Download presentation

第 4章 选择程序设计 #include <stdio. h> #include <math. h> void main() { float x,

第 4章 选择程序设计 #include <stdio. h> #include <math. h> void main() { float x, y; printf ("Enter x : "); scanf ("%f", &x ); 选择结构 关系运算 if ( x != 0. 0 ) y = sin (x) / x; else y = 1; printf ("x =%5. 2 fty =%7. 4 fn", x, y); } C语言程序设计 7

4. 3 if语句 3、多分支if-else if 语句 if ( P 1 ) S 1 else

4. 3 if语句 3、多分支if-else if 语句 if ( P 1 ) S 1 else if ( P 2 ) S 2 ┆ 真 S 1 P 1 假 真 else if ( Pn ) Sn S 2 else Sn+1 C语言程序设计 P 2 假 Pn 真 Sn 假 Sn+1 19

4. 3 if语句 #include <stdio. h> void main() { int a, b, c, max;

4. 3 if语句 #include <stdio. h> void main() { int a, b, c, max; printf("Please input three numbers: "); scanf("%d, %d", &a, &b, &c); /* 输入 3个整数,用逗号分隔 */ max = a; /* 假设a最大 */ if(b>max) max =b; /* 如果b较大则最大数为b */ if (c>max) max =c; /* 如果c较大则最大数为c */ printf("The three numbers are: %d, %dn", a, b, c); printf("max=%dn", max); } C语言程序设计 21

4. 3 if语句 #include <stdio. h> void main() { float w, p ; printf("Please

4. 3 if语句 #include <stdio. h> void main() { float w, p ; printf("Please input weight: "); scanf("%f", &w); if( w<=50 ) p = 0. 95*w; else p = 0. 95*50 + (w-50)*1. 5; printf("price is %. 2 fn", p); } C语言程序设计 23

4. 3 if语句 #include <stdio. h> void main() { int n, r, t; printf("enter

4. 3 if语句 #include <stdio. h> void main() { int n, r, t; printf("enter a number: "); scanf("%d", &n); if( n%7 = =0 ) { t = n/7; printf("quotient is %dn", t); } else { t = n /7; r = n % 7; printf("quotient is %d, remainder is %dn", t, r); } } C语言程序设计 25

4. 3 if语句 #include <stdio. h> void main() { int year, leap; printf("Please enter

4. 3 if语句 #include <stdio. h> void main() { int year, leap; printf("Please enter year: "); scanf("%d", &year); if( year%400==0) leap=1; else if( year%4 ==0 && year%100 != 0) leap=1; else leap=0; if( leap ) printf("%d is a leap year. n", year); else printf("%d is not a leap year. n", year); } C语言程序设计 28

4. 3 if语句 #include <stdio. h> void main() { int score; printf ("输入分数:"); scanf

4. 3 if语句 #include <stdio. h> void main() { int score; printf ("输入分数:"); scanf ("%d", &score ); if ( score >= 90 ) printf ("优秀n"); else if ( score >= 80 ) printf ("良好n"); else if ( score >= 70 ) printf ("中等n"); else if ( score >= 60 ) printf ("及格n"); else printf ("不及格n"); } C语言程序设计 31

4. 3 if语句 嵌套的形式: if ( P 1 ) if ( P 2 )

4. 3 if语句 嵌套的形式: if ( P 1 ) if ( P 2 ) A else B else if ( P 3 ) C else D if ( P 1 ) A if ( P 2 ) A else if ( P 3 ) C else D 对应P 1、 else D P 2? if ( P 1 ) if ( P 2 ) A else B else C else 与最近的 if 匹配!! C语言程序设计 33

4. 3 if语句 if ( P 1 ) if ( P 2 ) A

4. 3 if语句 if ( P 1 ) if ( P 2 ) A else if ( P 3 ) C else D if ( P 1 ) { if ( P 2 ) A } else if ( P 3 ) C else D if ( P 1 ) if ( P 2 ) A else C if ( P 1 ) { if ( P 2 ) A } else C else if 语句也可以看作是嵌套的 if 语句 C语言程序设计 34

4. 3 if语句 例6:判断一个年份是否闰年,用嵌套的if语句实现。 #include <stdio. h> void main() { int year, leap=0; /*

4. 3 if语句 例6:判断一个年份是否闰年,用嵌套的if语句实现。 #include <stdio. h> void main() { int year, leap=0; /* leap预置为 0,非闰年 */ printf("Please input the year: "); scanf("%d", &year); if (year % 100 != 0) { if (year % 4==0) leap=1; } else { if (year%400==0) leap=1; } if (leap) printf("%d is a leap year. n", year); else printf("%d is not a leap year. n", year); } C语言程序设计 35

4. 3 if语句 #include <stdio. h> void main() { float x, y; printf ("input

4. 3 if语句 #include <stdio. h> void main() { float x, y; printf ("input x, y: "); scanf ("%f, %f", &x, &y); if ( x > 0 ) if ( y > 0 ) printf ("在第一象限n"); else printf ("在第四象限n"); else if ( y > 0 ) printf ("在第二象限n"); else printf ("在第三象限n"); } C语言程序设计 37

4. 5 switch语句 #include <stdio. h> void main() { int age; printf("Please enter age:

4. 5 switch语句 #include <stdio. h> void main() { int age; printf("Please enter age: "); scanf("%d", &age); switch(age) { case 2: case 3: printf("入小班n"); break; case 4: printf("入中班n"); break; case 5: case 6: printf("入大班n"); break; default: printf("不能入园n"); } } C语言程序设计 43

4. 5 switch语句 #include <stdio. h> void main() { int score, k; printf (

4. 5 switch语句 #include <stdio. h> void main() { int score, k; printf ( "input a score: "); scanf ( "%d", &score); k= score / 10; switch ( k ) { case 10: case 9: printf ("优秀n"); case 8: printf ("良好n"); case 7: printf ("中等n"); case 6: printf ("及格n"); default: printf ("不及格n"); } } break; C语言程序设计 45

4. 6 选择程序设计 #include <stdio. h> void main() { int a, b, c, t;

4. 6 选择程序设计 #include <stdio. h> void main() { int a, b, c, t; printf ("input 3 numbers: "); scanf ("%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 ("sorted result:%d, %dn", a, b, c); } C语言程序设计 48

4. 6 选择程序设计 #include <stdio. h> #include <math. h> /* 用到了数学库函数sqrt */ void main()

4. 6 选择程序设计 #include <stdio. h> #include <math. h> /* 用到了数学库函数sqrt */ void main() { float x, y, r; int score; printf ("input x, y: "); scanf ("%f, %f", &x, &y); r = sqrt ( x * x + y * y ); if ( r <= 1 ) score = 10; else if ( r <= 2) score = 9; else if ( r <= 3) score = 8; else if ( r <= 4) score = 7; else if ( r <= 5) score = 6; else score = 0; printf ("score: %dn", score); } C语言程序设计 51

4. 6 选择程序设计 #include "stdio. h" #include "math. h" void main() { float a,

4. 6 选择程序设计 #include "stdio. h" #include "math. h" void main() { float a, b, c, disc, x 1, x 2; printf("Please input a、b、c:"); scanf("%f%f%f", &a, &b, &c); /* 输入数据用空白分开 */ if(a==0) printf("The equation is not a quadratic. n"); C语言程序设计 54

4. 6 选择程序设计 else { disc=b*b-4*a*c; if(disc<0) printf("The equation has not real roots. n");

4. 6 选择程序设计 else { disc=b*b-4*a*c; if(disc<0) printf("The equation has not real roots. n"); else if(disc>0) { x 1=(-b+sqrt(disc))/(2*a); x 2=(-b-sqrt(disc))/(2*a); printf("The equation has distinct real roots: " ); printf(" %7. 3 f and %7. 3 fn", x 1, x 2); } C语言程序设计 55

4. 6 选择程序设计 else { x 1 =-b /2 /a; printf("The equation has two

4. 6 选择程序设计 else { x 1 =-b /2 /a; printf("The equation has two equal real roots: "); printf(" %7. 3 fn", x 1); } } } C语言程序设计 56