c语言电量的输入及输出电费的计算并输出
时间: 2023-05-25 13:03:57 浏览: 147
#include <stdio.h>
int main() {
float electricity, cost;
printf("请输入本月用电量(千瓦时):");
scanf("%f", &electricity);
if (electricity <= 0) {
printf("输入有误,请重新输入。\n");
return 0;
}
if (electricity <= 170) {
cost = electricity * 0.52;
} else if (electricity <= 370) {
cost = 170 * 0.52 + (electricity - 170) * 0.68;
} else if (electricity <= 570) {
cost = 170 * 0.52 + 200 * 0.68 + (electricity - 370) * 0.8;
} else {
cost = 170 * 0.52 + 200 * 0.68 + 200 * 0.8 + (electricity - 570) * 1.2;
}
printf("本月用电量为%.2f千瓦时,电费为%.2f元。\n", electricity, cost);
return 0;
}
相关问题
c语言编写智能家居系统电量的输入及相应电费的计算并输出
#include <stdio.h>
int main()
{
int power; // 定义电量变量
float cost; // 定义电费变量
printf("请输入电量(千瓦时):");
scanf("%d", &power);
/* 根据电量计算电费 */
if (power <= 50)
{
cost = power * 0.52;
}
else if (power <= 100)
{
cost = 50 * 0.52 + (power - 50) * 0.68;
}
else if (power <= 150)
{
cost = 50 * 0.52 + 50 * 0.68 + (power - 100) * 0.74;
}
else
{
cost = 50 * 0.52 + 50 * 0.68 + 50 * 0.74 + (power - 150) * 1.00;
}
printf("您的电费为%.2f元。\n", cost);
return 0;
}
智能家居系统电量的输入及相应电费的计算并输出的程序C语言
#include <stdio.h>
int main() {
int num_devices; // 设备数量
float total_power = 0; // 总电量
float electricity_rate; // 电费单价
float total_cost; // 总电费
printf("请输入设备数量:");
scanf("%d", &num_devices);
for (int i = 1; i <= num_devices; i++) {
float power;
printf("请输入第%d个设备的电量:", i);
scanf("%f", &power);
total_power += power;
}
printf("请输入电费单价(元/度):");
scanf("%f", &electricity_rate);
total_cost = total_power * electricity_rate;
printf("本月电费为%.2f元\n", total_cost);
return 0;
}