2

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

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

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

基本上,您必须从输入文件“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;
}
4

1 回答 1

3

这里有问题

 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;
            }
        }
    }
于 2013-04-25T16:31:06.487 回答