He teacher teaching link C language and program design preliminary This lesson explains
Application: Calculate freight
The basic freight rates per ton of cargo per kilometer are p (price), the goods are w (weight), the distance is s, the discount is D (discount), then the total freight f (freight) is calculated as freight=price*weight*s* (1-discount )
#include <stdio.h>
int main ()
{
int c,s;
float p,w,d,f;
printf ("Please enter p,w,s:");
scanf ("%f%f%d", &p, &w, &s);
if (s>=3000)
c=12;
else
c=s/250;
Switch (c)
{case
0:
d=0;break;
Case 1:
d=2;break;
Case 2: Case
3:
d=5;break;
Case 4: Case
5: Case
6: Case
7:
d=8;break;
Case 8: Case 9: Case:
d=10;break;
Case:
d=15;break;
f=p*w*s* (1-d/100.0);
printf ("freight=%.2f\n", f);
return 0;
}
Using switch to find piecewise functions
#include <stdio.h>
#include <math.h>
int main ()
{
double x, y;
int t;
scanf ("%lf", &x);
T= (x<2) + (x<6) + (x<10);
Switch (t)
{
Case 3://(X<2), (x<6), (x<10) y=x when all is true
;
break;
Case 2://(x<6), (x<10) is true
y=x*x+1;
break;
Case 1://only (x<10) is true when
y=sqrt (x+1);
break;
Case 0: //(X<2), (x<6), (X<10) No one is true, that is, x>=10
y=1/(x+1);
printf ("%lf\n", y);
return 0;
}