c语言switch语句程序题,C语言及程序设计初步例程-26 利用switch语句解决问题

应用:计算运费

每公里每吨货物的基本运费为p(price),货物重为w(weight),距离为s,折扣为d(discount),则总运费f(freight)的计算公式为 freight=price*weight*s*(1-discount)

#include

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 10:

case 11:

d=10;break;

case 12:

d=15;break;

}

f=p*w*s*(1-d/100.0);

printf("freight=%.2f\n", f);

return 0;

}

用switch求分段函数

#include

#include

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;

break;

case 2://(x<6)、(x<10)为真时

y=x*x+1;

break;

case 1://仅(x<10)为真时

y=sqrt(x+1);

break;

case 0: //(x<2)、(x<6)、(x<10)没有一个为真,即x>=10

y=1/(x+1);

}

printf("%lf\n", y);

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值