Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct Student {
  6.     int id;
  7.     char sname[40];
  8.     char fname[40];
  9.     int points;
  10.     char prName[270];
  11. };
  12.  
  13. char *firstUp(char *s) {
  14.     int len = strlen(s);
  15.     if ('a' <= s[0] && s[0] <= 'z') {
  16.         s[0] = s[0] - 'a' + 'A';
  17.     }
  18.     for (int i = 1; i < len; i++) {
  19.         if ('A' <= s[i] && s[i] <= 'Z') {
  20.             s[i] = s[i] - 'A' + 'a';
  21.         }
  22.     }
  23.     return s;
  24. }
  25.  
  26. void readStud(FILE *fr, struct Student *students, int N) {
  27.     for (int i = 0; i < N; i++) {
  28.         fscanf(fr, "%d %s %s %d %s",
  29.                &students[i].id, students[i].sname, students[i].fname,
  30.                &students[i].points, students[i].prName);
  31.  
  32.         firstUp(students[i].sname);
  33.         firstUp(students[i].fname);
  34.         firstUp(students[i].prName);
  35.     }
  36. }
  37.  
  38. void sort(struct Student *students, int N) {
  39.     for (int i = 0; i < N; i++) {
  40.         for (int j = 0; j < N - 1 - i; j++) {
  41.             if (strcmp(students[j].sname, students[j + 1].sname) > 0) {
  42.                 struct Student tmp = students[j];
  43.                 students[j] = students[j + 1];
  44.                 students[j + 1] = tmp;
  45.             } else if (strcmp(students[j].sname, students[j + 1].sname) == 0 &&
  46.                        strcmp(students[j].fname, students[j + 1].fname) > 0) {
  47.                 struct Student tmp = students[j];
  48.                 students[j] = students[j + 1];
  49.                 students[j + 1] = tmp;
  50.             } else if (strcmp(students[j].sname, students[j + 1].sname) == 0 &&
  51.                        strcmp(students[j].fname, students[j + 1].fname) == 0 &&
  52.                        (students[j].id >= students[j + 1].id)) {
  53.                 struct Student tmp = students[j];
  54.                 students[j] = students[j + 1];
  55.                 students[j + 1] = tmp;
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. void write(FILE *fw, struct Student *students, int N, int K) {
  62.     for (int i = 0; i < N; i++) {
  63.         if (students[i].points >= K) {
  64.             fprintf(fw, "%s %s (%03d) %d\n", students[i].sname, students[i].fname, students[i].id, students[i].points);
  65.         }
  66.     }
  67. }
  68.  
  69. int main() {
  70.     FILE *fr = fopen("D:/Projects/CLionProjects/Lab_6/output.txt", "rt");
  71.     FILE *fw = fopen("D:/Projects/CLionProjects/Lab_6/input.txt", "wt");
  72. //    FILE *fr = fopen("input.txt", "rt");
  73. //    FILE *fw = fopen("output.txt", "wt");
  74.     unsigned N, K;
  75.  
  76.     fscanf(fr, "%d", &K);
  77.     fscanf(fr, "%d", &N);
  78.  
  79.     struct Student *students = (struct Student *) malloc(N * sizeof(struct Student));
  80.  
  81.     readStud(fr, students, N);
  82.     sort(students, N);
  83.     write(fw, students, N, K);
  84.  
  85.     free(students);
  86.     fclose(fw);
  87.     fclose(fr);
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement