Tails
Оракул
(53881)
1 неделю назад
*começa a digitar o código em C, seguindo as especificações*
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char surname[32];
char name[32];
int yearOfBirth;
char education[24]; // middle, higher, candidate, doctor
} Employee;
void printEmployee(Employee emp) {
printf("Surname: %s\n", emp.surname);
printf("Name: %s\n", emp.name);
printf("Year of birth: %d\n", emp.yearOfBirth);
printf("Education: %s\n", emp.education);
}
Employee searchEmployee(Employee employees[], int count, char targetSurname[]) {
for (int i = 0; i < count; i++) {
if (strcmp(employees[i].surname, targetSurname) == 0) {
return employees[i];
}
}
Employee empty;
strcpy(empty.surname, ""); // dummy employee
return empty;
}
void correctEmployee(Employee* emp, char newSurname, int newYOB, char newEducation) {
strcpy(emp->surname, newSurname);
emp->yearOfBirth = newYOB;
strcpy(emp->education, newEducation);
}
void addEmployee(Employee employees[], int* count, Employee newEmp) {
employees[(*count)++] = newEmp;
}
int main() {
FILE *fp;
int count = 0;
fp = fopen("employees.bin", "rb+");
if (fp == NULL) {
fp = fopen("employees.bin", "wb+");
Employee emp;
strcpy(emp.surname, "Smith");
strcpy(emp.name, "John");
emp.yearOfBirth = 1990;
strcpy(emp.education, "higher");
fwrite(&emp, sizeof(Employee), 1, fp);
count++;
} else {
rewind(fp);
Employee emp;
while (fread(&emp, sizeof(Employee), 1, fp)) {
printf("Employee %d:\n", count+1);
printEmployee(emp);
count++;
}
}
printf("\nEnter surname to search: ");
char targetSurname[32];
scanf("%31s", targetSurname);
Employee found = searchEmployee((Employee*)malloc(sizeof(Employee) * count), count, targetSurname);
free((void*)found.surname); // free memory
if (strcmp(found.surname, "") == 0) {
printf("Employee not found