# 利用switch中case的穿透性最大限度简洁代码结构
#### 目录
* 需要知识
* 简单介绍
* switch语句详解
* 无注释代码
* 含注释代码
* 总结
* 加入我们
## 需要知识:
* 了解基本数据类型 int;
* 会使用 scanf 输入语句;
* 会使用 printf 输出语句;
* 知道 switch 的基本特性(case的穿透性);
* 拥有能自主分析代码的基本能力 ┑( ̄Д  ̄)┍ 。
## 简单介绍
1. 首先定义两个 int 类型的变量 profit 和 bonus;
2. 用 profit 接受用户输入数据;
3. 书写 switch 语句的代码块内容;
4. 输出计算后的 bonus。
#### switch 语句详解
1. 利用 profit/100,000 和 case "num":的配合省略 if 语句的漫长判断语句;
2. 利用“穿透性”从最高位开始计算,从而省略复杂的计算表达式;
3. 注意使用case穿透性解决判断范围问题。
## 无注释代码
```
#include int main()
{
int profit,bonus=0;
scanf("%d", &profit);
switch(profit/100000)
{
case 10:
bonus += (profit - 1000000)*0.01;
profit = 1000000;
case 9:
case 8:
case 7:
case 6:
bonus += (profit - 600000)*0.015;
profit = 600000;
case 5:
case 4:
bonus += (profit - 400000)*0.03;
profit = 400000;
case 3:
case 2:
bonus += (profit - 200000)*0.05;
profit = 200000;
case 1:
bonus += (profit - 100000)*0.075;
profit = 100000;
case 0:
bonus += profit*0.1;
}
printf("%d\n", bonus);
return 0;
}
```
## 含注释代码
```
#include int main()
{
int profit,bonus=0;
scanf("%d", &profit);
switch(profit/100000)
{
case 10://负责计算超出 1,000,000 部分的利润
bonus += (profit - 1000000)*0.01;
profit = 1000000;//这步很关键!是利用穿透性简洁代码的核心步骤之一,需要细细体会一下哦!O(∩_∩)O
case 9://负责计算 600,000 到 1,000,000 部分的利润
case 8:
case 7:
case 6:
bonus += (profit - 600000)*0.015;
profit = 600000;
case 5://负责计算 400,000 到 600,000 部分的利润
case 4:
bonus += (profit - 400000)*0.03;
profit = 400000;
case 3://负责计算 200,000 到 400,000 部分的利润
case 2:
bonus += (profit - 200000)*0.05;
profit = 200000;
case 1://负责计算 100,000 到 200,000 部分的利润
bonus += (profit - 100000)*0.075;
profit = 100000;
case 0://负责计算低于 100,000 部分的利润
bonus += profit*0.1;
}
printf("%d\n", bonus);
return 0;
}
```
## 总结
怎么样?是不是很简洁而且脉络清晰,让人看了神清气爽呢?这也是我第一次发表关于 C语言的 研讨文章!希望大家可以多多支持,谢谢!
##### QQ群:1035426849
本群创建不久,还没有太多人!希望大家可以捧个场!慢慢的人数就能多起来了!无论你是大佬,还是小白我们都欢迎哦!(づ ̄3 ̄)づ╭❤~
0.0分
13 人评分