一.计算货物运费
总运费 = 基本运费 × 货物重量 × 路程 × (1-折扣)
路程 km | 折扣 |
---|---|
0 ≤ s<250 | 没有折扣 |
250 ≤ s<500 | 2%折扣 |
500 ≤ s<1000 | 5%折扣 |
1000 ≤ s<2000 | 8%折扣 |
2000 ≤ s<3000 | 8%折扣 |
3000 ≤ s | 8%折扣 |
#include <stdio.h>
int main()
{
double 总运费, 基本运费 = 10, 货物重量 = 10, 路程, 折扣 = 0.08;
int c;
printf("输入路程: ");
scanf_s("%lf", &路程);
c = (int)路程 / 250;
switch (c)
{
case 0:
折扣 = 0.00; break;
case 1:
折扣 = 0.02; break;
case 2:
case 3:
折扣 = 0.05; break;
}
总运费 = 基本运费 * 货物重量 * 路程 * (1 - 折扣);
printf("总运费为:%.2f\n", 总运费);
}
二.计算分段函数
![](https://i-blog.csdnimg.cn/blog_migrate/37474d0ba4416d96f8dc69c395720ddd.bmp)
#include <stdio.h>
#include <math.h>
int main()
{
double x, y;
int t;
scanf_s("%lf", &x);
t = (x < 2) + (x < 6) + (x < 10);
switch (t)
{
case 0: //(x<2)、(x<6)、(x<10)没有一个为真,即x>=10
y = 1 / (x + 1); break;
case 1: 仅(x<10)为真时
y = sqrt(x + 1); break;
case 2: //(x<6)、(x<10)为真时
y = x * x + 1; break;
case 3: //(x<2)、(x<6)、(x<10)全为真时
y = x; break;
}
printf("%.2f\n", y);
}
三.实践项目
1.设计一个投票表决器,其功能是:输入Y、y,打印agree;输入N、n,打印disagree;输入其他 ,打印lose
#include <stdio.h>
int main()
{
char c;
scanf_s("%c", &c, 1);
switch (c)
{
case 'Y':
case 'y':
printf("agree\n"); break;
case 'N':
case 'n':
printf("disagree\n"); break;
default:
printf("lose"); break;
}
}
2.给出百分制成绩,输出等级’A’,’B’,’C’,’D’,’E’,90以上为’A’,80-89为’B’,70-79为’C’,60-69为’D’,60以下为’E’。
#include <stdio.h>
int main()
{
int a,b;
scanf_s("%d", &a);
b = a / 10;
switch (b)
{
case 10:
case 9:
printf("A\n"); break;
case 8:
printf("B\n"); break;
case 7:
printf("C\n"); break;
case 6:
printf("D\n"); break;
default:
printf("E\n"); break;
}
}
3.用switch语句求分段函数值
方法一:构造表达式 t=(x<2) + (x<6) + (x<10)
#include <stdio.h>
#include <math.h>
int main()
{
double x, y;
int t;
scanf_s("%lf", &x);
t = (x < 2) + (x < 6) + (x < 10);
switch (t)
{
case 0: y = 1 / (x + 1); break;
case 1: y = sqrt(x + 1); break;
case 2: y = x * x + 1; break;
case 3: y = x; break;
}
printf("y=%.2f", y);
}
方法二:构造表达式 t= x/2;
#include <stdio.h>
#include <math.h>
int main()
{
double x, y;
int t;
scanf_s("%lf", &x);
t = (int)x/2;
if (t < 0) t = 0;
switch (t)
{
case 0: y = x; break;
case 1:
case 2: y = x * x + 1; break;
case 3:
case 4: y = sqrt(x + 1); break;
default: y = 1 / (x + 1); break;
}
printf("y=%.2f", y);
}
4.编程序,输入年份和月份,输出该月有多少天
#include <stdio.h>
int main()
{
int year, month, days=30;
printf("请输入年月: ");
scanf_s("%d %d", &year, &month);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31; break;
case 4:
case 6:
case 9:
case 11:
days = 30; break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
days = 29;
else days = 28; break;
}
printf("\n%d年%d月共有%d天\n", year, month, days);
}
5.利用switch计算分段函数的方法,计算个人所得税
#include <stdio.h>
int main()
{
double income, tax, s;
int t;
scanf_s("%lf", &income);
s = income - 3500;
t = (s < 0) + (s < 1500) + (s < 4500) + (s < 9000) + (s < 35000) + (s < 55000) + (s < 80000);
switch (t)
{
case 7: tax = 0; break;
case 6: tax = s * 0.03 - 0.00; break;
case 5: tax = s * 0.10 - 105; break;
case 4: tax = s * 0.20 - 555; break;
case 3: tax = s * 0.25 - 1005; break;
case 2: tax = s * 0.30 - 2755; break;
case 1: tax = s * 0.35 - 5505; break;
case 0: tax = s * 0.45 - 13505; break;
}
printf("个人所得税为%.2f\n", tax);
}