Section 26 switch statement to solve the problem

1. Calculate freight

Total freight = basic freight × cargo weight × distance × (1-discount)

Distance km discount
0 ≤ s<250 No discount
250 ≤ s<500 2% discount
500 ≤ s<1000 5% discount
1000 ≤ s<2000 8% discount
2000 ≤ s<3000 8% discount
3000 ≤ s 8% discount
#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. Calculate the piecewise function

#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. Practical project
1. Design a voting device, its function is: input Y, y, print agreement; input N, n, print disagree; input other, print 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. Give the score in a percentile system, output grades'A','B','C','D','E', above 90 is'A', 80-89 is'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. Use the switch statement to find the value of the piecewise function

方法一:构造表达式 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. Program, enter the year and month, and output how many days there are in the month

#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 switch to calculate the piecewise function method to calculate personal income tax

#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);
}

Guess you like

Origin blog.csdn.net/m0_51439429/article/details/115016533