編譯此代碼後,總計和平均值部分的計算結果顯示的結果不同於以下所示。在這個問題中,我已經在下面展示了結果應該是什麼樣子。然而,如果你點擊提供給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
#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 */
1)你的標題不準確,2)你應該張貼代碼,而不是鏈接到它,3 )最重要的是,你應該從小而簡單的事情開始,並且不要只寫很多代碼,並且當它不起作用時會感到驚訝。 – Beta
此外,這不是一個「編輯禍」,這是一個運行時的悲哀。 :) –
歡迎來到本網站! @ Beta的評論是專注的;你應該簡化它,並使用更小的代碼塊。當你提出一個離散的問題時,堆棧溢出效果最好,而不是你不想要的那種代碼牆。 –