【问题标题】:C Program Displays multiple printsC 程序显示多个打印
【发布时间】:2020-07-22 14:52:15
【问题描述】:

我写了一个 c 代码来计算电费 它给出了我期望的输出,但它重复输出太多我试图调试它但似乎不知道我在哪里弄错了有人可以查看我的代码吗? 我只希望第一个输出可见,而不是所有这些结果

#include <stdio.h>
#include <stdlib.h>

int main(){
    GetInputs();
    CalConsumCharge();
    CalExtraCharges();
    DispBill();
}

void GetInputs(){
    int unittot;
    int unit1;
    int Prev_Cons;
    printf("Enter Previous units consumption: ");
    scanf("%d",&Prev_Cons);
    printf("Enter the units consumed: ");
    scanf("%d",&unittot);
    unit1 = unittot - Prev_Cons;
    CalConsumCharge(unit1);
}

void CalConsumCharge(int unit){
    int total = 0;
    if(unit<=50){
        total = (unit*22);
    }
    else if(unit<=100){
        total = ((50*22)+(unit-50)*30);
    }
    else if(unit<=200){
        total = ((50*22)+(50*30)+(unit-100)*36);
    }
    else if(unit<=350){
        total = ((50*22)+(50*30)+(100*36)+(unit-200)*70);
    }
    else if(unit<=650){
        total = ((50*22)+(50*30)+(100*36)+(150*70)+(unit-350)*90);
    }
    else if(unit<=1000){
        total = ((50*22)+(50*30)+(100*36)+(150*70)+(300*90)+((unit-650)*135));
    }
    else{
        total = ((unit*145));
    }

    CalExtraCharges(total);
}

void CalExtraCharges(int total){
    int total1;
    total = total/100;
    if(total>=2000){
        total1 = total + 10 +20;
    }
    else{
        total1 = total +10;
    }
    DispBill(total1);
}

void DispBill(int total1){
    if(total1>=2000){
        printf("A penalty Charge was Placed Because your bill is over 2000 L.E");
        printf("Bill amount is: %d L.E",total1);
    }
    else if(total1<2000) {
        printf("your bill is : %d L.E",total1);
    }
}

【问题讨论】:

  • 欢迎来到 SO!这看起来无效,因为CalConsumCharge() 没有参数,但基本上在main 中调用GetInputs 会启动一个调用所有其他函数的链,因此您不需要在@ 中再次显式调用它们987654325@。但是,链接所有函数是糟糕的设计,因此请考虑将值返回给 main,以便每个函数都可以与其他函数解耦。换句话说,有时你可能想在不计算任何东西的情况下获得输入,有时你想在不显示账单的情况下计算东西。但目前的设计无法支持该用例。

标签: c variables debugging output


【解决方案1】:

你应该编译你的代码,然后看看会发生什么(错误、警告)。

首先,您忘记声明函数。在执行 main 函数之前添加以下声明。

void GetInputs();
void CalConsumCharge(int unit);
void CalExtraCharges(int total);
void DispBill(int total1);

其次,因为你在GetInputs中调用CalConsumCharge,在CalConsumCharge中调用CalExtraCharges等等,所以你只要在main函数中调用GetInputs就够了。

int main(){
    GetInputs();
}

如果你想在主函数中调用许多函数作为你的代码,你应该在每个函数的末尾返回值,如下所示:

所有函数的声明:

int GetInputs();
int CalConsumCharge(int unit);
int CalExtraCharges(int total);
void DispBill(int total1);

对每个函数进行一点修正

int GetInputs() {
   ...
   unit1 = unittot - Prev_Cons;
   return unit1;
}
int CalConsumCharge(int unit) {
    ...
    else{
        total = ((unit*145));
    }
    return total;
}
int CalExtraCharges(int total) {
   ...
   else{
        total1 = total +10;
    }
    return total1;
}

然后,在主函数中:

int main () {
   int unit1 = GetInputs();
   int total = CalConsumCharge(unit1);
   int total1 = CalExtraCharges(total);
   DisBill(total1);

   return 0;
}

【讨论】:

    【解决方案2】:

    好的,这段代码需要很多的爱和关注。我很惊讶你可以编译并运行它。您的主函数只需要调用 GetInputs(),因为它会调用所有其他函数。

    我重构了主要代码和其余代码以保持整洁。我没有改变任何逻辑,它运行良好。看看:

    #include <stdlib.h>
    #include <stdio.h>
    
    void DispBill(int total1)
    {
    
        if(total1>=2000)
        {
            printf("A penalty Charge was Placed Because your bill is over 2000 L.E");
            printf("Bill amount is: %d L.E",total1);
        }
        else if(total1<2000) 
        {
            printf("your bill is : %d L.E",total1);
        }
    }
    
    void CalExtraCharges(int total)
    {
        int total1;
        total = total/100;
    
        if(total>=2000)
        {
            total1 = total + 10 +20;
        }
        else
        {
            total1 = total +10;
        }
    
        DispBill(total1);
    }
    
    void CalConsumCharge(int unit)
    {
        int total = 0;
    
        if(unit<=50)
        {
            total = (unit*22);
        }
        else if(unit<=100)
        {
            total = ((50*22)+(unit-50)*30);
        }
        else if(unit<=200)
        {
            total = ((50*22)+(50*30)+(unit-100)*36);
        }
        else if(unit<=350)
        {
            total = ((50*22)+(50*30)+(100*36)+(unit-200)*70);
        }
        else if(unit<=650)
        {
            total = ((50*22)+(50*30)+(100*36)+(150*70)+(unit-350)*90);
        }
        else if(unit<=1000)
        {
            total = ((50*22)+(50*30)+(100*36)+(150*70)+(300*90)+((unit-650)*135));
        }
        else
        {
        total = ((unit*145));
        }
    
        CalExtraCharges(total);
    }
    
    void GetInputs(void)
    {
        int unitTotal;
        int unitOne;
        int Prev_Cons;
    
        printf("Enter Previous units consumption: ");
        scanf("%d",&Prev_Cons);
    
        printf("Enter the units consumed: ");
        scanf("%d",&unitTotal);
    
        unitOne = unitTotal - Prev_Cons;
    
        CalConsumCharge(unitOne);
    }
    
    int main()
    {
        GetInputs();
    }
    

    这是我的输出:

    【讨论】:

    • 所以只调用一次函数很重要?
    • 我注意到你只从 main 中删除了函数,只留下了 GetInputs 函数,对吗?
    • 正确!您只想调用一次 GetInputs。因为它调用了所有其他函数。如果你多次调用它,你会得到错误的输出。
    • 嘿,请问你为什么在“void GetInputs(void)”中再次输入void,为什么不是“void GetInputs()”??
    • GetInputs 函数不接受任何输入参数。我想清楚地指出,在函数声明/定义中使用 void。这样就可以 100% 清楚地知道函数的输入和返回是什么。
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多