#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

正如你所看到的,在使用相同的输入时,气体电荷和电荷的输出是不同的。我该怎么解决?

最佳答案

你不能指望有人知道你想使用什么公式:)但我认为你的问题在于代码而不是公式。试试这个:

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

}

或者提供你从教授那里得到的公式以便有人检查你的代码。

09-15 15:09