-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_17.cpp
216 lines (182 loc) · 9.08 KB
/
Lab_17.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Week21.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdarg.h>
#include "string.h"
#include "stdlib.h"
struct Time
{
int hour, minute;
};
struct Date
{
int day, month, year;
};
struct workTime
{
int employeeID;
struct Date date;
struct Time startTime;
struct Time endTime;
};
char months[12][20] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
struct Time getTime(char *string);
struct Date getDate(char *string);
void AddRecord(struct workTime Record, FILE *fp);
void SearchRecords(int ID, FILE *fp);
void PrintRecords(FILE *fp);
int main(int argc, char *argv[])
{
struct workTime temp;
char *inputPath;
FILE *fp;
int i;
int ID;
inputPath = argv[1];
fp = fopen("Data.txt", "a+");
if (fp == NULL)
{
puts("Coudn't open file, closing \n");
return 1;
}
if (!strcmp(argv[2], "/a"))
{
i = 3;
while (1)
{
if (argv[i] == NULL)
{
break;
}
temp.employeeID = atoi(argv[i]);
i++;
temp.date = getDate(argv[i]);
i++;
temp.startTime = getTime(argv[i]);
i++;
temp.endTime = getTime(argv[i]);
AddRecord(temp, fp);
i++;
}
}
if (!strcmp(argv[2], "/p"))
{
PrintRecords(fp);
}
if (!strcmp(argv[2], "/s"))
{
ID = atoi(argv[3]);
SearchRecords(ID, fp);
}
return 0;
}
struct Time getTime(char *string)
{
char *token;
char delim[2] = ":";
struct Time time;
token = strtok(string, delim);
time.hour = atoi(token);
token = strtok(NULL, delim);
time.minute = atoi(token);
return time;
}
struct Date getDate(char *string)
{
struct Date date;
char *token;
char delim[2] = "/";
token = strtok(string, delim);
date.day = atoi(token);
token = strtok(NULL, delim);
date.month = atoi(token);
token = strtok(NULL, delim);
date.year = atoi(token);
return date;
}
void AddRecord(struct workTime Record, FILE *fp)
{
char temp[200];
sprintf(temp, "\"%s %d %d\",\"%d\",\"%d:%d\",\"%d:%d\"\n",months[Record.date.month -1],Record.date.day,Record.date.year,Record.employeeID,Record.startTime.hour,Record.startTime.minute,Record.endTime.hour,Record.endTime.minute );
fputs(temp, fp);
}
void PrintRecords(FILE *fp)
{
int i;
struct workTime record;
char line[200];
char *token;
char delim[5] = "\", :";
rewind(fp);
fgets(line, 200, fp);
while (!feof(fp))
{
token = strtok(line, delim);
for (i = 0; i < 12; i++)
{
if (!strcmp(months[i], token))
{
record.date.month = i + 1;
break;
}
}
token = strtok(NULL, delim);
record.date.day = atoi(token);
token = strtok(NULL, delim);
record.date.year = atoi(token);
token = strtok(NULL, delim);
record.employeeID = atoi(token);
token = strtok(NULL, delim);
record.startTime.hour = atoi(token);
token = strtok(NULL, delim);
record.startTime.minute = atoi(token);
token = strtok(NULL, delim);
record.endTime.hour = atoi(token);
token = strtok(NULL, delim);
record.endTime.minute = atoi(token);
printf("Date: %d/%d/%d Employee: %d started at %d:%d and finished at %d:%d\n", record.date.day, record.date.month, record.date.year, record.employeeID, record.startTime.hour, record.startTime.minute, record.endTime.hour, record.endTime.minute);
fgets(line, 200, fp);
}
}
void SearchRecords(int ID, FILE *fp)
{
int i;
struct workTime record;
char line[200];
char *token;
char delim[5] = "\", :";
rewind(fp);
fgets(line, 200, fp);
while (!feof(fp))
{
token = strtok(line, delim);
for (i = 0; i < 12; i++)
{
if (!strcmp(months[i], token))
{
record.date.month = i + 1;
break;
}
}
token = strtok(NULL, delim);
record.date.day = atoi(token);
token = strtok(NULL, delim);
record.date.year = atoi(token);
token = strtok(NULL, delim);
record.employeeID = atoi(token);
token = strtok(NULL, delim);
record.startTime.hour = atoi(token);
token = strtok(NULL, delim);
record.startTime.minute = atoi(token);
token = strtok(NULL, delim);
record.endTime.hour = atoi(token);
token = strtok(NULL, delim);
record.endTime.minute = atoi(token);
if (record.employeeID == ID)
{
printf("Date: %d/%d/%d Employee: %d started at %d:%d and finished at %d:%d\n", record.date.day, record.date.month, record.date.year, record.employeeID, record.startTime.hour, record.startTime.minute, record.endTime.hour, record.endTime.minute);
}
fgets(line, 200, fp);
}
}