-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecord.h
More file actions
97 lines (72 loc) · 1.77 KB
/
Copy pathrecord.h
File metadata and controls
97 lines (72 loc) · 1.77 KB
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
#ifndef __RECORD__
#define __RECORD__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_FILE_NAME_LENGTH 30
#define MAX_COACHES 4
#define MAX_INT_DIGITS 10
#define MAX_LONG_DIGITS 19
#define BATCH 100
#define READ 0
#define WRITE 1
typedef unsigned int uint;
typedef struct Record {
long id;
char name[20];
char surname[20];
char street_name[20];
int house_id;
char city[20];
char postcode[6];
float salary;
} Record;
int comparator(const Record *p, const Record *q, int attribute)
{
switch (attribute) {
case 1: {
return p->id - q->id;
}
case 2: {
return strcmp(p->name, q->name);
}
case 3: {
return strcmp(p->surname, q->surname);
}
case 4: {
return strcmp(p->street_name, q->street_name);
}
case 5: {
return p->house_id - q->house_id;
}
case 6: {
return strcmp(p->city, q->city);
}
case 7: {
return strcmp(p->postcode, q->postcode);
}
case 8: {
/* works well because all salaries are actually integers . If they
differed for like 0.5 then we should use an if statement and return -1
if p->salary < q->salary, 0 if they are equal, and 1 otherwise. */
return p->salary - q->salary;
}
default: {
printf("Should have never come here. Error in record.h::comparator.\n");
exit(0);
}
}
}
void swap(Record* rec1, Record* rec2)
{
Record temp;
memcpy(&temp, rec1, sizeof(Record));
memcpy(rec1, rec2, sizeof(Record));
memcpy(rec2, &temp, sizeof(Record));
}
void print_record(Record record)
{
printf("%ld %s %s %s %d %s %s %-9.2f\n", record.id, record.surname, record.name, record.street_name, record.house_id, record.city, record.postcode, record.salary);
}
#endif