0

输出显示全 0。总和OT不计算..

#include <stdio.h>


#define STD_HOURS 40.0
#define OT_RATE 1.5
#define SIZE 5


void read_hours(float worked_hours[], long int clockNumber[]);
void calculate_gross(float wage[], float worked_hours[], float gross);
void calculate_gross_ot(float wage[], float worked_hours[]);
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[],              float gross);

int i;



int main()
{

    long int clockNumber [SIZE] = {98401, 526488, 765349, 34645, 127615};/* employee ID */
    float gross = 0.0; /* gross */
    float OT [SIZE] = {}; /* overtime */
    float wage [SIZE] = {10.6, 9.75, 10.5, 12.25, 8.35};      /* hourly wage */
    float worked_hours [SIZE] = {};    //  Hours  worked


    read_hours(worked_hours, clockNumber);

    if (worked_hours[i]>STD_HOURS)
    {

    calculate_gross_ot(wage, worked_hours);

    }

    else

    {
       calculate_gross(wage,worked_hours, gross);

    }
    printFunction(clockNumber, wage, worked_hours, OT, gross);

    return(0);
}



void read_hours(float worked_hours[], long int clockNumber[])
{



    for (i=0; i<SIZE; i++)
    {

        printf("\n Enter Hours for Emlployee ID: %ld\n", clockNumber[i]);
        scanf ("%f", &worked_hours[i]);

    }


}

void calculate_gross(float wage[], float worked_hours[], float gross)
{


    for(i=0; i<SIZE; i++)

        gross=(wage[i]*worked_hours[i]);
}


void calculate_gross_ot(float wage[], float worked_hours[])
{


    float gross;
    float OT[SIZE];

    for (i=0; i<SIZE; i++)
    {



        /* calculating overtime hours */
           OT[i]=worked_hours[i]-STD_HOURS;

            /* calculating gross pay with overtime */
            gross = (STD_HOURS*wage[i]) + (OT[i]*OT_RATE*wage[i]);
        //}

    }

}

void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross)
    {


        /* creating a table for the output */
        printf("------------------------------------------------\n");
        printf("%7s","Clock#");
        printf("%7s","Wages");
        printf("%7s","Hours");
        printf("%7s","OT");
        printf("%7s\n","Gross");
        printf("------------------------------------------------\n");
for (i=0; i<SIZE; i++)

{
        /* printing the results */
        printf("%6ld", clockNumber[i]);
        printf("%10.2f",wage[i]);
        printf("%6.1f", worked_hours[i]);
        printf("%6.1f", OT[i]);
        printf("%10.2f",gross);
        printf("\n");

 }


    }

该程序用于计算加班时间和不加班时间的总和。输出显示总和 OT 的所有 0。请帮助找出错误在哪里。

4

2 回答 2

3

在第一种情况下,您是gross按值传递。

在第二种情况下,您根本没有传递它(该函数有一个名为 的本地函数gross)。

在这两种情况下,只要各自的函数发生变化gross,这种变化就不会传播给调用者。

您需要gross通过指针传递,或者使用return语句从函数返回值(并适当地更改返回类型)。

于 2013-10-13T18:41:59.590 回答
0

从传递OT给您的calculate_gross_ot()函数main()

 ...
 calculate_gross_ot(wage, worked_hours, OT);
 ...


 void calculate_gross_ot(float wage[], float worked_hours[], float OT[])
于 2013-10-13T18:45:47.407 回答