Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define max_students 35
  4. #define max_name 10
  5. #define max_number 5
  6. #define max_results 4
  7.  
  8. struct student{
  9.   char second_name[max_name], initials[2], number_of_book[max_number];
  10.   int results[max_results];
  11.   double avg_rating;
  12. };
  13.  
  14. void sort ( struct student students[], int n, int key ){
  15.   int i, j;
  16.   if ( key == 1 ){
  17.     /*for ( i = 0; i < n - 1; i++ ){
  18.       for ( j = 0; j < n - i - 1; j++ ){
  19.         int compare_element = 0, random_var = 1;
  20.         while ( random_var && compare_element < max_name ){
  21.           if ( students[j].second_name[compare_element] > students[j+1].second_name[compare_element] ){
  22.             struct student swap = students[j];
  23.             students[j] = students[j+1];
  24.             students[j+1] = swap;
  25.             random_var = 0;
  26.           }
  27.           else compare_element++;
  28.           if ( compare_element == max_name + 1 ){
  29.             compare_element = 0;
  30.             while ( random_var && compare_element < 2 ){
  31.               if ( students[j].initials[compare_element] > students[j+1].initials[compare_element] ){
  32.                 struct student swap = students[j];
  33.                 students[j] = students[j+1];
  34.                 students[j+1] = swap;
  35.                 random_var = 0;
  36.               }
  37.               else compare_element++;
  38.             }
  39.             break;
  40.           }
  41.         }
  42.       }
  43.     }*/
  44.   }
  45.   else{
  46.     for ( i = 0; i < n - 1; i++ )
  47.       for ( j = 0; j < n - i - 1; j++ )
  48.         if ( students[j].avg_rating < students[j+1].avg_rating ){
  49.           struct student swap = students[j];
  50.           students[j] = students[j+1];
  51.           students[j+1] = swap;
  52.         }
  53.   }
  54. }
  55.  
  56. double avg_rate ( struct student student ){
  57.   double avg = 0;
  58.   for ( int i = 0; i < max_results; i++ )
  59.     avg += student.results[i];
  60.   return avg / max_results;
  61. }
  62.  
  63. int search ( struct student students[], int n, double key ){
  64.   int sum = 0, i, j;
  65.   for ( i = 0; i < n; i++ ){
  66.     int num = 0;
  67.     for ( j = 0; j < max_results; j++ ){
  68.       if ( students[i].results[j] < key ){
  69.         num = 0;
  70.         break;
  71.       }
  72.       else if ( students[i].results[j] == key ) num = 1;
  73.     }
  74.     if ( num ) sum++;
  75.   }
  76.   return sum;
  77. }
  78.  
  79. void output ( struct student students[], int n, int key, int result, int type ){
  80.   if ( key == 0 || key == 1 ){
  81.     char types[2][15] = { "name", "average marks" };
  82.     key == 0 ? printf ( "Information about students:\n") : NULL;
  83.     key == 1 ? printf ( "Sorted by %s:\n", &types[type] ) : NULL;
  84.  
  85.     for ( int i = 0; i < n; i++ ){
  86.       printf ( "[%d] %s %c.%c.\n", i+1, &students[i].second_name,
  87.                                         students[i].initials[0],
  88.                                         students[i].initials[1] );
  89.       printf ( "\tRecord-book number:\t%s\n", &students[i].number_of_book );
  90.       printf ( "\tExam results:\t\t%d, %d, %d, %d\n", students[i].results[0],
  91.                                                       students[i].results[1],
  92.                                                       students[i].results[2],
  93.                                                       students[i].results[3] );
  94.       printf ( "\n");
  95.     }
  96.   }
  97.   else if ( key == 2 ){
  98.     char types[4][35] = { "excellent students", "good students",
  99.                        "threesome students", "students who didn't pass exams" };
  100.     printf ( "Number of %s: %d.\n\n", &types[type], result );
  101.   }
  102. }
  103.  
  104. int main (){
  105.   struct student students[max_students];
  106.   double avg_rating = 0;
  107.   int n, i;
  108.  
  109.   printf ( "Input number of students: " );
  110.   scanf ( "%d", &n );
  111.  
  112.   for ( i = 0; i < n; i++ ){
  113.     char second_name[max_name], initials[2], number_of_book[max_number];
  114.     printf ( "Input second name and initials of %d student: ", i + 1 );
  115.     scanf ( "%s %c.%c.", &second_name, &initials[0], &initials[1] );
  116.     for ( int j = 0; j < max_name; j++ ){
  117.       students[i].second_name[j] = second_name[j];
  118.       j < 2 ? students[i].initials[j] = initials[j] : NULL;
  119.     }
  120.  
  121.     printf ( "Input %d student record-book number: ", i + 1 );
  122.     scanf ( "%s", &number_of_book );
  123.     for ( int j = 0; j < max_number; j++ )
  124.       students[i].number_of_book[j] = number_of_book[j];
  125.  
  126.     printf ( "Input %d exam results of %d student: ", max_results, i + 1 );
  127.     scanf ( "%d %d %d %d", &students[i].results[0], &students[i].results[1],
  128.                            &students[i].results[2], &students[i].results[3] );
  129.  
  130.     students[i].avg_rating = avg_rate ( students[i] );
  131.  
  132.     printf( "\n" );
  133.   }
  134.  
  135.   output ( students, n, 0, 0, 0);
  136.   printf ( "\n\n" );
  137.  
  138.   sort ( students, n, 1);
  139.   output ( students, n, 1, 0, 0);
  140.   printf ( "\n\n" );
  141.  
  142.   sort ( students, n, 0);
  143.   output ( students, n, 1, 0, 1);
  144.   printf ( "\n\n" );
  145.  
  146.   int j = 0;
  147.   for ( i = 5; i > 1; i-- ){
  148.     output ( students, n, 2, search ( students, n, ( double ) i ), j );
  149.     j++;
  150.   }
  151.  
  152.   return 1;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement