用c语言练习1.编写程序,输入上网的时间计算上网费用,计算的方法如下: 【25元 <10小时 费用=每小时2.5元 10~50小时 [每小时2元 ≥50小时 同时,为了鼓励多上网,每月收费最多不超过130元。 要求:使用switch语句编程
时间: 2023-05-12 09:06:08 浏览: 240
以下是用C语言编写的计算上网费用的程序,使用了switch语句:
```
#include <stdio.h>
int main() {
int hours;
float cost;
printf("请输入上网时间(小时):");
scanf("%d", &hours);
switch (hours) {
case 0:
cost = 0;
break;
case 1 ... 9:
cost = hours * 2.5;
break;
case 10 ... 49:
cost = hours * 2;
break;
default:
cost = 50 * 2 + (hours - 50) * 1.5;
break;
}
if (cost > 130) {
cost = 130;
}
printf("上网费用为:%.2f元\n", cost);
return 0;
}
```
这个程序会根据输入的上网时间计算出上网费用,并且会根据每月最高收费130元的规定进行限制。