C Exercise 2
100 Classic C Language Examples
**Problem:** A company distributes bonuses based on profit commissions.
- When the profit (I) is less than or equal to 100,000 yuan, the bonus is 10%;
- When the profit is above 100,000 but below 200,000 yuan, the portion below 100,000 earns 10% commission and the portion above 100,000 earns 7.5%;
- Between 200,000 and 400,000 yuan, the portion above 200,000 earns 5%;
- Between 400,000 and 600,000 yuan, the portion above 400,000 earns 3%;
- Between 600,000 and 1,000,000 yuan, the portion above 600,000 earns 1.5%;
- Above 1,000,000 yuan, the portion exceeding 1,000,000 earns 1% commission.
Enter the monthly profit I from the keyboard and find the total bonus to be paid.
**Analysis:** Use a number line to define the boundaries and locate the ranges. Note that when declaring the bonus, define it as a double-precision floating-point (double) type.
Example
#include
<
stdio.h
int
main
(
)
{
double
i;
double
bonus1
,
bonus2
,
bonus4
,
bonus6
,
bonus10
,
bonus
;
printf
(
"
Your net profit is:
\
n
"
)
;
scanf
(
"
%lf
"
,&
i
)
;
bonus1
=
100000
0
.1
;
bonus2
=
bonus1
100000
0
.075
;
bonus4
=
bonus2
200000
0
.05
;
bonus6
=
bonus4
200000
0
.03
;
bonus10
=
bonus6
400000
0
.015
;
if
(
i
<=
100000
)
{
bonus
=
i
0
.1
;
}
else
if
(
i
<=
200000
)
{
bonus
=
bonus1
(
i
100000
)
0
.075
;
}
else
if
(
i
<=
400000
)
{
bonus
=
bonus2
(
i
200000
)
0
.05
;
}
else
if
(
i
<=
600000
)
{
bonus
=
bonus4
(
i
400000
)
0
.03
;
}
else
if
(
i
<=
1000000
)
{
bonus
=
bonus6
(
i
600000
)
0
.015
;
}
else
if
(
i
1000000
)
{
bonus
=
bonus10
(
i
1000000
)
0
.01
;
}
printf
(
"
The commission is: bonus=%lf
"
,
bonus
)
;
printf
(
"
\
n
"
)
;
}
The output of the above example is:
Your net profit is:
120000
提成为:bonus=11500.000000