我必须编写一个程序来计算10名员工的总薪酬,扣除额,净工资,并包括加班费(如果适用)。我必须使用一个结构来执行此操作,名称最多可以包含20个字符,ID为6个字符。我知道我的主要问题是我如何在我的结构中使用数组,因为我只有一名员工才能正常工作。也许我只是不理解如何在结构中正确实现它们。我已经尝试将它与另一个结构嵌套,其中包括我的名字[21]和pin [7],并且更多地将它作为一个字符串,以及其他一些在我脑海中听起来可行但却没有正常工作的东西。任何帮助表示赞赏,并且温柔我是新来的大声笑。提前谢谢。
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
struct payroll
{
char name [MAX][21];
char pin [MAX][7];
float hours[MAX];
float hourly_pay[MAX];
float gross_pay[MAX];
float net_pay[MAX];
float overtime_pay[MAX];
float deductions[MAX];
};
typedef struct payroll PAYROLL;
void funcoutput (PAYROLL);
int main( )
{
PAYROLL employee;
int i;
for (i=0;i<=MAX;i++)
{
printf ("Enter the employees last name:\n");
scanf("%s", &employee.name[i]);
printf ("Enter the employees 6 character ID:\n");
scanf("%s", &employee.pin[i]);
printf ("Enter the employees hours for the week:\n");
scanf ("%f", &employee.hours[i]);
printf ("Enter the employees hourly rate of pay: \n");
scanf ("%f", &employee.hourly_pay[i]);
printf ("Enter any employee ovetime hours, hours exceeding 40: \n");
scanf ("%f", &employee.overtime_pay[i]);
employee.overtime_pay[i] = employee.overtime_pay[i] * 1.5;
employee.gross_pay[i] = employee.hours[i] + employee.hourly_pay[i] + employee.overtime_pay[i];
employee.deductions[i] = employee.gross_pay[i] * .25;
employee.net_pay[i] = employee.gross_pay[i] - employee.deductions[i];
}
funcoutput (employee);
return 0;
}
void funcoutput (PAYROLL employee1)
{
int i;
for (i=0;i<MAX;i++)
{
printf("Name:%s ID:%s Hours:%8.2f Hourly rate:$%8.2f Gross pay:$%8.2f Deductions:$%8.2f Net pay:$%8.2f\n",
employee1.name[i],employee1.pin[i],employee1.hours[i],employee1.hourly_pay[i],
employee1.gross_pay[i],employee1.deductions[i],employee1.net_pay[i]);
}
return;
}
答案 0 :(得分:3)
您希望结构代表员工的单个实例,然后拥有这些结构的数组。所以你的类型定义是
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct PAYROLL
{
char name [21];
char pin [7];
float hours;
float hourly_pay;
float gross_pay;
float net_pay;
float overtime_pay;
float deductions;
} payroll;
在主函数中,将变量声明更改为
PAYROLL employee[MAX];
然后,只要您employee.<field_name>[i]
,就可以将其更改为employee[i].<field_name>
最后,你必须改变funcoutput的定义
void funcoutput (PAYROLL employee1[])