gpt4 book ai didi

c - C 语言的员工薪酬程序,带有数组和结构

转载 作者:行者123 更新时间:2023-12-04 05:00:50 25 4
gpt4 key购买 nike

我对这个程序的输出有一些问题。

逻辑似乎没问题,但是每当我编译/运行它时,我都会在输出部分(我使用 Xcode)中得到一个 (11db)。

它说“构建成功”并且代码本身没有显示任何错误。

基本上,您必须从输入文件“clock.txt”中读取一些值,其中包含:

*雇员的数量 (n) *他们的名字和姓氏,然后是他们的工资,(你把它放在一个名为雇员的结构中)*周数 (k)

然后,您必须计算一周的总工资(超过税金总额和净工资(总税)。

这是我到目前为止所写的内容:

#include <stdio.h>
#include <string.h>
#define MAX_LEN 30

struct employee {
char first[MAX_LEN];
char last[MAX_LEN];
double payperhr;
double gross;
double taxes;
double hours_in_week;
} ;

int main() {
FILE *ifp, *ofp;
ifp = fopen("clock.txt", "r");
ofp = fopen("w2.txt", "w");
int i, j, n, k, m, l, matchLast, found;
int hourIn, minIn, hourOut, minOut;
double total_time_day = 0.0;
double under40tax, over40tax, tax;
double netPay;

char firstRead[MAX_LEN], lastRead[MAX_LEN], lastName[MAX_LEN], firstName[MAX_LEN];
float rateRead;

fscanf(ifp, "%d", &n);


struct employee arrayEmployees[n];

for (i=0; i<n; i++) {
fscanf(ifp, "%s", firstRead);
fscanf(ifp, "%s", lastRead);
fscanf(ifp, "%f", &rateRead);

strcpy(arrayEmployees[n].first, firstRead);
strcpy(arrayEmployees[n].last, lastRead);
arrayEmployees[n].payperhr = rateRead;
}

fscanf(ifp, "%d", &k);
fscanf(ifp, "%d", &m);

for (i=0; i<k; i++) {
for (j=0; j<m; j++) {
fscanf(ifp, "%s", lastName);
fscanf(ifp, "%s", firstName);

for (l=0; l<n; l++)
matchLast = strcmp(lastName, arrayEmployees[l].last);
if (matchLast == 0) {
found = matchLast;
strcpy(arrayEmployees[i].last, lastName);
strcpy(arrayEmployees[i].first, firstName);

fscanf(ifp, "%d%d%d%d", &hourIn, &minIn, &hourOut, &minOut);
hourIn%=12;
hourOut%=12;
total_time_day = (((hourOut*1.0) - hourIn)+((minOut*1.0) - minIn))/60;
arrayEmployees[j].hours_in_week += total_time_day;
arrayEmployees[j].gross += total_time_day*rateRead;
}
}
if (arrayEmployees[i].hours_in_week/40<1) {
under40tax = arrayEmployees[j].gross*.1;
tax = under40tax;
arrayEmployees[i].taxes = tax;
}
else if (arrayEmployees[j].hours_in_week/40>1) {
over40tax = (40*.1)+((arrayEmployees[j].hours_in_week-40)*.2);
arrayEmployees[i].gross -= 40;
arrayEmployees[i].gross = arrayEmployees[i].hours_in_week*1.5;
arrayEmployees[i].gross +=40;
tax = over40tax;
arrayEmployees[i].taxes = tax;
}
netPay = arrayEmployees[i].gross - tax;
arrayEmployees[i].hours_in_week = 0.0;

fprintf(ofp, "W2 Form\n");
fprintf(ofp, "-----");
fprintf(ofp, "Name: %s %s", arrayEmployees[i].last, arrayEmployees[i].first);
fprintf(ofp, "Gross Pay: %lf", arrayEmployees[i].gross);
fprintf(ofp, "Taxes Withheld: %lf", arrayEmployees[i].taxes);
fprintf(ofp, "Net Pay: %lf", netPay);

arrayEmployees[i].hours_in_week = 0.0;
arrayEmployees[i].taxes = 0.0;
arrayEmployees[i].gross = 0.0;
}
return 0;
}

最佳答案

这里有问题

 for (i=0; i<n; i++) {
fscanf(ifp, "%s", firstRead);
fscanf(ifp, "%s", lastRead);
fscanf(ifp, "%f", &rateRead);

strcpy(arrayEmployees[n].first, firstRead);
strcpy(arrayEmployees[n].last, lastRead);
arrayEmployees[n].payperhr = rateRead;
}


    for (j=0; j<m; j++) {
fscanf(ifp, "%s", lastName);
fscanf(ifp, "%s", firstName);

for (l=0; l<n; l++)
matchLast = strcmp(lastName, arrayEmployees[l].last);
if (matchLast == 0) {
found = matchLast;
strcpy(arrayEmployees[i].last, lastName);
strcpy(arrayEmployees[i].first, firstName);

fscanf(ifp, "%d%d%d%d", &hourIn, &minIn, &hourOut, &minOut);
hourIn%=12;
hourOut%=12;
total_time_day = (((hourOut*1.0) - hourIn)+((minOut*1.0) - minIn))/60;
arrayEmployees[j].hours_in_week += total_time_day;
arrayEmployees[j].gross += total_time_day*rateRead;
}
}

它实际上应该是
 for (i=0; i<n; i++) {
fscanf(ifp, "%s", firstRead);
fscanf(ifp, "%s", lastRead);
fscanf(ifp, "%f", &rateRead);

strcpy(arrayEmployees[i].first, firstRead);
strcpy(arrayEmployees[i].last, lastRead);
arrayEmployees[i].payperhr = rateRead;
}


    for (j=0; j<m; j++) {

fscanf(ifp, "%s", lastName);
fscanf(ifp, "%s", firstName);

for (l=0; l<n; l++){

matchLast = strcmp(lastName, arrayEmployees[l].last);

if (matchLast == 0) {

found = matchLast;
strcpy(arrayEmployees[j].last, lastName);
strcpy(arrayEmployees[j].first, firstName);

fscanf(ifp, "%d%d%d%d", &hourIn, &minIn, &hourOut, &minOut);

hourIn%=12;
hourOut%=12;
total_time_day = (((hourOut*1.0) - hourIn)+((minOut*1.0) - minIn))/60;

arrayEmployees[j].hours_in_week += total_time_day;
arrayEmployees[j].gross += total_time_day*rateRead;
}
}
}

关于c - C 语言的员工薪酬程序,带有数组和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com