在我的C类简介中进行工资单分配,目标是读取文本文件,然后将其工资信息输出到文本文件。我收到了一些不兼容的指针错误,CodeBlocks只给了我一个包含一堆乱码的输出文件。我发布了下面的文本文件和我的评论,这样你就可以理解我对我的代码做了些什么。任何帮助都会做,欣赏它们和gals。
2 (this is the number of employees)
5.50 (pay rate for employee 0)
10.00 (pay rate of employee 1)
3 (number of weeks)
2 (data in relation to this week)
1 10 30 13 30 (reads: employee 1 clocked in at 1030, clocked out 1330)
0 7 0 16 30 (reads: employee 0 clocked in at 700, clocked out 1630)
4
0 9 0 14 30
1 7 0 23 0
1 9 0 22 0
1 7 20 23 20
3
0 10 0 15 0
1 8 0 12 0
0 9 30 11 30
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EMPLOYEES 20
#define WEEKS 10
/*prototype functions: One to take in the total hours, the other printing out the text file*/
double totalHrs(int hi, int mi, int ho, int mo);
void printLog( int weeks, int numEmp, double Emp[numEmp][2], int wkHrs[numEmp][weeks]);
int main()
{
/*variables for number of employees, number of weeks, shifts and hours in and hour along with minutes.*/
int n, weeks, shifts;
int empId, hrIn, minIn, hrOut, minOut, tmpWkHrs;
double m;
FILE *ifp;
/*Read clock text file*/
ifp = fopen("clock.txt", "r");
if(ifp == NULL)
{
exit(1);
}
/*scan in the number of employees*/
fscanf(ifp, "%d", &n);
if(n>EMPLOYEES)
{
printf("Employees provided is greater than 20");
exit(0);
}
double emp[n][2];
for(int i=0; i<n;i++)
{
fscanf(ifp, "%lf", &m);
emp[i][1] = m;
}
fscanf(ifp, "%d", &weeks);
double empWkHrs[n][weeks];
for(int i = 0; i < weeks; i++)
{
fscanf(ifp, "%d", &shifts);
for (int j = 0; j < shifts; j++)
{
fscanf(ifp, "%d", &empId);
fscanf(ifp, "%d", &hrIn);
fscanf(ifp, "%d", &minIn);
fscanf(ifp, "%d", &hrOut);
fscanf(ifp, "%d", &minOut);
emp[empId][0] += totalHrs(hrIn, minIn, hrOut, minOut);
empWkHrs[empId][i] += totalHrs(hrIn, minIn, hrOut, minOut);
}
}
printLog(weeks, n, emp, empWkHrs);
fclose(ifp);
return 0;
}
/*------------------------------------------------------------------------------------*/
double totalHrs(int hi, int mi, int ho, int mo)
{
double totalHrs = (double)abs(ho - hi);
double totalmins = (double)abs(mo - mi);
totalHrs += totalmins/60;
return totalHrs;
}
/*------------------------------------------------------------------------------------*/
/*Print function that prints out the information onto a text file named payroll*/
void printLog( int weeks, int numEmp, double Emp[numEmp][2], int wkHrs[numEmp][weeks])
{
FILE *ofp;
ofp = fopen("payroll.txt", "w+");
if(ofp == NULL)
{
exit(1);
}
fprintf(ofp, "Number of employees: %d\n", numEmp);
fprintf(ofp, "Number of weeks: %d\n", weeks);
/*Loop through to print out all of the information in regards to every week*/
for(int i = 0; i < weeks; i++)
{
fprintf(ofp, "Wk%d\n", weeks+1);
fprintf(ofp,"EmpID\t Hours\t Pay\n");
for(int j = 0; j < numEmp; j++)
{
/*If statement for an employee that works over 40 hours */
if(wkHrs[j][i] > 40)
{
fprintf(ofp, "\t %d\t %lf\t %lf", j, wkHrs[j][i], (Emp[j][1]*40)+(wkHrs[j][i]-40)*1.5*Emp[j][1]);
}
else
{
fprintf(ofp, "\t %d\t %lf\t %lf", j, wkHrs[j][i], Emp[j][1]*wkHrs[j][i]);
}
}
}
fprintf(ofp, "Total\n");
fprintf(ofp, "EmpID\t Hours\t Pay\n");
for(int j = 0; j < numEmp; j++)
{
fprintf(ofp, "\t %d\t %lf\t %lf", j, Emp[j][0], Emp[j][1]*Emp[j][0]);
}
fclose(ofp);
}
这就是输出的样子。
Number of employees: 2
Number of weeks: 3
Wk 1
EmpID Hours Pay
0 9.50 52.25
1 3.00 30.00
Wk 2
EmpID Hours Pay
0 5.50 30.25
1 45.00 450.00
Wk 3
EmpID Hours Pay
0 7.00 38.5.00
1 4.00 40.00
Total
EmpID Hours Pay
0 22.00 121.00
1 52.00 520.00
这是我在payroll.o文件中得到的内容
Number of weeks: %d
Wk%d
EmpID Hours Pay
%d %lf %lfTotal
qÄzRxê$@¯ˇˇˇˇˇˇAÜC
$D˚ˇˇˇˇˇˇqAÜC
$lp˚ˇˇˇˇˇˇAÜC
-c-
答案 0 :(得分:1)
在printLog
中,最后一个参数的类型应为double
,而不是int
。