Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct Student {
- int id;
- char sname[40];
- char fname[40];
- int points;
- char prName[270];
- };
- char *firstUp(char *s) {
- int len = strlen(s);
- if ('a' <= s[0] && s[0] <= 'z') {
- s[0] = s[0] - 'a' + 'A';
- }
- for (int i = 1; i < len; i++) {
- if ('A' <= s[i] && s[i] <= 'Z') {
- s[i] = s[i] - 'A' + 'a';
- }
- }
- return s;
- }
- void readStud(FILE *fr, struct Student *students, int N) {
- for (int i = 0; i < N; i++) {
- fscanf(fr, "%d %s %s %d %s",
- &students[i].id, students[i].sname, students[i].fname,
- &students[i].points, students[i].prName);
- firstUp(students[i].sname);
- firstUp(students[i].fname);
- firstUp(students[i].prName);
- }
- }
- void sort(struct Student *students, int N) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N - 1 - i; j++) {
- if (strcmp(students[j].sname, students[j + 1].sname) > 0) {
- struct Student tmp = students[j];
- students[j] = students[j + 1];
- students[j + 1] = tmp;
- } else if (strcmp(students[j].sname, students[j + 1].sname) == 0 &&
- strcmp(students[j].fname, students[j + 1].fname) > 0) {
- struct Student tmp = students[j];
- students[j] = students[j + 1];
- students[j + 1] = tmp;
- } else if (strcmp(students[j].sname, students[j + 1].sname) == 0 &&
- strcmp(students[j].fname, students[j + 1].fname) == 0 &&
- (students[j].id >= students[j + 1].id)) {
- struct Student tmp = students[j];
- students[j] = students[j + 1];
- students[j + 1] = tmp;
- }
- }
- }
- }
- void write(FILE *fw, struct Student *students, int N, int K) {
- for (int i = 0; i < N; i++) {
- if (students[i].points >= K) {
- fprintf(fw, "%s %s (%03d) %d\n", students[i].sname, students[i].fname, students[i].id, students[i].points);
- }
- }
- }
- int main() {
- FILE *fr = fopen("D:/Projects/CLionProjects/Lab_6/output.txt", "rt");
- FILE *fw = fopen("D:/Projects/CLionProjects/Lab_6/input.txt", "wt");
- // FILE *fr = fopen("input.txt", "rt");
- // FILE *fw = fopen("output.txt", "wt");
- unsigned N, K;
- fscanf(fr, "%d", &K);
- fscanf(fr, "%d", &N);
- struct Student *students = (struct Student *) malloc(N * sizeof(struct Student));
- readStud(fr, students, N);
- sort(students, N);
- write(fw, students, N, K);
- free(students);
- fclose(fw);
- fclose(fr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement