用c语言编写程序神州行(Easyown)用户无月租费,话费每分钟0.6元,全球通(GSM)用户月租费50元,话费每分钟0. 4元。输入一个月的通话时间,分别计算出两种方式的费用,并判断哪一种方式较便宜
时间: 2024-03-01 21:54:57 浏览: 51
好的,以下是用 C 语言编写的程序,可以实现你的需求:
```c
#include <stdio.h>
int main() {
int time; // 通话时间
float cost1, cost2; // 费用
printf("请输入一个月的通话时间(分钟):");
scanf("%d", &time);
// 神州行用户
cost1 = time * 0.6;
// 全球通用户
cost2 = time * 0.4 + 50;
printf("神州行用户费用为:%.2f元\n", cost1);
printf("全球通用户费用为:%.2f元\n", cost2);
// 判断哪种方式更便宜
if (cost1 < cost2) {
printf("神州行用户更便宜!\n");
} else {
printf("全球通用户更便宜!\n");
}
return 0;
}
```
在程序中,我们定义了 `time` 变量来存储用户输入的通话时间,然后根据题目给出的计费公式来计算出两种方式的费用,并使用 `printf` 函数输出结果。最后,使用 `if...else` 语句来判断哪种方式更便宜。
相关问题
编写c语言】神州行(Easyown)用户无月租费,话费每分钟0.6元,全球通(GSM)用户月租费50元,话费每分钟0. 4元。输入一个月的通话时间,分别计算出两种方式的费用,并判断哪一种方式较便宜。 【输入形式】“%f” 【输出形式】字符串“Easyown” 、 “GSM”或 “equal”
好的,以下是代码实现:
```c
#include <stdio.h>
int main()
{
float time, cost1, cost2;
scanf("%f", &time);
cost1 = time * 0.6; // 神州行的费用
cost2 = time * 0.4 + 50; // 全球通的费用
if (cost1 < cost2)
printf("Easyown");
else if (cost1 > cost2)
printf("GSM");
else
printf("equal");
return 0;
}
```
程序中,先用 `scanf()` 函数获取用户输入的通话时长,然后根据题目给出的费用公式计算出两种方式的费用,最后使用 `if` 语句比较两种费用的大小并输出结果。
改错int time,c1,c2; //定义变量类型 scanf("%d",&time); //双引号里可以是其他数据类型,&符号一定不能忘 c1=0.6*time; //第一种计费 c2=0.4*time+50 ; //第二种计费 if(c1>c2) printf("GSM"); else if (c1==c2 ) printf("equal"); else (c1<c2); printf("Easyown") ; return 0; }
将第三个判断条件改为 `else if (c1<c2)`,因为 `else` 本身就是一个没有条件的默认情况,不需要再加判断条件。同时,由于 `c1` 和 `c2` 都是整型变量,所以在计算 `c1=0.6*time` 和 `c2=0.4*time+50` 时需要注意数据类型转换,可以将其中一个常量改为浮点型,例如 `c1=0.6*time` 改为 `c1=0.6f*time`。修改后的代码如下:
```
int time, c1, c2;
scanf("%d", &time);
c1 = 0.6f * time;
c2 = 0.4f * time + 50;
if (c1 > c2)
printf("GSM");
else if (c1 == c2)
printf("equal");
else if (c1 < c2)
printf("Easyown");
return 0;
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)