1
#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

如您所见,气体电荷和电荷的输出在使用相同输入时是不同的。我怎样才能解决这个问题?

4

2 回答 2

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);

}

或者提供您从教授那里获得的公式,以便有人可以检查您的代码。

于 2012-11-05T10:35:11.727 回答
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;
}

这仍然几乎不能产生所需的输出,但在不知道计算费用的实际公式应该是什么的情况下,我们无法诊断。

于 2012-11-05T15:55:44.977 回答