Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
dajavo
/
Programming2_HomeWork1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f1f7afb6
authored
Feb 25, 2025
by
dajavo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
166 additions
and
0 deletions
HomeWork1Code
HomeWork1Code
0 → 100644
View file @
f1f7afb6
#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");
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment