Skip to content

Instantly share code, notes, and snippets.

@DieTime
Last active December 15, 2019 15:20
Show Gist options
  • Save DieTime/207073df4ce1ccfe693cc88270bc7f35 to your computer and use it in GitHub Desktop.
Save DieTime/207073df4ce1ccfe693cc88270bc7f35 to your computer and use it in GitHub Desktop.
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
struct Student {
char surname[32];
char group[32];
int marks[3];
};
int main() {
setlocale(LC_ALL, "rus");
FILE* input = NULL;
FILE* output = NULL;
int num = 0, i = 0, j = 0, sizeoffile = 1;
char surname[32];
char group[32];
int marks[3];
input = fopen("ddd.dat", "rb");
if (input) {
fseek(input, 0, SEEK_END);
sizeoffile = ftell(input);
fseek(input, 0, SEEK_SET);
}
if (!input || sizeoffile == 0) {
output = fopen("ddd.dat", "wb");
printf("Введите колличество студентов: ");
scanf_s("%d", &num);
struct Student* students = (struct Student*)malloc(sizeof(struct Student)*num);
printf("Вводите данные в формате (Фамилия, Группа, 3 оценки) через пробел на английском:\n");
for (i = 0; i < num; i++) {
scanf("%s%s%d%d%d", surname, group, &marks[0], &marks[1], &marks[2]);
strcpy_s(students[i].surname, 32, surname);
strcpy_s(students[i].group, 32, group);
for (int j = 0; j < 3; j++) {
students[i].marks[j] = marks[j];
}
}
fwrite(&num, sizeof(int), 1, output);
for (i = 0; i < num; i++) {
fwrite(&students[i], sizeof(struct Student), 1, output);
}
free(students);
fclose(output);
}
input = fopen("ddd.dat", "rb");
fread(&num, sizeof(int), 1, input);
struct Student* students = (struct Student*)malloc(sizeof(struct Student) * num);
for (i = 0; i < num; i++)
fread(&students[i], sizeof(struct Student), 1, input);
fclose(input);
for (i = 0; i < num - 1; i++) {
for (j = i + 1; j < num; j++) {
if (students[i].marks[0] + students[i].marks[1] + students[i].marks[3] > students[j].marks[0] + students[j].marks[1] + students[j].marks[3]) {
struct Student temp;
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Отсортированные по среднему баллу: \n");
for (i = 0; i < num; i++) {
for (i = 0; i < num; i++) {
printf("%s %s %d %d %d\n", students[i].surname, students[i].group, students[i].marks[0], students[i].marks[1], students[i].marks[2]);
}
}
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment