1. Calcular el flete
Carga total = carga básica × peso de la carga × distancia × (1 descuento)
Distancia km | descuento |
---|---|
0 ≤ s < 250 | Sin descuento |
250 ≤ s < 500 | 2% de descuento |
500 ≤ s < 1000 | 5% de descuento |
1000 ≤ s < 2000 | 8% de descuento |
2000 ≤ s < 3000 | 8% de descuento |
3000 ≤ s | 8% de descuento |
#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", 总运费);
}
2. Calcule la función por partes
#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);
}
3. Proyecto práctico
1. Diseñar un dispositivo de votación, su función es: ingresar Y, y, imprimir acuerdo; ingresar N, n, imprimir en desacuerdo; ingresar otro, imprimir perder
#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. Dé la puntuación en un sistema de percentiles, califique las calificaciones'A ',' B ',' C ',' D ',' E ', por encima de 90 es'A', 80-89 es 'B', 70-79 is'C '', 60-69 is'D ', 60 and below is'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. Utilice la instrucción switch para encontrar el valor de la función por partes
方法一:构造表达式 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. Programe, ingrese el año y el mes, y muestre cuántos días hay en el mes
#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. Use el interruptor para calcular el método de función por partes para calcular el impuesto sobre la renta personal
#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);
}