2014-04-02 176 views
-2

編譯此代碼後,總計和平均值部分的計算結果顯示的結果不同於以下所示。在這個問題中,我已經在下面展示了結果應該是什麼樣子。然而,如果你點擊提供給IDEone的鏈接,你可以看到我從我的代碼中得到的結果。我的問題是需要改變我的代碼才能達到正確的結果?C程序編譯問題


Name   Clock# Wage Hours  OT  Gross 
--------------------------------------------------------- 
Connie Cobol 098401 10.60 51.0 11.0 598.90 
Mary Apl  526488 9.75 42.5  2.5 426.56 
Frank Fortran 765349 10.50 37.0  0.0 388.50 
Jeff Ada  034645 12.25 45.0  5.0 581.88 
Anton Pascal 127615 10.00 40.0  0.0 400.00 
--------------------------------------------------------- 

Total:       215.5 18.5 2395.84 
Average:       43.1  3.7 479.168 

Link to Ideone Here

#include<stdio.h> 

/* Define Constants */ 
#define NUM_EMPL 5 
#define OVERTIME_RATE 1.5f 
#define STD_WORK_WEEK 40.0f 

struct employee 
{ 
char first_name[20]; 
char last_name[20]; 
long clock_number; 
float wage_rate; 
float hours; 
float OT; 
float gross; 
float total_hours; 
float avg_hours; 
float total_OT; 
float avg_OT; 
float total_gross; 
float avg_gross; 
}; 

/* define prototypes here for each function except main */ 
void Get_Input (struct employee [NUM_EMPL]); 
void Gross_pay_calc (struct employee [NUM_EMPL]); 
void Output_results_screen (struct employee [NUM_EMPL]); 
void Total_and_Avg (struct employee [NUM_EMPL]); 

/*************************************************************************/ 
/* Function: Output_results_screen */ 
/* */ 
/* Purpose: Outputs to screen in a table format the following */ 
/* information about an employee: Clock, Wage, */ 
/* Hours, Overtime, and Gross Pay. */ 
/* */ 
/* Parameters: employeeData - an array of structures containing */ 
/* employee information */ 
/* */ 
/* Returns: Nothing (void) */ 
/* */ 
/************************************************************************/ 

void Output_results_screen (struct employee employeeData[NUM_EMPL]) 
{ 
int idx; /* loop index */ 

     printf ("\n\tGeorge Smith, C Programming, Fifth Homework Assignment\n\n"); 
     printf ("\t----------------------------------------------------------\n"); 
     printf ("\tName   Clock# Wage Hours OT  Gross\n"); 
     printf ("\t----------------------------------------------------------\n"); 

/* printf information about each employee */ 
for (idx = 0; idx < NUM_EMPL ; ++idx) 
{ 
     printf("\t%s %s \t%06li \t%5.2f \t%4.1f \t%4.1f \t%6.2f \n",employeeData[idx].first_name, employeeData[idx].last_name, employeeData[idx].clock_number, employeeData[idx].wage_rate, employeeData[idx].hours,employeeData[idx].OT, employeeData[idx].gross); 
} /* for */ 
     printf ("\t----------------------------------------------------------\n"); 
     printf ("Total: \t\t\t\t\t%5.1f \t%5.1f \t%5.2f\n",employeeData[idx].total_hours, employeeData[idx].total_OT, employeeData[idx].total_gross); 
     printf ("Average: \t\t\t\t%5.1f \t%5.1f \t%5.3f", employeeData[idx].avg_hours, employeeData[idx].avg_OT, employeeData[idx].avg_gross); 
} /* Output_results_screen */ 

/*function for user input*/ 
void Get_Input (struct employee employeeData[NUM_EMPL]) 
{ 
    int idx; /* loop index */ 

    /* printf information about each employee */ 
    for (idx = 0; idx < NUM_EMPL ; ++idx) 
    { 
     printf("\nEnter hours worked for employee %06li : ",employeeData[idx].clock_number); 
     scanf("%f",&employeeData[idx].hours); 
    } 
} 
/*function to calculate overtime and gross pay*/ 
void Gross_pay_calc (struct employee employeeData[NUM_EMPL]) 
{ 
    int idx; /* loop index */ 

    /* printf information about each employee */ 
    for (idx = 0; idx < NUM_EMPL ; ++idx) 
    { 
     if (employeeData[idx].hours <= STD_WORK_WEEK) 
     { 
      employeeData[idx].OT = 0.0; 
      employeeData[idx].gross = employeeData[idx].wage_rate * employeeData[idx].hours; 
     } 

      else if (employeeData[idx].hours > STD_WORK_WEEK) 
      { 
      employeeData[idx].OT = employeeData[idx].hours - STD_WORK_WEEK; 
      employeeData[idx].gross = (STD_WORK_WEEK * employeeData[idx].wage_rate) + (employeeData[idx].OT * (OVERTIME_RATE * employeeData[idx].wage_rate)); 
      } 

    } 
} 

/*function to calculate overtime and gross pay*/ 
void Total_and_Avg (struct employee employeeData [NUM_EMPL]) 
{ 
int idx; /* loop index */ 
float total_hours=0; 
float avg_hours; 
float total_OT=0; 
float avg_OT; 
float total_gross=0; 
float avg_gross; 
/* printf information about each employee */ 
    for (idx = 0; idx < NUM_EMPL ; ++idx) 
{ 
    total_hours+= employeeData[idx].hours; 
    avg_hours= total_hours/employeeData[idx].hours; 
    total_OT+= employeeData[idx].OT; 
    avg_OT= total_OT/employeeData[idx].OT; 
    total_gross+= employeeData[idx].gross; 
    avg_gross= total_gross/employeeData[idx].gross; 
} 
} 

int main() 
{ 
/* Variable Declaration and initialization */ 
struct employee employeeData[NUM_EMPL] = { 
{"Connie", "Cobol", 98401, 10.60}, 
{"Mary", "Apl", 526488, 9.75}, 
{"Frank", "Fortran", 765349, 10.50}, 
{"Jeff", "Ada", 34645, 12.25}, 
{"Anton", "Pascal", 127615, 8.35} 
}; 

/* Call various functions needed to reading, calculating, and printing as needed */ 
Get_Input(employeeData); 
Gross_pay_calc(employeeData); 
/* Function call to output results to the screen in table format. */ 
Output_results_screen (employeeData); 
Total_and_Avg (employeeData); 
return(0); /* success */ 

}; /* main */ 
+6

1)你的標題不準確,2)你應該張貼代碼,而不是鏈接到它,3 )最重要的是,你應該從小而簡單的事情開始,並且不要只寫很多代碼,並且當它不起作用時會感到驚訝。 – Beta

+2

此外,這不是一個「編輯禍」,這是一個運行時的悲哀。 :) –

+1

歡迎來到本網站! @ Beta的評論是專注的;你應該簡化它,並使用更小的代碼塊。當你提出一個離散的問題時,堆棧溢出效果最好,而不是你不想要的那種代碼牆。 –

回答

0

爲什麼你:

float total_hours; 
float avg_hours; 
float total_OT; 
float avg_OT; 
float total_gross; 
float avg_gross; 

struct employee

如果我瞭解您的設計,那麼這些數字就是您只需要每個副本的一個副本,然後根據每個員工更新該單一數字集。

Total_And_Avg函數中的代碼根本沒有意義,因爲你只是更新你永遠不會返回的局部變量(所以它們會丟失)。

你的垃圾輸出來自行:

printf ("Total: \t\t\t\t\t%5.1f \t%5.1f \t%5.2f\n", 
    employeeData[idx].total_hours, [...] 

,因爲此時idx已經熄滅循環的結束,所以你從無效的內存讀取。

另外,您在致電Total_and_Avg之前致電Output_results_screen,所以即使您做對了,您仍然會得到垃圾。

您需要重新設計一個主要設計。我開始從struct employee中刪除這些虛假變量。那麼你將不得不對Total_and_Avg中的變量做些什麼。在該函數結束時輸出它們將是最容易的,將最後3行Output_results_screen移動到Total_and_Avg

如果要使用不同的函數來計算總數而不是顯示總計,則必須確保這些變量在兩者中均可見。一種方法是將這些變量放入另一個struct,您在main()中創建並通過參考Total_and_AvgOutput_results_screen

最後,您在Total_and_Avg函數中的計算是非常錯誤的。一旦您修復了程序結構,以便您可以正確顯示這些結果,那麼您可以再次嘗試使其正確。想想你如何計算紙上的總計和平均值,然後試着讓你的代碼反映出來。

0

藉此:它顯示了正確的結果,你的代碼是沒有錯的,你只是誤用的東西

#include<stdio.h> 

/* Define Constants */ 
#define NUM_EMPL 5 
#define OVERTIME_RATE 1.5f 
#define STD_WORK_WEEK 40.0f 

struct employee 
{ 
char first_name[20]; 
char last_name[20]; 
long clock_number; 
float wage_rate; 
float hours; 
float OT; 
float gross; 
//float total_hours; 
//float avg_hours; 
//float total_OT; 
//float avg_OT; 
//float total_gross; 
//float avg_gross; 
}; 

struct total 
{ 
float total_hours; 
float avg_hours; 
float total_OT; 
float avg_OT; 
float total_gross; 
float avg_gross; 
}total_calc; 

/* define prototypes here for each function except main */ 
void Get_Input (struct employee [NUM_EMPL]); 
void Gross_pay_calc (struct employee [NUM_EMPL]); 
void Output_results_screen (struct employee [NUM_EMPL]); 
void Total_and_Avg (struct employee [NUM_EMPL]); 

/*************************************************************************/ 
/* Function: Output_results_screen */ 
/* */ 
/* Purpose: Outputs to screen in a table format the following */ 
/* information about an employee: Clock, Wage, */ 
/* Hours, Overtime, and Gross Pay. */ 
/* */ 
/* Parameters: employeeData - an array of structures containing */ 
/* employee information */ 
/* */ 
/* Returns: Nothing (void) */ 
/* */ 
/************************************************************************/ 

void Output_results_screen (struct employee employeeData[NUM_EMPL]) 
{ 
int idx; /* loop index */ 

     //printf ("\n\tGeorge Smith, C Programming, Fifth Homework Assignment\n\n"); 
     printf ("\n\t----------------------------------------------------------\n"); 
     printf ("\tName   Clock# Wage Hours OT  Gross\n"); 
     printf ("\t----------------------------------------------------------\n"); 

/* printf information about each employee */ 
for (idx = 0; idx < NUM_EMPL ; ++idx) 
{ 
     printf("\t%s %s \t%06li \t%5.2f \t%4.1f \t%4.1f \t%6.2f \n",employeeData[idx].first_name, employeeData[idx].last_name, employeeData[idx].clock_number, employeeData[idx].wage_rate, employeeData[idx].hours,employeeData[idx].OT, employeeData[idx].gross); 
} /* for */ 
     printf ("\t----------------------------------------------------------\n"); 
     printf ("Total: \t\t\t\t\t%5.1f \t%5.1f \t%5.2f\n",total_calc.total_hours, total_calc.total_OT, total_calc.total_gross); 
     printf ("Average: \t\t\t\t%5.1f \t%5.1f \t%5.3f", total_calc.avg_hours, total_calc.avg_OT, total_calc.avg_gross); 
} /* Output_results_screen */ 

/*function for user input*/ 
void Get_Input (struct employee employeeData[NUM_EMPL]) 
{ 
    int idx; /* loop index */ 

    /* printf information about each employee */ 
    for (idx = 0; idx < NUM_EMPL ; ++idx) 
    { 
     printf("\nEnter hours worked for employee %06li : ",employeeData[idx].clock_number); 
     scanf("%f",&employeeData[idx].hours); 
    } 
} 
/*function to calculate overtime and gross pay*/ 
void Gross_pay_calc (struct employee employeeData[NUM_EMPL]) 
{ 
    int idx; /* loop index */ 

    /* printf information about each employee */ 
    for (idx = 0; idx < NUM_EMPL ; ++idx) 
    { 
     if (employeeData[idx].hours <= STD_WORK_WEEK) 
     { 
      employeeData[idx].OT = 0.0; 
      employeeData[idx].gross = employeeData[idx].wage_rate * employeeData[idx].hours; 
     } 

      else if (employeeData[idx].hours > STD_WORK_WEEK) 
      { 
      employeeData[idx].OT = employeeData[idx].hours - STD_WORK_WEEK; 
      employeeData[idx].gross = (STD_WORK_WEEK * employeeData[idx].wage_rate) + (employeeData[idx].OT * (OVERTIME_RATE * employeeData[idx].wage_rate)); 
      } 

    } 
} 

/*function to calculate overtime and gross pay*/ 
void Total_and_Avg (struct employee employeeData [NUM_EMPL]) 
{ 
int idx; /* loop index */ 
float total_hours=0; 
float avg_hours; 
float total_OT=0; 
float avg_OT; 
float total_gross=0; 
float avg_gross; 
/* printf information about each employee */ 
    for (idx = 0; idx < NUM_EMPL ; ++idx) 
{ 
    total_hours+= employeeData[idx].hours; 

    //avg_hours= total_hours/employeeData[idx].hours; 

    total_OT+= employeeData[idx].OT; 

    //avg_OT= total_OT/employeeData[idx].OT; 

    total_gross+= employeeData[idx].gross; 

    //avg_gross= total_gross/employeeData[idx].gross; 

} 
avg_hours = total_hours/NUM_EMPL; 
avg_OT = total_OT/NUM_EMPL; 
avg_gross = total_gross/NUM_EMPL; 

total_calc.total_hours = total_hours; 
total_calc.avg_hours = total_hours/NUM_EMPL; 
total_calc.total_OT = total_OT; 
total_calc.avg_OT = total_OT/NUM_EMPL; 
total_calc.total_gross = total_gross; 
total_calc.avg_gross = total_gross/NUM_EMPL; 
} 

int main() 
{ 
/* Variable Declaration and initialization */ 
struct employee employeeData[NUM_EMPL] = { 
{"Connie", "Cobol", 98401, 10.60}, 
{"Mary", "Apl", 526488, 9.75}, 
{"Frank", "Fortran", 765349, 10.50}, 
{"Jeff", "Ada", 34645, 12.25}, 
{"Anton", "Pascal", 127615, 10.0} // 8.35 
}; 
//total total_calc; 

/* Call various functions needed to reading, calculating, and printing as needed */ 
Get_Input(employeeData); 
Gross_pay_calc(employeeData); 
/* Function call to output results to the screen in table format. */ 
Total_and_Avg (employeeData); 
Output_results_screen (employeeData); 

return(0); /* success */ 

}; /* main */