2013-03-25 3 views
-2

을 계산하기 위해 나는 다음과 같은 프로그램이 있습니다 gross_pay_calc 실행시C - 포인터와 함수를 참조하는 것은 여러 번

#include <stdio.h> 
#include <stdlib.h> /* for malloc */ 
#include <ctype.h> 

#define STD_WORK_WEEK 40 
#define OVERTIME_RATE 1.5 


struct employee 
{ 
char first_name[10]; 
char last_name[10]; 
long id_number; 
float wage; 
float hours; 
float overtime; 
float gross; 

struct employee *next; 
}; 

/************************************************************************/ 
/*      Function: Gross_pay_calc      */ 
/*                  */ 
/* Purpose: Calculates gross pay of an employee using hours and  */ 
/*    wage data that is already stored      */ 
/*                  */ 
/*                  */ 
/* Parameters: wage - Array of employee wages       */ 
/*    hours- Array of number of hours worked by an employee */ 
/*    grossPay- Gross Pay of an employee      */ 
/*    size-number of employees to process      */ 
/*                  */ 
/* Returns: Stores grossPay to be referenced in another function output */ 
/*                  */ 
/************************************************************************/ 

/* Function call to calculate gross pay. */ 
void Gross_pay_calc (struct employee *emp1) 
{ 
    /*Local Variable Declaration */ 
    struct employee *tmp; /* tmp pointer value to current node */ 



      if (emp1->hours <= STD_WORK_WEEK) /* Calculates grossPay based on <40 or >40 for OT */ 

      { 
      emp1->gross = emp1->wage*emp1->hours; 
      } 

      else 

      { 
      emp1->gross = (emp1->wage*STD_WORK_WEEK)+((emp1->hours-STD_WORK_WEEK)*  (emp1->wage*OVERTIME_RATE)); 
      } 

    } 

    /*-----------------------------------------------------------------------------*/ 
    /*                    */ 
    /* FUNCTION: print_list              */ 
    /*                    */ 
    /* DESCRIPTION: This function will print the contents of a linked    */ 
    /*    list. It will traverse the list from beginning to the  */ 
    /*    end, printing the contents at each node.      */ 
    /*                    */ 
    /* PARAMETERS: emp1 - pointer to a linked list        */ 
    /*                    */ 
    /* OUTPUTS:  None               */ 
    /*                    */ 
    /* CALLS:  None               */ 
    /*                    */ 
    /*-----------------------------------------------------------------------------*/ 
    void print_list(struct employee *emp1) 
    { 
    struct employee *tmp; /* tmp pointer value to current node */ 
    int i = 0;    /* counts the nodes printed   */ 

     printf ("--------------------------------------------------------------\n");  /*Print Header To Screen */ 
     printf ("  Name \t  Clock# \t Wage \t Hours \t OT \t Gross\n"); 
     printf ("---------------------------------------------------------------\n"); 

    /* Start a beginning of list and print out each value    */ 
    /* loop until tmp points to null (remember null is 0 or false)  */ 

    for(tmp = emp1; tmp ; tmp = tmp->next) 
    { 
     i++; 

     /* TODO - print other members as well */ 
     printf("\n %s %s %6d %8.2f %5.2f %7.2f\n",tmp->first_name,tmp->last_name,tmp- >id_number, 
                tmp->wage,tmp->hours,tmp->gross); 

    } 

    printf("\n\nTotal Number of Employees = %d\n", i); 

    } 

    /*----------------------------------------------------------------------------*/ 
    /*                   */ 
    /* FUNCTION: main               */ 
    /*                   */ 
    /* DESCRIPTION: This function will prompt the user for an employee   */ 
    /*    id and wage until the user indicates they are finished.  */ 
    /*    At that point, a list of id and wages will be    */ 
    /*    generated.             */ 
    /*                   */ 
    /* PARAMETERS: None               */ 
    /*                   */ 
    /* OUTPUTS:  None               */ 
    /*                   */ 
    /* CALLS:  print_list             */ 
    /*                   */ 
    /*----------------------------------------------------------------------------*/ 
    int main() 
    { 

    char answer[80];  /* to see if the user wants to add more employees */ 
    int more_data = 1; /* flag to check if another employee is to be processed */ 
     char value;    /* gets the first character of answer */ 

struct employee *current_ptr, /* pointer to current node */ 
       *head_ptr;  /* always points to first node */ 

    /* Set up storage for first node */ 
head_ptr = (struct employee *) malloc (sizeof(struct employee)); 
current_ptr = head_ptr; 

while (more_data) 
{ 
    /* Read in Employee ID and Hourly Wage */ 

    printf("\nEnter employee ID: "); 
    scanf("%li", & current_ptr -> id_number); 

    printf("\nEnter employee weekly wage: "); 
    scanf("%f", & current_ptr -> wage); 

    printf("\nEnter employee weekly hours: "); 
    scanf("%f", & current_ptr -> hours); 

    printf("\nEnter First Name: "); 
    scanf("%s", & current_ptr -> first_name); 

    printf("\nEnter Last Name: "); 
    scanf("%s", & current_ptr -> last_name); 

    printf("Would you like to add another employee? (y/n): "); 
    scanf("%s", answer); 

    Gross_pay_calc(head_ptr); 

    /* Ask user if they want to add another employee */ 
    if ((value = toupper(answer[0])) != 'Y') 
     { 
     current_ptr->next = (struct employee *) NULL; 
     more_data = 0; 
     } 
    else 
     { 
     /* set the next pointer of the current node to point to the new node */ 
     current_ptr->next = (struct employee *) malloc (sizeof(struct employee)); 
     /* move the current node pointer to the new node */ 
     current_ptr = current_ptr->next; 
     } 
} /* while */ 

/* print out listing of all employee id's and wages that were entered */ 


print_list(head_ptr); 

    printf("\n\nEnd of program\n"); 

return 0; 
    } 

지금, 그것은 첫 번째 직원 총 급여를 계산됩니다. 프로그램에 추가하기로 결정한 모든 직원의 gross_pay를 어떻게 계속 계산합니까?

+0

이 코드는 어디에서 찾을 수 있습니까? ** –

+0

먼저 들여 쓰기를 수정하십시오. – kjprice

+0

링크 된 목록의 마지막 노드를 null로 설정합니다. 그렇다면 다음과 같이 할 수 있습니다 : for (curr = head; curr! = null; curr ++)' – kjprice

답변

1

지금 코드는 머리돈 만 검사하고 머리는 변하지 않습니다.

간단한 해결책은 현재 직원에게 gross_pay_calc를 실행하는 것입니다.

그러나 코드를 다시 구성해야하지만 더 나은 방법이라고 생각합니다. 마지막에 새 노드를 추가하는 대신 링크 된 목록 (기본적으로 여기있는 내용)을 작성하는 것이 좋습니다. 변경을 반영하기 위해 머리를 시작하고 업데이트합니다.

또한 더 많은 도우미 기능을 만드는 습관을 갖습니다. 코드를 읽고 디버그하는 것이 훨씬 쉬워집니다.