/************************************************************************************/ /* This program has been written by : KAMRAN AMINI 841410306 */ /* This program claculates some operations on one matrice */ /************************************************************************************/ #define MAX 20 #include #include #include //=============== Dars Structure =========================== struct Dars { unsigned int code ; unsigned float score ; } ; //=============== Student Structure ======================== struct Student { unsigned int number ; unsigned int number_of_dars ; char first_name[ 128 ] ; char last_name[ 128 ] ; unsigned float average ; Dars dars[ MAX ] ; } ; //=============== Functions ================================ void assign( Student * , Student ) ; void sort_by_number( Student [ MAX ] , int ) ; void show( Student [ MAX ] , int ) ; void print( Student ) ; void avg( Student * ) ; //================== Main Program ========================== int main( void ) { int student_counter = 0 , dars_counter , index , num ; Student list[ MAX ] ; bool cont = true , add = true , found = false ; //Initializeing ... for( int counter = 0 ; counter < MAX ; counter++ ) list[ counter ].number_of_dars = 0 ; //Begining ... while( cont && student_counter < MAX ) { cout << "Enter the number of student( " << student_counter + 1 << " )[ Enter -1000 for exit ] ? " ; cin >> num ; if( num == -1000 ) cont = false ; else { //There is anyone with this number ... for( counter = 0 ; counter < MAX ; counter++ ) if( num == list[ counter ].number ) { index = counter ; found = true ; break ; } if( found ) add= false ; else { cout << "\tEnter the first and last name ?" ; cin >> list[ student_counter ].first_name >> list[ student_counter ].last_name ; list[ student_counter ].number = num ; index = student_counter ; add = true ; } dars_counter = list[ index ].number_of_dars ; while( dars_counter < MAX ) { cout << "\tEnter the code of dars( " << dars_counter + 1 << " )[ Enter -1000 for exit ] ? " ; cin >> list[ index ].dars[ dars_counter ].code ; if( list[ index ].dars[ dars_counter ].code == -1000 || dars_counter == 19 ) { list[ index ].number_of_dars = dars_counter ; break ; } else { cout << "\tEnter the score of this dars ? " ; cin >> list[ index ].dars[ dars_counter ].score ; } dars_counter++ ; } avg( &list[ index ] ) ; if( add ) student_counter++ ; } } sort_by_number( list , student_counter ) ; show( list , student_counter ) ; return 0 ; } //===================================================== void assign( Student *s1 , Student s2 ) { s1->average = s2.average ; for( int counter = 0 ; counter < MAX ; counter++ ) s1->dars[ counter ] = s2.dars[ counter ] ; strcpy( s1->first_name , s2.first_name ) ; strcpy( s1->last_name , s2.last_name ) ; s1->number = s2.number ; s1->number_of_dars = s2.number_of_dars ; } //===================================================== void sort_by_number( Student list[ MAX ] , int student_counter ) { Student temp ; for( int rco = 0 ; rco < student_counter ; rco++ ) for( int cco = 0 ; cco < student_counter ; cco++ ) if( list[ rco ].number < list[ cco ].number ) { assign( &temp , list[ rco ] ) ; assign( &list[ rco ] , list[ cco ] ) ; assign( &list[ cco ] , temp ) ; } } //===================================================== void show( Student list[ MAX ] , int student_counter ) { for( int counter = 0 ; counter < student_counter ; counter++ ) print( list[ counter ] ) ; } //===================================================== void print( Student s ) { cout << setiosflags( ios::showpoint || ios::fixed ) << setprecision( 2 ) << "************************************" << endl << "Student Number : " << s.number << endl << "Student Name : " << s.first_name << " " << s.last_name << endl << "Student Average : " << s.average << endl << "Number of dars : " << s.number_of_dars << endl ; } //===================================================== void avg( Student *s ) { float sum = 0 ; for( int counter = 0 ; counter < s->number_of_dars ; counter++ ) sum += s->dars[ counter ].score ; s->average = sum / s->number_of_dars ; }