全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构

时间:2023-03-08 21:20:24
全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构

switch什么时候用break,什么时候不用break

调用break:一次执行一个分支,输入一个数据,对应一个级别

不调用break:连续执行多个分支

if...else

可以处理任何情况,大于小于等于与或非等复杂逻辑都可以处理,看起来不够简洁。

switch

只能处理常量,处理字符整数型常量,看起来很简洁。

case标签值必须是常量,只能判断相等。

在 if 或 else 后面总是用{}

即使只有一条语句的时候

if 最简单的用法

 #include <stdio.h>
main()
{
if ()
printf("AAAA\n"); //会输出 if ()
printf("BBBB\n"); //不会输出 if ( == )
printf("CCCC\n"); //会输出
}

if 的常见问题解析

1 空语句的问题

 #include <stdio.h>
main()
{
if ( > ); //等价于 if ( > )
; //这是一个空语句
}

2

 #include <stdio.h>
main()
{
if ( > )
printf("AAAA\n");
else
printf("BBBB\n"); //是正确的 if ( > );
printf("AAAA\n");
else
printf("BBBB\n"); //是错误的
}

3

 #include <stdio.h>
main()
{
if ( > )
printf("AAAA\n");
else if ( > )
printf("BBBB\n");
else if ( > )
printf("CCCC\n");
else
printf("DDDD\n"); //即便表达式1和2都成立,也只会执行A语句
}

4

 #include <stdio.h>
main()
{
int x;
scanf("%d", &x); if (x >= )
printf("AAAA\n");
else if (x >= )
printf("BBBB\n");
else if (x >= )
printf("CCCC\n");
else if (x >= )
printf("DDDD\n"); //最后不加上else,不会出错,但是逻辑上有漏洞
}

5

 #include <stdio.h>
main()
{
int x;
scanf("%d", &x); if (x >= )
printf("AAAA\n");
else if (x >= )
printf("BBBB\n");
else if (x >= )
printf("CCCC\n");
else if (x >= )
printf("DDDD\n");
else (x < ); //最后else不能加表达式
printf("EEEE\n");
}

在计算机中可以精确地存放一个整数,不会出现误差,但整型数值的数值范围比实数小。实型数的数值范围较整型大,但往往存在误差。

如何判断实型x 是否为0

 #include <stdio.h>
main()
{
double x = 0.000000; if (x - 0.000001 <= 0.0001 || x - 0.000001 >= -0.0001)
printf("AAAA\n");
else
printf("BBBB\n");
}

在多层 switch 嵌套中,break 只能终止距离它最近的 switch

 #include <stdio.h>
main()
{
int x = , y = , a = , b = ; switch (x)
{
case :
switch (y)
{
case :
a++;
break;
case :
b++;
break;
}
b = ;
break;
case :
a++;
b++;
break;
} printf("a=%d,b=%d\n", a, b);
}

输出格式:

a=1,b=100

请按任意键继续. . .

continue 在 for 语句

 #include <stdio.h>
main()
{
for (;;)
{
A;
B;
continue; // 如果执行该语句,则执行完该语句后,会执行语句3,C和D会被跳过,C和D不会被执行
C;
D;
}
}

continue 在 while 语句

 #include <stdio.h>
main()
{
while (表达式)
{
A;
B;
continue; // 如果执行该语句,则执行完该语句后,会执行表达式,C和D会被跳过,C和D不会被执行
C;
D;
}
}

scanf 对用户非法输入的处理

 #include <stdio.h>
main()
{
int i;
char ch; scanf("%d", &i);
printf("i=%d\n", i); while ((ch = getchar() != '\n'))
continue; int j;
scanf("%d", &j);
printf("j=%d\n", j);
}

1输入长方形的三个边,计算长方形的体积。

 #include <stdio.h>
main()
{
double a, b, c, s, v;
printf("input a,b,c: \n");
scanf("%lf %lf %lf", &a, &b, &c);
s = a*b; /*计算长方形的面积*/
v = a*b*c; /*计算长方形的体积*/
printf("a=%lf,b=%lf,c=%lf \n", a, b, c);
printf("s=%lf v=%lf", s, v);
}

2把560分钟换算成用小时和分钟表示,然后输出。

 #include <stdio.h>
main()
{
int a = , h, m;
h = / ;
m = % ;
printf("560分钟=%d小时%d分钟", h, m);
}

3读入三个双精度数,求它们的平均值并保留此平均值小数点后一位数,对小数点后第二位数进行四舍五入,最后输出结果。


 #include <stdio.h>
main()
{
double a, b, c, d;
printf("input a,b,c: \n");
scanf("%lf %lf %lf", &a, &b, &c);
d = (a + b + c) / ;
d = d * ;
d = d + 0.5;
d = (int)d;
d = d / ;
printf("平均值是%lf", d);
}

4读入三个整数给a, b, c,然后交换它们中的数,把a中原来的值给b,把b中原来的值给c,把c中原来的值给a,然后输出a,b,c。

 #include <stdio.h>
main()
{
int a, b, c, t;
printf("input a,b,c: \n");
scanf("%d %d %d", &a, &b, &c);
t = a;
a = c;
c = b;
b = t;
printf("%d %d %d", a, b, c);
}

5输入三个整数,分别放在变量a,b,c中,然后把输入的数据重新按由小到大的顺序放在变量a,b,c中,最后输出a,b,c中的值。

 #include <stdio.h>
main()
{
int a, b, c, t;
printf("input a,b,c: \n");
scanf("%d %d %d", &a, &b, &c);
printf("a=%d,b=%d,c=%d \n", a, b, c);
if (a>b) /*如果a比b大,则进行交换,把小的数放入a中*/
{ t = a;a = b;b = t; }
if (a>c) /*如果a比c大,则进行交换,把小的数放入a中*/
{ t = a;a = c;c = t; }
if (b>c) /*如果b比c大,则进行交换,把小的数放入b中*/
{ t = b;b = c;c = t; }
printf("%d %d %d", a, b, c);
}

6输入两个数,分别赋给x和y,输出其中的大数。

 #include <stdio.h>
main()
{
int x, y;
printf("input x&y: \n");
scanf("%d %d", &x, &y);
printf("x=%d,y=%d \n", x, y);
if (x > y)
printf("max=x=%d \n", x);
else
printf("max=y=%d \n", y);
}

7输入一个数,判别它是否能被3整除。若能被3整除,打印YES;不能被3整除,打印NO。

 #include <stdio.h>
main()
{
int n;
printf("input n:");
scanf("%d", &n);
if (n % == ) /*判断n能否被3整除*/
printf("n=%d YES \n", n);
else printf("n=%d NO \n", n);
}

8根据输入的学生成绩给出相应的等级,大于或等于90分以上的等级为A,60分以下的等级为E,其余每10分为一个等级。

8.1用 if,else if 方法

 #include <stdio.h>
main()
{
int g;
printf("enter g:");
scanf("%d", &g);
printf("g=%d:", g);
if (g >= ) printf("A \n");
else if (g >= ) printf("B \n");
else if (g >= ) printf("C \n");
else if (g >= ) printf("D \n");
else printf("E \n");
}

8.2用 switch,break 方法

 #include <stdio.h>
main()
{
int g;
printf("enter g:");
scanf("%d", &g);
printf("g=%d:", g);
switch (g / )
{
case :printf("A \n");break;
case :printf("A \n");break;
case :printf("B \n");break;
case :printf("C \n");break;
case :printf("D \n");break;
default:printf("E \n");break;
}
}

9若a的值小于100,a<30 m=1; a<40 m=2; a<50 m=3; a<60 m=4; else m=5;

9.1用 if,else if 方法。

 #include <stdio.h>
main()
{
int a, m;
printf("input a:");
scanf("%d", &a);
if (a < ) m = ;
else if (a < ) m = ;
else if (a < ) m = ;
else if (a < ) m = ;
else m = ;
printf("%d", m);
}

9.2用 switch ,break 方法

 #include <stdio.h>
main()
{
int a;
printf("input a:");
scanf("%d", &a);
switch (a/)
{
case :printf("");break;
case :printf("");break;
case :printf("");break;
case :printf("");break;
default:printf("");
}
}

10输入一位学生的生日(年月日),并输入当前的日期(年月日),输出实际年龄。

 #include <stdio.h>
main()
{
int y0, m0, d0, y1, m1, d1, age;
printf("input birthday:");
scanf("%d %d %d", &y0, &m0, &d0);
printf("生日为%d年%d月%d日 \n", y0, m0, d0); printf("input sysdate:");
scanf("%d %d %d", &y1, &m1, &d1);
printf("当前日期为%d年%d月%d日 \n", y1, m1, d1); age = y1 - y0;
if (m1 < m0 || m1 == m0&&d1 < d0)
age = age - ; printf("实际年龄为%d", age);
}

11输入一个整数,打印出它是奇数还是偶数。

 #include <stdio.h>
main()
{
int a;
printf("输入一个整数:");
scanf("%d", &a);
if (a % == ) printf("偶数 \n");
else printf("奇数 \n");
}

12输入a,b,c三个数,打印最大值。

 #include <stdio.h>
main()
{
int a, b, c, t;
printf("input a,b,c:");
scanf("%d %d %d", &a, &b, &c);
printf("a=%d,b=%d,c=%d \n", 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("max=%d", t);
}

13对于以下函数,要求输入x的值,输出y的值。

y=x (-5<x<0)

y=x-1 (x=0)

y=x+1 (0<x<10)

13.1不嵌套的if语句

 #include <stdio.h>
main()
{
double x, y;
printf("input x:");
scanf("%lf", &x);
printf("x=%lf \n", x); if (x > (-) && x < )
y = x;
if (x == )
y = x - ;
if (x > && x < )
y = x + ; printf("y=%lf", y);
}

13.2嵌套的if语句

 #include <stdio.h>
main()
{
double x, y;
printf("input x:");
scanf("%lf", &x);
printf("x=%lf \n", x); if (x > (-) && x < ) y = x;
else if (x == ) y = x - ;
else if (x > && x < ) y = x + ; printf("y=%lf", y);
}

13.3 if-else的语句

 #include <stdio.h>
main()
{
double x, y;
printf("input x:");
scanf("%lf", &x);
printf("x=%lf \n", x); if (x == ) (y = x - );
else {
if (x > (-) && x < ) (y = x);
if (x > && x < ) (y = x + );
} printf("y=%lf", y);
}

13.4 switch 语句

 #include <stdio.h>
main()
{
double x, y;
int n;
printf("input x:");
scanf("%lf", &x); if (x > (-) && x < ) n = ;
if (x == ) n = ;
if (x > && x < ) n = ; switch (n)
{
case :y = x;break;
case :y = x - ;break;
case :y = x + ;break;
} printf("y=%lf", y);
}