Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define max_students 35
- #define max_name 10
- #define max_number 5
- #define max_results 4
- struct student{
- char second_name[max_name], initials[2], number_of_book[max_number];
- int results[max_results];
- double avg_rating;
- };
- void sort ( struct student students[], int n, int key ){
- int i, j;
- if ( key == 1 ){
- /*for ( i = 0; i < n - 1; i++ ){
- for ( j = 0; j < n - i - 1; j++ ){
- int compare_element = 0, random_var = 1;
- while ( random_var && compare_element < max_name ){
- if ( students[j].second_name[compare_element] > students[j+1].second_name[compare_element] ){
- struct student swap = students[j];
- students[j] = students[j+1];
- students[j+1] = swap;
- random_var = 0;
- }
- else compare_element++;
- if ( compare_element == max_name + 1 ){
- compare_element = 0;
- while ( random_var && compare_element < 2 ){
- if ( students[j].initials[compare_element] > students[j+1].initials[compare_element] ){
- struct student swap = students[j];
- students[j] = students[j+1];
- students[j+1] = swap;
- random_var = 0;
- }
- else compare_element++;
- }
- break;
- }
- }
- }
- }*/
- }
- else{
- for ( i = 0; i < n - 1; i++ )
- for ( j = 0; j < n - i - 1; j++ )
- if ( students[j].avg_rating < students[j+1].avg_rating ){
- struct student swap = students[j];
- students[j] = students[j+1];
- students[j+1] = swap;
- }
- }
- }
- double avg_rate ( struct student student ){
- double avg = 0;
- for ( int i = 0; i < max_results; i++ )
- avg += student.results[i];
- return avg / max_results;
- }
- int search ( struct student students[], int n, double key ){
- int sum = 0, i, j;
- for ( i = 0; i < n; i++ ){
- int num = 0;
- for ( j = 0; j < max_results; j++ ){
- if ( students[i].results[j] < key ){
- num = 0;
- break;
- }
- else if ( students[i].results[j] == key ) num = 1;
- }
- if ( num ) sum++;
- }
- return sum;
- }
- void output ( struct student students[], int n, int key, int result, int type ){
- if ( key == 0 || key == 1 ){
- char types[2][15] = { "name", "average marks" };
- key == 0 ? printf ( "Information about students:\n") : NULL;
- key == 1 ? printf ( "Sorted by %s:\n", &types[type] ) : NULL;
- for ( int i = 0; i < n; i++ ){
- printf ( "[%d] %s %c.%c.\n", i+1, &students[i].second_name,
- students[i].initials[0],
- students[i].initials[1] );
- printf ( "\tRecord-book number:\t%s\n", &students[i].number_of_book );
- printf ( "\tExam results:\t\t%d, %d, %d, %d\n", students[i].results[0],
- students[i].results[1],
- students[i].results[2],
- students[i].results[3] );
- printf ( "\n");
- }
- }
- else if ( key == 2 ){
- char types[4][35] = { "excellent students", "good students",
- "threesome students", "students who didn't pass exams" };
- printf ( "Number of %s: %d.\n\n", &types[type], result );
- }
- }
- int main (){
- struct student students[max_students];
- double avg_rating = 0;
- int n, i;
- printf ( "Input number of students: " );
- scanf ( "%d", &n );
- for ( i = 0; i < n; i++ ){
- char second_name[max_name], initials[2], number_of_book[max_number];
- printf ( "Input second name and initials of %d student: ", i + 1 );
- scanf ( "%s %c.%c.", &second_name, &initials[0], &initials[1] );
- for ( int j = 0; j < max_name; j++ ){
- students[i].second_name[j] = second_name[j];
- j < 2 ? students[i].initials[j] = initials[j] : NULL;
- }
- printf ( "Input %d student record-book number: ", i + 1 );
- scanf ( "%s", &number_of_book );
- for ( int j = 0; j < max_number; j++ )
- students[i].number_of_book[j] = number_of_book[j];
- printf ( "Input %d exam results of %d student: ", max_results, i + 1 );
- scanf ( "%d %d %d %d", &students[i].results[0], &students[i].results[1],
- &students[i].results[2], &students[i].results[3] );
- students[i].avg_rating = avg_rate ( students[i] );
- printf( "\n" );
- }
- output ( students, n, 0, 0, 0);
- printf ( "\n\n" );
- sort ( students, n, 1);
- output ( students, n, 1, 0, 0);
- printf ( "\n\n" );
- sort ( students, n, 0);
- output ( students, n, 1, 0, 1);
- printf ( "\n\n" );
- int j = 0;
- for ( i = 5; i > 1; i-- ){
- output ( students, n, 2, search ( students, n, ( double ) i ), j );
- j++;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement