【问题标题】:Passing Array of Structure to Functions将结构数组传递给函数
【发布时间】:2014-06-29 11:27:51
【问题描述】:

这是我第一次发帖,因为我非常忙于这个编程任务。这一章我们开始学习结构。过去两周我们学习了数组和函数,因此作业试图将它们联系在一起。我想要做的是将这个结构数组作为引用而不是元素传递给函数。我的思考过程是这些函数中的大多数都没有返回任何内容,所以我希望在其中更新我的数组。我的问题是,我不断收到“getInput 的参数 1 的类型不兼容”的错误。

据我所知,我的语法是正确的。我概述了主函数之外的结构,我定义了一个 emp 变量,指定了一个从常量派生的数组等。我的函数应该没问题。该程序在我上次分配时使用了单个数组,这一次我删除了数组并替换了一个结构。我完全迷路了吗?我只是想克服第一个错误,如果有人能指出我正确的方向吗?

#include <stdio.h>

//Structures
struct employee {

    long int id_number;
    float wage;
    float hours;
    float overtime;
    float gross;

};

//Constants
#define NUM_EMPL 5
#define OVERTIME_RATE 1.5f
#define WORK_WEEK 40.0f

//Prototypes
void getInput (struct employee input, int size);
float overtime (struct employee input);
void gross_pay (struct employee input, float work, float ot_rate, int size);
void output (struct employee input, int size);

int main ()  {

    struct employee emp[NUM_EMPL] = {

                                {98401, 10.60},
                                {526488, 9.75},
                                {765349, 10.50},
                                {34645, 12.25},
                                {127615, 8.35}
    };


    int count;

    getInput (emp, NUM_EMPL);

    for (count = 0; count < NUM_EMPL; count++)  {
        //if overtime rate applies, function 'overtime' is called
        if (emp.hours[count] > WORK_WEEK)  {
            //function overtime takes specific elements of array and two constants as arguments
            emp.overtime[count] = overtime(emp, WORK_WEEK);
        }
        //if above does not apply, ot hours are set to 0
        else  {
            emp.overtime[count] = 0.0;
        }
    }
    //function called to calculate gross for each employee
    gross_pay (emp, WORK_WEEK, OVERTIME_RATE, NUM_EMPL);

    //function called to display output with proper header and formatting
    output(emp, NUM_EMPL);

    return (0);


}



//**************************************************************/
// Function: getInput
//
// Purpose: Obtains input from user, the number of hours worked
// per employee and stores the results in an array that is
// passed back to the calling program by reference.
//
// Parameters: emp_num - Array of employee clock numbers.
// hrs - Array of number of hours worked by an employee
// size - Number of employees to process */
//
// Returns: Nothing (call by refernce)
//
//**************************************************************/

void getInput (struct employee input, int size)    //(long emp_num[], float emp_hrs[], int size)
{

    int count; /* loop counter */

    /* Get Hours for each employee */
    for (count = 0; count < size; ++count)
    {
        printf("\nEnter hours worked by emp # %06li: ", input.id_number[count]);
        scanf ("%f", &input.hours[count]);
    }

    printf("\n\n");
}

//**************************************************************/
// Function: overtime
//
// Purpose: Figures out amount of overtime hours worked by
//subtracting 'work' from emp hrs.
//
// Parameters: emp_hrs - Array of hours worked by employees
// work - standard hours of work before OT

// Returns: float ot_hours
//
//**************************************************************/


float overtime (struct employee input, int size)                        //(float emp_hrs, float work)
{
    return (input.hours - work);
}

//**************************************************************/
// Function: gross_pay
//
// Purpose: Function that determines gross pay of each employee by multiplying ot rate by wage rate by hours worked
//and adding this amount to work times wage rate.
//
// Parameters: emp_hrs - Array of hours worked by employee
// wage_rate - Array of hourly wages for employees
// ot_hours - overtime hours if worked
// work - standard hours of work before OT
// ot_rate - time and a half constant
// size - amount of employees to be tested
//
// Returns: float gross to array of gross
//
//**************************************************************/
void gross_pay (struct employee input, float work, float ot_rate, int size)              //(float emp_hrs[], float gross[], float wage_rate[], float ot_hours[], float work, float        ot_rate, int size)
{
    int count; //loop counter


    for (count = 0; count < size; count++)  {
        input.gross[count] = (ot_rate * input.wage[count] * input.overtime[count]) + ((input.hours[count] - input.overtime[count]) * input.wage[count]);

    }
}

//**************************************************************/
// Function: output
//
// Purpose: Displays output of calculations in a neat,
// table.  Incorporates suppressed 0's and proper line allignment.
//
// Parameters: clock_number - array of each employee
// wage_rate - hourly wages for employees
// hours - hours worked by employees
// ot - overtime hours worked by employees
// gross - gross amount made by each employee
//
// Returns: Nothing: Displays printf within function
//
//**************************************************************/

void output (struct employee input, int size)   //(long clock_number[], float wage_rate[], float hours[], float ot[], float gross[], int size)
{

    int counter;

    printf("\n\n####################################################################\n");
    printf("##RESULTS##RESULTS##RESULTS##RESULTS##RESULTS##RESULTS##RESULTS#####\n");
    printf("####################################################################\n\n");

    printf("-------------------------------------------------------\n");
    printf("        Clock Number    Wage    Hours   OT      Gross\n");
    printf("-------------------------------------------------------\n");

    //employee data is displayed in a proper format
    for (counter = 0; counter < size; counter++)  {

        printf("    %06li       %-4.2f  %-4.2f  %-2.2f  %-5.2f\n", input.id_number[counter], input.wage[counter], input.hours[counter], input.overtime[counter], input.gross[counter]);
    }
}

【问题讨论】:

  • gross_pay 被声明为只接受一个struct employee,而不是一个数组。
  • 例如void getInput (struct employee input, int size) --> void getInput (struct employee *input, int size) and input[count].id_number and &amp;input[count].hours
  • 啊,我想我明白你的意思了。我所做的是在 get_input 函数的原型和函数定义中添加了空括号,它不再显示该消息。我相信我的语法是问题所在。谢谢。
  • BLUEPIXY,这和指针有关系吗?星号和&符号是什么?我们在本书中还没有走得那么远,到目前为止,我的所有示例都显示了通过引用将结构数组传递给函数的能力,而不是使用这两个符号。不过我想我们会在下周开始讨论。
  • 除了答案之外:overtime 中没有可见的work,因此无法编译。请注意,结构是按值传递的,因此您的 getInput 在本地副本上工作,因此输入将在返回后被丢弃。

标签: c arrays function structure


【解决方案1】:

在声明和定义中这样做

void getInput (struct employee input[], int size);
float overtime (struct employee input[],int size);
void gross_pay (struct employee input[], float work, float ot_rate, int size);
void output (struct employee input[], int size);

你的函数overtime()的声明和定义不一样。它们包含不同的参数。你也应该修复它。

【讨论】:

  • 感谢阿尤什的帮助。我确实知道我的问题出在哪里。我忘记了声明和定义中的括号。另外,我检查了超时功能并修复了参数。我的错误是更改变量的名称而不是仔细检查它们。感谢您的帮助。
【解决方案2】:

您的函数被定义为对单个员工进行操作,而不是对他们的数组进行操作。我将展示如何修复 gross_pay,您需要对其他函数进行类似的修复。

void gross_pay (struct employee input[], float work, float ot_rate, int size)              
{
    int count; //loop counter
    for (count = 0; count < size; count++)  {
        input[count].gross = (ot_rate * input[count].wage * input[count].overtime) + ((input[count].hours - input[count].overtime) * input[count].wage);

    }
}

访问结构成员时,它是input[count].member,而不是input.member[count]——如果成员是数组,而不是数组中结构的元素,则使用后者。

【讨论】:

  • 啊,我明白了。我错误地试图循环遍历结构变量的一个元素的同一成员,而不是循环遍历数组的元素。谢谢马萨诸塞州同胞。我想我需要更多地阅读书籍并密切关注我的语法。
猜你喜欢
  • 2015-02-24
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2019-06-22
  • 2019-05-03
相关资源
最近更新 更多