#include <stdio.h>
#include <math.h>
#define BASELINE 75.6
#define CONST1 0.11430
#define CONST2 0.12989
#define CONST3 0.22944
#define CONST4 0.32146
#define GAS_BASELINE 12.6
#define CONST5 1.13309
#define CONST6 1.35349
void getInput( long *preElecRead, long *currElecRead, long *preGasRead, long *currGasRead);
double calcOutput( double *kwhConsumed, double *thermsConsumed, double *elecCharges, double *gasCharges, double *totalDue,
long preElecRead, long currElecRead, long preGasRead, long currGasRead);
void printOutput(long preElecRead, long currElecRead, long preGasRead, long currGasRead,double kwhConsumed, double thermsConsumed,
double elecCharges, double gasCharges, double totalDue );
double calcKWH(long preElecRead, long currElecRead);
double calcGasTherms(long preGasRead, long currGasRead);
double calcElecCharges(double* kwhConsumed);
double calcGasCharges(double thermsConsumed);
double round100th(double n);
double calcTotalDue(double* gasCharges, double* elecCharges);
int main(void)
{ //Declaration
long preElecRead;
long currElecRead;
long preGasRead;
long currGasRead;
double kwhConsumed;
double thermsConsumed;
double elecCharges;
double gasCharges;
double totalDue;
//Statement
getInput(&preElecRead, &currElecRead, &preGasRead, &currGasRead);
calcOutput(&kwhConsumed, &thermsConsumed, &elecCharges, &gasCharges, &totalDue, preElecRead,
currElecRead, preGasRead, currGasRead);
printOutput(preElecRead, currElecRead, preGasRead, currGasRead, kwhConsumed, thermsConsumed, elecCharges, gasCharges,totalDue);
return 0;
} //end main
void getInput( long *preElecRead, long *currElecRead, long *preGasRead, long *currGasRead)
{
printf("Enter previous & current electric meter readings: ", preElecRead, currElecRead);
scanf("%ld %ld", preElecRead, currElecRead);
printf("Enter previous & current gas meter readings: ", preGasRead, currGasRead);
scanf("%ld %ld", preGasRead, currGasRead);
}
double calcOutput( double *kwhConsumed, double *thermsConsumed, double *elecCharges, double *gasCharges, double *totalDue,
long preElecRead, long currElecRead, long preGasRead, long currGasRead)
{
*kwhConsumed = calcKWH(preElecRead, currElecRead);
*thermsConsumed = calcGasTherms(preGasRead, currGasRead);
*elecCharges = calcElecCharges(kwhConsumed);
*gasCharges = calcGasCharges(*thermsConsumed);
*totalDue = calcTotalDue(gasCharges, elecCharges);
}
double calcKWH(long preElecRead, long currElecRead)
{
double kwh;
kwh = (double)currElecRead - (double)preElecRead;
return kwh;
}
double calcGasTherms(long preGasRead, long currGasRead)
{
double gas;
gas = (double)currGasRead - (double)preGasRead;
return gas;
}
double calcElecCharges(double* kwhConSumed)
{
double elecCharge ;
if (*kwhConSumed > 0 && *kwhConSumed <= BASELINE)
{
elecCharge = *kwhConSumed * CONST1;
}
else
if ( *kwhConSumed > BASELINE && *kwhConSumed <= (1.3*BASELINE))
{
elecCharge = *kwhConSumed * CONST2;
}
else
if ( *kwhConSumed > (1.3*BASELINE) && *kwhConSumed <= (2.0*BASELINE))
{
elecCharge = *kwhConSumed * CONST3;
}
else
if (*kwhConSumed > 2.0*BASELINE)
{
elecCharge = *kwhConSumed * CONST4;
}
return round100th(elecCharge);
}
double calcGasCharges(double thermsConsumed)
{
double gas;
// check if therms < 0 and <= baseline
if (thermsConsumed > 0 && thermsConsumed <= GAS_BASELINE) gas = thermsConsumed * CONST5;
else
if (thermsConsumed > GAS_BASELINE) gas = thermsConsumed * CONST6;
return round100th(gas);
}
double round100th(double n)
{
if(n > 0 )
{
n = (n + 50) / 100 * 100;
}
else
{
return n = 0;
}
return n;
}
double calcTotalDue(double* gasCharges, double* elecCharges)
{
double i;
i = (double)(*gasCharges)+ (double)(*elecCharges);// calc total
return i;
}
void printOutput(long preElecRead, long currElecRead, long preGasRead, long currGasRead,double kwhConsumed, double thermsConsumed,
double elecCharges, double gasCharges, double totalDue )
{
printf("Electric:\n");
printf("Previous: %ld, Current: %ld, KWH Used: %0.0lf, Charges: %.2lf\n", preElecRead, currElecRead, kwhConsumed, elecCharges);
printf("Gas:\n");
printf("Previous: %ld, Current: %ld, Therms Used: %0.0lf, Charges: %.2lf\n", preGasRead, currGasRead, thermsConsumed, gasCharges);
printf("Total Charges: %60.2lf", totalDue);
}
嗨我有这个代码的问题我完成了代码,但不知何故,我的电荷充电和充气不同于我教授使用的测试运行输出,我想我可能没有使用计算燃气费和电费的正确公式。任何人都可以告诉我正确的计算气体充电和电荷的公式
这是我的输出
Enter previous & current electric meter readings: 80000
80500
Enter previous & current gas meter readings: 990
1030
Electric:
Previous: 80000, Current: 80500, KWH Used: 500, Charges: 210.73
Gas:
Previous: 990, Current: 1030, Therms Used: 40, Charges: 104.14
Total Charges: 314.87
Process returned 0 (0x0) execution time : 21.512 s
Press any key to continue.
这是测试运行输出:
Enter previous & current electric meter readings: 80000
80500
Enter previous & current gas meter readings: 990
1030
Electric:
Previous: 80000, Current: 80500, KWH Used: 500, Charges: 135.78
Gas:
Previous: 990, Current: 1030, Therms Used: 40, Charges: 135.78
Total Charges: 271.56
正如您所看到的,使用相同的输入时,气体充电和电荷的输出都不同。我该如何解决这个问题?
答案 0 :(得分:1)
您不能指望有人知道您要使用的公式:)但我认为您的问题出在代码而不是公式中。试试这个:
double calcElecCharges(double* kwhConSumed)
{
double elecCharge ;
if (*kwhConSumed > 0 && *kwhConSumed <= BASELINE)
{
elecCharge = *kwhConSumed * CONST1;
}
else
{
if ( *kwhConSumed > BASELINE && *kwhConSumed <= (1.3*BASELINE))
{
elecCharge = *kwhConSumed * CONST2;
}
else
{
if ( *kwhConSumed > (1.3*BASELINE) && *kwhConSumed <= (2.0*BASELINE))
{
elecCharge = *kwhConSumed * CONST3;
}
else if (*kwhConSumed > 2.0*BASELINE)
{
elecCharge = *kwhConSumed * CONST4;
}
else
{
//*kwhConSumed < (1.3*BASELINE) || *kwhConSumed > (2.0*BASELINE)
//What now??
}
}
}
return round100th(elecCharge);
}
double calcGasCharges(double thermsConsumed)
{
double gas;
// check if therms < 0 and <= baseline
if (thermsConsumed > 0 && thermsConsumed <= GAS_BASELINE)
gas = thermsConsumed * CONST5;
else if (thermsConsumed > GAS_BASELINE)
gas = thermsConsumed * CONST6;
else
{
//thermsConsumed < 0
//What now??
}
return round100th(gas);
}
或者提供教授提供的公式,以便有人检查您的代码。
答案 1 :(得分:0)
首先是一些更高级别的评论。
double calcOutput(...);
不会返回任何内容,并且因为它接收指向它应该影响的所有变量的指针,所以它不需要返回任何内容。应该声明
void calcOutput(...);
接下来,
double calcElecCharges(double* kwhConsumed);
不会改变消耗的KWh数量,所以传递一个指向它的指针是没有意义的。其亲属calcGasCharges()
以普通double
为参数,calcElecCharges()
也是如此。
在getInput()
中,您将无关的参数传递给printf()
来电:
printf("Enter previous & current electric meter readings: ", preElecRead, currElecRead)
printf("Enter previous & current gas meter readings: ", preGasRead, currGasRead);
这没有实际的伤害,因为printf()
只是忽略格式字符串所需的参数,但它的样式很糟糕。
现在是一个实现错误,
double round100th(double n)
{
if(n > 0 )
{
n = (n + 50) / 100 * 100;
}
else
{
return n = 0;
}
return n;
}
几乎没有按照它在盖子上所说的那样做,它基本上将50加到正值并且为负值返回0。后者可能是有意的,但对于前者,我认为你想要将值四舍五入到小数点之后的两个位置,这将忽略浮点表示的更精细点 - 将通过乘以100来实现,舍入到最接近的整数并除以100,
if (n > 0) {
n = round(100.0*n)/100.0;
}
这几乎不会产生所需的输出,但不知道计算电荷的实际公式应该是什么,我们无法诊断出来。