第二和第三函数中的计算无法正常工作

时间:2018-10-06 16:32:41

标签: c function

问题出在第二个和第三个函数中,其中“ gross_pay”数组被调用来起作用。尽我所能想到的一切,我似乎都无法获得“总薪资”。

我以为我可能已经传递了太多的数组来起作用,但是看来需要它们来完成我的数据。每次我编辑代码时,都会破坏所有进度。如果我不传递给函数,则可以正确完成所有计算,但是需要在此代码中引入几个函数。

任何输入或建议都会受到赞赏。

#include <stdio.h>

/* constants */
#define STD_HOURS 40.0 /* hours per week */
#define SIZE 5 /* employees to process */
#define OT 1.5 /* for overtime calculation */
#define AVERAGE 5 /* division by 5 for obtaining averages */

/* function to obtain hours worked for employees */
void getHours(long int clock_id[], float hours_worked[]) {
  int count;
  for (count = 0; count < SIZE; count++) {
    printf("Enter the number of hours worked for employee #%li: ", clock_id[count]);
    scanf("%f", &hours_worked[count]);
  }
} /* end function */

/* function call to calculate overtime hours */
void overtime_grosspay(float overtime_hours[], float hours_worked[], float gross[]) {
  float hourly_Wage[0]; /* initialize array */
  int count;
  for (count = 0; count < SIZE; count++)
    if (hours_worked[count] > STD_HOURS) /* calculation for gross pay to include overtime */ {
      overtime_hours[count] = hours_worked[count] - STD_HOURS;
      gross[count] = hourly_Wage[count] * STD_HOURS + (hourly_Wage[count]
        * overtime_hours[count] * OT);
    }
} /* end function */

/* function call to calculate gross pay */
void regular_grosspay(float overtime_hours[], float hours_worked[], float gross[]) {
  float Hourly_wage[0]; /* initialize array */
  /*float Overtime_hours[0];  array for overtime pay */
  /*float gross_pay[0];   array for gross pay per employee */
  int count;
  for (count = 0; count < SIZE; count++)
    if (hours_worked[count] <= STD_HOURS) {
      overtime_hours[count] = 0;
      gross[count] = hours_worked[count] * Hourly_wage[count];
    }
} /* end function */

int main(void) {
  long int clock_number[SIZE] = {
    98401,
    526488,
    765349,
    34645,
    127615
  }; /* initialize array */
  float hourly_wage[SIZE] = {
    10.6,
    9.75,
    10.5,
    12.25,
    8.35
  }; /* initialize array */
  int count = 0; /* loop index */
  /* float d = 0.0; */
  float hours_worked[SIZE]; /* array for hours worked */
  float overtime_hours[SIZE]; /* array for overtime pay */
  float gross_pay[SIZE]; /* array for gross pay per employee */ #
#if 0
  float sum_of_wages = 0; /* total employee wages */
  float average_wage = 0; /* average of employee wages */
  float sum_of_hours = 0; /* total employee hours */
  float average_hours_worked = 0; /* average employee hours */
  float total_OT = 0; /* sum of employee overtime */
  float average_of_OT = 0; /* average of overtime */
  float total_gross = 0; /* sum of employee gross pay */
  float average_total_gross = 0; /* average of employee gross pay */ #
#endif

  /* Welcome message to user */
  printf("* * * Welcome to the Pay Calculator * * *\n\n");
  printf("This program calculates the gross pay for a set of 5 employees.\n\n");

  /* function to obtain employee hours */
  getHours(clock_number, hours_worked);

  /* function call to calculate overtime hours */
  overtime_grosspay(overtime_hours, hours_worked, gross_pay);

  /* function call to calculate gross pay without overtime */
  regular_grosspay(overtime_hours, hours_worked, gross_pay);

  {
    printf("\n\n-------------------------------------------------\n");
    printf("Clock#    Wage   Hours    OT    Gross\n");
    printf("-------------------------------------------------\n");
  }

  /* begin loop to display tablulated data */
  for (count = 0; count < SIZE; count++) {
    printf("%06li   %5.2f   %5.1f %5.1f  %7.2f\n", clock_number[count], hourly_wage[count], hours_worked[count], overtime_hours[count], gross_pay[count]);
  } /* end for loop */

  printf("\nThank you for using the Pay Calculator. Good bye!\n\n");

  return 0;
} /* end main */

1 个答案:

答案 0 :(得分:1)

此行:

float hourly_Wage[0];  /* initialize array */

创建一个大小为0的数组。您可能想要:

float hourly_Wage[SIZE];  /* initialize array */

如果要在此处定义。但是,那么您就没有价值了。使用模型来传入和传出数组,您可能想要传递 hourly_wage在您致电overtime_grosspay()时,我已经修复了我的代码副本,现在看来该代码可以正常工作了。


还有一些错别字,可能是您粘贴到StackOverflow中时出现的:

此行无法正确关闭评论:

if (hours_worked[count] > STD_HOURS) /* calculation for gross pay        to include overtime *`

此行末尾有一个坏字符:

printf("Enter the number of hours worked for employee #%li: ",      clock_id[count]);`

我将为您在问题中对其进行编辑。