编程问题:

来源:百度知道 编辑:UC知道 时间:2024/06/23 09:10:44
编程问题:月用电量50千瓦时以内的,电价为0.35元/千瓦时;超出50千瓦时的电量,电价上调0.05元/千瓦时.编写程序,输入用户的月用电量(千瓦时),计算并输出该用户应支付的的电费(元).

给你看看 蛮简单的 能运行!汉字在编译器中是乱码,你可以改一下:
程序如下:
#include "stdio.h"

main ()
{ float a,s;

printf("输入用户的月用电量:");
scanf("%f",&a);
if(a<0)
printf("error!");
else if(a<=50)
{s=0.35*a;
printf("用户应支付电费为:");
printf("%f",s);}
else {s=50*0.35+(a-50)*(0.35+0.05);
printf("用户应支付电费为:");
printf("%f",s);}

}

float PowerFee(float power)
{
if(power > 0 && power <50){return 0.35*power};
if(power >= 50){return 0.35*power+0.05*(power-50)};
std::cout<<"用电量输入错误!"<<endl;
return 0;
}
大概意思就是这样,形参power是用户的月用电量。如果输入了小于0的数,会提示输入错误并返回0。

float compute(int used)
{
float money=0;
if(used<50)money=used*0.35;
else money=50*0.35+(used-50)*0.4;
return money;
}

fuction(in:float):float
begin<