HomeWork1Code
4.34 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STUDENTS 100
#define MAX_RESIDENTS 100
#define MAX_STRING 50
typedef struct {
char studentCode[MAX_STRING];
char name[MAX_STRING];
char idCode[MAX_STRING];
} Student;
typedef struct {
char idCode[MAX_STRING];
char city[MAX_STRING];
} Resident;
int loadStudents(Student students[], const char *filename);
int loadResidents(Resident residents[], const char *filename);
void showAvailableCities(Resident residents[], int residentCount);
void toLowerCase(char *str);
int isCityValid(Resident residents[], int residentCount, const char *inputCity);
void findAndPrintStudents(Student students[], int studentCount, Resident residents[], int residentCount, const char *inputCity);
int initializeData(Student students[], int *studentCount, Resident residents[], int *residentCount, char *inputCity);
int main() {
Student students[MAX_STUDENTS];
Resident residents[MAX_RESIDENTS];
int studentCount, residentCount;
char inputCity[MAX_STRING];
if (initializeData(students, &studentCount, residents, &residentCount, inputCity) == -1) {
return 1;
}
findAndPrintStudents(students, studentCount, residents, residentCount, inputCity);
return 0;
}
int initializeData(Student students[], int *studentCount, Resident residents[], int *residentCount, char *inputCity) {
*studentCount = loadStudents(students, "F1.txt");
if (*studentCount == -1) return -1;
*residentCount = loadResidents(residents, "F2.txt");
if (*residentCount == -1) return -1;
showAvailableCities(residents, *residentCount);
while (1) {
printf("Enter residence: ");
scanf("%s", inputCity);
toLowerCase(inputCity);
if (isCityValid(residents, *residentCount, inputCity)) {
break;
} else {
printf("Error: The city '%s' is not in the available list. Try again.\n", inputCity);
}
}
return 0;
}
int loadStudents(Student students[], const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening %s\n", filename);
return -1;
}
int count = 0;
while (fscanf(file, "%s %s %s", students[count].studentCode, students[count].name, students[count].idCode) == 3) {
count++;
if (count >= MAX_STUDENTS) break;
}
fclose(file);
return count;
}
int loadResidents(Resident residents[], const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening %s\n", filename);
return -1;
}
int count = 0;
while (fscanf(file, "%s %s", residents[count].idCode, residents[count].city) == 2) {
toLowerCase(residents[count].city);
count++;
if (count >= MAX_RESIDENTS) break;
}
fclose(file);
return count;
}
void showAvailableCities(Resident residents[], int residentCount) {
printf("Available cities:\n");
for (int i = 0; i < residentCount; i++) {
int duplicate = 0;
for (int j = 0; j < i; j++) {
if (strcmp(residents[i].city, residents[j].city) == 0) {
duplicate = 1;
break;
}
}
if (!duplicate) {
printf("- %s\n", residents[i].city);
}
}
printf("\n");
}
void toLowerCase(char *str) {
for (int i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
}
}
int isCityValid(Resident residents[], int residentCount, const char *inputCity) {
for (int i = 0; i < residentCount; i++) {
if (strcmp(residents[i].city, inputCity) == 0) {
return 1;
}
}
return 0;
}
void findAndPrintStudents(Student students[], int studentCount, Resident residents[], int residentCount, const char *inputCity) {
printf("\nStudents in %s:\n", inputCity);
int found = 0;
for (int i = 0; i < residentCount; i++) {
if (strcmp(residents[i].city, inputCity) == 0) {
for (int j = 0; j < studentCount; j++) {
if (strcmp(students[j].idCode, residents[i].idCode) == 0) {
printf("%s %s\n", students[j].studentCode, students[j].name);
found = 1;
}
}
}
}
if (!found) {
printf("No students found in this city.\n");
}
}