【发布时间】: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