全国计算机等级考试二级教程-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() ...
随机推荐
- C语言的本质(37)——makefile之隐含规则和模式规则
Makefile有很多灵活的写法,可以写得更简洁,同时减少出错的可能.本节我们来看看这样一个例子还有哪些改进的余地. 一个目标依赖的所有条件不一定非得写在一条规则中,也可以拆开写,例如: main.o ...
- UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>
K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Su ...
- python分布式抓取网页
呵呵,前两节好像和python没多大关系..这节完全是贴代码, 这是我第一次写python,很多地方比较乱,主要就看看逻辑流程吧. 对于编码格式确实搞得我头大..取下来页面不知道是什么编码,所以先找c ...
- 见过的最好的Layout讲解,挺全的
见过的最好的Layout讲解,挺全的 1. http://wenku.baidu.com/link?url=-9wSWx-oLet8R51iXUbikEMWZF8DK4-n6AqoA5_fk3rtrh ...
- Gradle+Jetty实现静态资源的热部署
本文转自http://www.cnblogs.com/huang0925/p/3302487.html --------------------------------------- 通过Gradle ...
- PHP 文件打开/读取
PHP Open File - fopen() 打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. 在课程中,我们将使用文本文件 "w ...
- 从3dmax中导入模型到UDK Editor(供个人备忘)
笔记从3dmax中导入模型到UDK Editor 1) 在3dmax中导出 2) 选择FBX格式,保存 3) 在UDK中打开content browser,自己选个pac ...
- cloudstack4.4新增功能前瞻
cloudstack4.4.0新功能前瞻 转载请注明地址:http://blog.csdn.net/zt689/article/details/37698989 1. cloudstack4.4. ...
- CSS学习笔记——盒模型,块级元素和行内元素的区别和特性
今天本来打算根据自己的计划进行前端自动化的学习的,无奈早上接到一个任务需求需要新增一个页面.自从因为工作需要转前端之后,自己的主要注意力几 乎都放在JavaScript上面了,对CSS和HTML这方面 ...
- .NET防止SQL、JS、HTML注入
/// <summary> /// 过滤标记 /// </summary> /// <param name="NoHTML">包括HTML,脚本 ...