全国计算机等级考试二级教程-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);
}
全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构的更多相关文章
- 全国计算机等级考试二级教程-C语言程序设计_第8章_地址和指针
面试: unsigned int *p1 = # int *p2 = # #define _CRT_SECURE_NO_WARNINGS #include<std ...
- 全国计算机等级考试二级教程-C语言程序设计_第15章_位运算
位运算,不适用于实数,仅仅适用于整数.字符. C语言的位运算只能操作整数.字符,实数是指数方式表示的,不适用于位运算. #define _CRT_SECURE_NO_WARNINGS #include ...
- 全国计算机等级考试二级教程-C语言程序设计_第9章_数组
四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型
函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...
- 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串
字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...
- 全国计算机等级考试二级教程-C语言程序设计_第5章_循环结构
for循环结构的嵌套 外层循环每循环一次,内层循环会完整循环一次. 外层循环是竖. 内层循环是横. for, do...while, while的选择: 如果有固定次数,如阶乘! ,判断素数,用 fo ...
- 全国计算机等级考试二级教程-C语言程序设计_第3章_顺序结构
1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(" ...
- 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识
正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...
- 全国计算机等级考试二级教程-C语言程序设计_第7章_函数
函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...
随机推荐
- Java中Array.sort()的几种用法
****************************************************** * 精品书籍推荐:<Java从入门到经通> * 本书系统全面.浅显易懂,非常适 ...
- JOB+MERGE 跨服务器同步数据
为了解决单服务器压力,将库分服务器部署,但是原来用触发器实现的表数据同步就实现不了了. 因为总监老大不允许 开启分布式事务(MSDTC),我又不想为了一个几千行的基础数据做复制订阅. 于是乎决定用 J ...
- Unity 鼠标点击左右移动,人物跟随旋转
上代码: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { private Vector ...
- [每日一题] OCP1z0-047 :2013-08-18 禁用启用约束――主键与外键 ..................................61
正确答案:C 根据题意,测试结果如下: 1.创建表emp,并且设emp_no字段为主键,设mgr_no字段为外键. gyj@MYDB> create table emp 2 (emp_no nu ...
- c# 图片简单模糊 非高斯模糊
/// <summary> /// 图像模糊化 /// </summary> /// <param name="bit ...
- linux下tar.xz 文件解压
在linux下下载源码文件安装时有些会遇到tar.xz文件的解压,习惯了tar解压缩,第一次遇到.xz文件还是有点迷惑,google 如下,解压这种格式的文件需要xz工具,如果xz工具没有安装,则安装 ...
- LoadRunner性能测试中Controller场景创建需注意的几点
在LR工具做性能测试中,最关键的一步是Controller场景的设计,因为场景的设计与测试用例的设计相关联,而测试用例的执行,直接影响最终的测试结果是怎么的,因此,我们每设计一种场景,就有可能是一个测 ...
- .net如何后台批量删除
button_Click(Sender sender,Event e){foreach (DataListItem item in DataList1.Items){CheckBox cbox=(Ch ...
- sql server数据库区分大小写设置
数据库表中字段alter Table TableName 区分大小写 ALTER Column ColumnName VARCHAR(50) COLLATE Chinese_PRC_CS_AS不区分大 ...
- Oracle 查看执行计划
刚刚开始接触Oracle,使用的工具是Sql Developer.在看执行计划的的时候,选中SQL语句,直接F5即可. 但是这里的执行计划不是最终的执行计划,它使用的是 explain for 命令. ...