C语言-第三章-条件判断

判断过程

1.1 布尔表达式

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <stdbool.h>

int main()
{
bool result=5>4;
printf("%d", result); //1。任何非0数值转换成bool类型时,都得到true,也就是1。
return 0;
}

1.2 简单的条件判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdbool.h>

int main()
{
int nef,too;
printf("你的学校是985吗?(是--1;不是--0)\n");
scanf("%d",&nef);
if(nef)
printf("我们公司欢迎您。");
else
{
printf("那是211吗?(是--1;不是--0)");
scanf("%d",&too);
if(too)
printf("还不错,继续");
else
{printf("我们考虑考虑。");}
}
return 0;
}

1.3 大小写转换

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <ctype.h> //引入大小写函数

int main()
{
char lch,uch; //lowerchar, upperchar
scanf("%c %c", &lch, &uch);
printf("%c %c", toupper(lch), tolower(uch)); //upperchar, lowerchar
return 0;
}

1.4 逻辑运算符

1.4.1 逻辑与运算符–&&

它是一个二元运算符,前后两个表达式的值同为true才为true。

1
2
if(age > 12 && age < 20)
printf("u r a saonian.");

1.4.2 逻辑或运算符–||

也是二元运算符,只有当全为false时,整个表达式的值才是false。

1.4.3 逻辑非运算符–!

一元运算符,颠倒是非。

1.5 条件运算符

三元运算符:
condition ? expression1 : expression2
如果条件为真,就执行表达式1,否则执行2。如:

1
x = 我的体重 > 你的体重 ? 我的体重 : 你的体重;

从右往左编译。如果我的体重 > 你的体重那么x = 我的体重,否则x = 你的体重。
如果用条件表达式,则代码更复杂点:

1
2
3
if(我的体重>你的体重)
x=我的体重;
else x=你的体重;

这在批发货物时能应用到:

1
totalmoney = unit_price * quantity * (quantity > 100 ? 0.95 : 1.0);

如果一次性买了超过100件商品,那么打95折。
还有一个单复数的例子:

1
printf("u have %d pet%s.", pets, pets == 1 ? "" : "s");

1.6 运算符优先级

赋值运算符 < 条件运算符 < 二元逻辑运算符 < 比较运算符 < 二元算术运算符。

特殊情况可以用括号改写优先级。

点击这里查看优先级详情

所有单目运算符,条件运算符和赋值运算符的匹配规则是从右到左,其它运算符从左到右匹配。

2 多项选择问题

在C语言中,有两种方式处理多项选择问题:1.else if语句;2.switch语句。

2.1 if else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()
{
int salary;
printf("Input ur salary:\n");
scanf("%d", &salary);
if(salary<5000)
printf("ur pay is very poor.");
else if(salary<15000)
printf("ur pay is not good.");
else if(salary<50000)
printf("ur pay is not bad.");
else
printf("exceptional.");
return 0;
}

2.2 switch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

int main()
{
int num;
printf("输入你的彩票号:\n");
scanf("%d", &num);
switch(num) //与主函数main一样,句末无分号
{
case 13: //此处用冒号
printf("Congratulations!一等奖!"); //句末正常使用分号
break; //每个case分支都有一个break;
case 38:
printf("second prize");
break;
case 120:
case 250:
case 380: //将相同功能的分支放在一起
case 999: //在新的一行上只是好看,可以直接贴在上一case: 后面
printf("third prize");
break;
default: //当没有匹配项时,选用缺省值。
printf("sorry, nothing.");
break;
}
return 0;
}

3 按位运算符

按位运算符看起来和逻辑运算符很像,但逻辑运算符判定的是两个两达式合值的真假,而按位操作的是整数值中的位,一个字节(byte)有八个位,就是这个位!所以也只能用于操作整数类型。

按位运算符

运算符说明

&按位与运算符,如果两个位都是1,结果才为1。int x=13,y=6;那么x的二进制表示:0000 1101;y: 0000 0110。x&y: 0000 0100(0*2^3+1*2^2+0*2^1+0*2^0),即十进制的4。

|按位或,只要相同位的值有一个为true,结果就是true。还是上例,x|y=0000 1111。即十进制的15。

^按位异或(EOR),两个位不相同,为true。x^y=0000 1011。

~按位非,单目运算符。~x=1111 0010。

<<按位左移n位,右边自动补0,结果x << 3的值等于x * 2^3。

>>按位右移,如果原值是正,则右边补0;如果是负数,要看系统怎么补,一般是补1



和算术运算符类似,逻辑运算符也支持op=用法。
lhs op= rhs;
等价于:
lhs = lhs op rhs;
例:
value <<=4;
value = value << 4;

4 小实践

4.1 问题描述

编写一个简单的计算器,实现两个数字的加、减、乘、除操作,在执行除操作时,还要确定其余数。如:5.6 * 27 or 3 + 6,测试用例可自行设计。

4.2 分析问题

1. 获得用户要求计算机执行计算所需的输入;
2. 检查输入,确保输入可被执行;
3. 执行计算;
4. 显示结果。

4.3 解决方案

4.3.1 step 1:

调用头文件<stdio.h>,用printf()和scanf()来输入输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main()
{
double num1=0.0,num2=0.0;
char op=0;

printf("输入表达式:(数 操作 数)\n");
scanf("%lf %c %lf", &num1, &op, &num2); //数和操作符之间用空格隔开

/* balabala */
return 0;
}

4.3.2 step 2:

检查输入是否有效,有效的操作有+ - * / 和 %。

另外还要检查第二个操作数,如果操作是/ or %,则它不能是0,可用switch语句完成。

将下面程序替换上面程序/* balabala */。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
switch(op)
{
case '+':
//add
break;
case '-':
//subtract
break;
case '*':
//multiply
break;
case '/':
if(num2 == 0)
printf("\nDivision by zero error!\n");
else //divide
break;
case '%':
if((long)num2 == 0)
printf("\nDivision by zero error!\n");
else //divide
break;
default:
printf("\nIllegal operation!");
break;
}

4.3.3 step 3:

当输入的操作符是’+’时:

1
printf(" = %lf\n", num1 + num2);

其它操作类似,自动将这些代码补入上面程序中。

需要注意的是’%’中,要将double强制转换成long,使用(long)num1这样。

4.3.4 step 4:

测试用例。

原书的完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <ctype.h>

int main(void)
{
double number1 = 0.0; /* First operand value a decimal number */
double number2 = 0.0; /* Second operand value a decimal number */
char operation = 0; /* Operation - must be +, -, *, /, or % */
char reply = 0; /* Indicates another calculation - or not*/

printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);

/* Code to check the input goes here */
switch(operation)
{
case '+': /* No checks necessary for add */
printf("= %lf\n", number1 + number2);
break;

case '-': /* No checks necessary for subtract */
printf("= %lf\n", number1 - number2);
break;

case '*': /* No checks necessary for multiply */
printf("= %lf\n", number1 * number2);
break;

case '/':
if(number2 == 0) /* Check second operand for zero */
printf("\n\n\aDivision by zero error!\n");
else
printf("= %lf\n", number1 / number2);
break;

case '%': /* Check second operand for zero */
if((long)number2 == 0)
printf("\n\n\aDivision by zero error!\n");
else
printf("= %lf\n", (long)number1 % (long)number2);
break;

default: /* Operation is invalid if we get to here */
printf("\n\n\aIllegal operation!\n");
break;
}
return 0;
}

5 本章小节

在这一章学习了条件控制,使程序可以有选择性的执行,下一章将介绍循环语句,让某一段代码执行多次。