步骤/方法
/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ ssm.c : Students' score manager V 1.01 Copyright (C) 2003 by syuui . All rights reserved . Red Hat 9.0 , gcc 3.2.2 compiling passed . Usage : ssm -axnlh [-i [input file]] [-o [output file]] -a : calculate classes' average score -x : calculate students' max score -n : calculate students' min score -l : list all records -h : print help page -i [input file] : read data from [input file] Default file : in.src -o [output file] : output result to [output file] Default file : out.src _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/ #include #include #define FNAME_LEN 255 //--------------file name length #define NM_LEN 255 //--------------name length #define _DFLT_OUT out.src //------default output file name #define _DFLT_INP in.src //------default input file name /*-=-=-=-definations of flags-=-=-=-*/ #define LIST 1 #define AVER 2 #define MAX 4 #define MIN 8 #define INPUT 16 #define OUTPUT 32 struct MODEL { /*-=-=-=- Name -=-=-=-*/ char name[NM_LEN] ; /*-=-=-=- scores -=-=-=-*/ int c1 , //--------------score 1 c2 , //--------------score 2 c3 , //--------------score 3 c4 , //--------------score 4 c5 , //--------------score 5 total ; //--------------total of score float aver ; //--------------average of score struct MODEL *next ; //------link pointer }; typedef struct MODEL SCORE ; typedef int FLAG ; /*-=-=-=-=-=-Function _help-=-=-=-=-=-*/ void _help() { fprintf( stderr , \n ); fprintf( stderr , _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/\n\n ); fprintf( stderr , ssm.c : Students' score manager V1.01\n ); fprintf( stderr , Copyright (C) 2003 by syuui . All rights reserved .\n\n ); fprintf( stderr , Usage : ssm -axnlh [-i ] [-o ]\n ); fprintf( stderr , -a : calculate classes' average score\n ); fprintf( stderr , -x : calculate students' max score\n ); fprintf( stderr , -n : calculate students' min score\n ); fprintf( stderr , -l : list all records\n ); fprintf( stderr , -h : print this screen\n ); fprintf( stderr , -i [input file] : read data from [input file]\n ); fprintf( stderr , default : in.src\n ); fprintf( stderr , -o [output file] : output result to [output file]\n ); fprintf( stderr , default : out.src\n\n ); fprintf( stderr , _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/\n\n ); } /*-=-=-=-=-=-Function push-=-=-=-=-=-*/ /* This function will push a node to link */ /* If this function successfully exited , return 0 */ int push( SCORE **head , //--------------head of the link const char *dname , //----------student's name const int dc1 , //--------------score 1 const int dc2 , //--------------score 2 const int dc3 , //--------------score 3 const int dc4 , //--------------score 4 const int dc5 ) //--------------score 5 { SCORE *tmp , *new ; //------new node /*-=-=-=-Open a node-=-=-=-*/ new = (SCORE *)malloc( sizeof(SCORE) ); /*-=-=-=-Set data-=-=-=-*/ strcpy( new->name , dname ); new->c1 = dc1 ; new->c2 = dc2 ; new->c3 = dc3 ; new->c4 = dc4 ; new->c5 = dc5 ; /*-=-=-=-Push node into the link-=-=-=-*/ if( *head == NULL ) { *head = new ; (*head)->next = NULL ; } else { tmp = *head ; *head = new ; (*head)->next = tmp ; } return 0; } /*-=-=-=-=-=-Function class_aver-=-=-=-=-=-*/ /* This function will claculate average score */ /* This function returns number of the records */ int class_aver( SCORE *head , //--------------head of the link float *ac1 , //--------------average score 1 float *ac2 , //--------------average score 2 float *ac3 , //--------------average score 3 float *ac4 , //--------------average score 4 float *ac5 ) //--------------average score 5 { float tc1 , tc2 , tc3 , tc4 , tc5 ; //------total of a class SCORE *pr ; int n=0 ; pr = head ; tc1 = tc2 = tc3 = tc4 = tc5 = 0.0 ; while( pr != NULL ) { /*-=-=-=-Claculate total score of each class-=-=-=-*/ tc1 += pr->c1 ; tc2 += pr->c2 ; tc3 += pr->c3 ; tc4 += pr->c4 ; tc5 += pr->c5 ; pr = pr->next ; n++ ; } *ac1 = tc1 / n ; *ac2 = tc2 / n ; *ac3 = tc3 / n ; *ac4 = tc4 / n ; *ac5 = tc5 / n ; return n ; } /*-=-=-=-=-=-Function stu_ttl_aver-=-=-=-=-=-*/ /* This function will claculate average score and total score for each student */ /* This function returns number of the records */ int stu_ttl_aver( SCORE *head ) //------head of the link { SCORE *pr ; int n = 0 ; pr = head ; while( pr != NULL ) { /*-=-=-=-claculating total-=-=-=-*/ pr->total = pr->c1 + pr->c2 + pr->c3 + pr->c4 + pr->c5 ; /*-=-=-=-claculating average-=-=-=-*/ pr->aver = (float)pr->total / 5.0 ; n++ ; pr = pr->next ; } return n ; } /*-=-=-=-=-=-Function srch_max-=-=-=-=-=-*/ /* This function will search the record which has the max value of total score */ /* This function returns a SCORE pointer which points to the result */ SCORE *srch_max( SCORE *head ) //------head of the link { SCORE *pr ; int max ; pr = head ; max = -1 ; while( pr != NULL ) { /*-=-=-=-search max value-=-=-=-*/ if( pr->total > max ) max = pr->total ; pr = pr->next ; } pr = head ; while( pr != NULL ) { /*-=-=-=-search record-=-=-=-*/ if( pr->total == max ) break ; pr = pr->next ; } return pr ; } /*-=-=-=-=-=-Function srch_min-=-=-=-=-=-*/ /* This function will search the record which has the min value of total score */ /* This function returns a SCORE pointer which points to the result */ SCORE *srch_min( SCORE *head ) //------head of the link { SCORE *pr ; int min ; pr = head ; min = 9999 ; while( pr != NULL ) { /*-=-=-=-search min value-=-=-=-*/ if( pr->total < min ) min = pr->total ; pr = pr->next ; } pr = head ; while( pr != NULL ) { /*-=-=-=-search record-=-=-=-*/ if( pr->total == min ) break ; pr = pr->next ; } return pr ; }
/*-=-=-=-=-=-functon main-=-=-=-=-=-*/ int main( int argc , char *argv[] ) { FILE *fp ; //----------------------I/O file stream FLAG flag=0 ; //----------------------operations flag SCORE *head , *pr; int i , ndata , c1 , c2 , c3 , c4 , c5 ; char in_fname[FNAME_LEN] , //--------input file name out_fname[FNAME_LEN] , //------output file name name[NM_LEN] ; float ac1 , ac2 , ac3 , ac4 , ac5 ; head = NULL ; pr = NULL ; fp = NULL ; /*-=-=-=-check number of the parameters-=-=-=-*/ if( argc<2 ) { fprintf( stderr , %s -atxnlh [-i ] [-o ]\n , argv[0] ); fprintf( stderr , %s -h to get more help.\n , argv[0] ); exit(1); } strcpy( in_fname , _DFLT_INP ); strcpy( out_fname , _DFLT_OUT ); /*-=-=-=-get the parameters and mark the flag-=-=-=-*/ for( i=1 ; i { if( argv[i][0] != '-' ) { fprintf( stderr , Unknown argument %s .\n , argv[i] ); exit(1); } else { switch( argv[i][1] ) { case 'h' : _help(); exit(0); break ; case 'l' : flag |= LIST ; break ; case 'a' : flag |= AVER ; break ; case 'x' : flag |= MAX ; break ; case 'n' : flag |= MIN ; break ; case 'i' : flag |= INPUT ; i++ ; if( i >= argc ) break ; if( argv[i][0] != '-' ) { /*-=-=-=-get input file name-=-=-=-*/ strcpy( in_fname , argv[i] ); break ; } else { i-- ; break ; } case 'o' : flag |= OUTPUT ; i++ ; if( i >= argc ) break ; if( argv[i][0] != '-' ) { /*-=-=-=-get output file name-=-=-=-*/ strcpy( out_fname , argv[i] ); break ; } else { i-- ; break ; } default : fprintf( stderr , Unknown parameter %s .\n , argv[i] ); exit(1); break ; } } } /*-=-=-=-=-=-Input data-=-=-=-=-=-*/ if( (flag & INPUT) == INPUT ) { /*-=-=-=-opne input file stream-=-=-=-*/ fp = fopen( in_fname , r ); if( fp==NULL ) { fprintf( stderr , %s not found or not useable .\n , in_fname ); exit(1); } } if( (flag & INPUT) != INPUT ) { /*-=-=-=-input from keyboard-=-=-=-*/ while( printf( Now input students' score :\n ) ) { if( ( printf( Name : ) , scanf( %s , name)!=1 ) || ( printf( C1 : ) , scanf( %d , &c1 )!=1 ) || ( printf( C2 : ) , scanf( %d , &c2 )!=1 ) || ( printf( C3 : ) , scanf( %d , &c3 )!=1 ) || ( printf( C4 : ) , scanf( %d , &c4 )!=1 ) || ( printf( C5 : ) , scanf( %d , &c5 )!=1 ) ) break ; push( &head, name, c1, c2, c3, c4, c5 ) ; } } else { /*-=-=-=-input from file-=-=-=-*/ while( !feof( fp ) ) { if( fscanf( fp , %s , name ) != 1 || fscanf( fp , %d , &c1 )!=1 || fscanf( fp , %d , &c2 )!=1 || fscanf( fp , %d , &c3 )!=1 || fscanf( fp , %d , &c4 )!=1 || fscanf( fp , %d , &c5 )!=1 ) break ; push( &head, name, c1, c2, c3, c4, c5 ) ; } } if( (flag & INPUT) == INPUT ) { /*-=-=-=-close input file stream-=-=-=-*/ fclose( fp ); } /*-=-=-=-claculate total and average score for each student-=-=-=-*/ ndata = stu_ttl_aver( head ); if( (flag & OUTPUT) != OUTPUT ) { ; /*-=-=-=-Output to screen-=-=-=-*/ } else { /*-=-=-=-Output to file , open output file stream-=-=-=-*/ fp = fopen( out_fname , w ); if( fp==NULL ) { fprintf( stderr , %s not useable .\n , out_fname ); exit(1); } } if( (flag & LIST) == LIST ) { /*-=-=-=-make a list-=-=-=-*/ pr = head ; while( pr != NULL ) { if( (flag & OUTPUT) != 0 ) { /*-=-=-=-outout to file-=-=-=-*/ fprintf( fp , Name : %s\n , pr->name ); fprintf( fp , %3d %3d %3d %3d %3d\t%4d\t%3.2f\n , pr->c1 , pr->c2 , pr->c3 , pr->c4 , pr->c5 , pr->total , pr->aver ); } else { /*-=-=-=-output to screen-=-=-=-*/ printf( Name : %s\n , pr->name ); printf( %3d %3d %3d %3d %3d\t%4d\t%3.2f\n , pr->c1 , pr->c2 , pr->c3 , pr->c4 , pr->c5 , pr->total , pr->aver ); } pr = pr->next ; } } if( (flag & MAX) == MAX ) { /*-=-=-=-search max-=-=-=-*/ pr = srch_max( head ); if( (flag & OUTPUT) == OUTPUT ) { /*-=-=-=-output to file-=-=-=-*/ fprintf( fp , MAX : %s\n , pr->name ); fprintf( fp , %3d %3d %3d %3d %3d\t%4d\t%3.2f\n , pr->c1 , pr->c2 , pr->c3 , pr->c4 , pr->c5 , pr->total , pr->aver ); } else { /*-=-=-=-output to screen-=-=-=-*/ printf( MAX : %s\n , pr->name ); printf( %3d %3d %3d %3d %3d\t%4d\t%3.2f\n , pr->c1 , pr->c2 , pr->c3 , pr->c4 , pr->c5 , pr->total , pr->aver ); } } if( (flag & MIN) == MIN ) { /*-=-=-=-search min-=-=-=-*/ pr = srch_min( head ); if( (flag & OUTPUT) == OUTPUT ) { /*-=-=-=-output to file-=-=-=-*/ fprintf( fp , MIN : %s\n , pr->name ); fprintf( fp , %3d %3d %3d %3d %3d\t%4d\t%3.2f\n , pr->c1 , pr->c2 , pr->c3 , pr->c4 , pr->c5 , pr->total , pr->aver ); } else { /*-=-=-=-output to screen-=-=-=-*/ printf( MIN : %s\n , pr->name ); printf( %3d %3d %3d %3d %3d\t%4d\t%3.2f\n , pr->c1 , pr->c2 , pr->c3 , pr->c4 , pr->c5 , pr->total , pr->aver ); } } if( (flag & AVER) == AVER ) { /*-=-=-=-claculate average value for each class-=-=-=-*/ class_aver( head , &ac1 , &ac2 , &ac3 , &ac4 , &ac5 ); if( (flag & OUTPUT) == OUTPUT ) { /*-=-=-=-output to file-=-=-=-*/ fprintf( fp , Average class :\n ); fprintf( fp , %3.2f\t%3.2f\t%3.2f\t%3.2f\t%3.2f\n , ac1 , ac2 , ac3 , ac4 , ac5 ); } else { /*-=-=-=-output to screen-=-=-=-*/ printf( Average class :\n ); printf( %3.2f\t%3.2f\t%3.2f\t%3.2f\t%3.2f\n , ac1 , ac2 , ac3 , ac4 , ac5 ); } } if( (flag & OUTPUT)==OUTPUT ) { /*-=-=-=-close output file stream-=-=-=-*/ fclose( fp ); } /*-=-=-=-delete link to clear memory-=-=-=-*/ while( head->next != NULL ) { pr = head->next ; free( (void *)head ); head = pr ; } free( (void *)head ); return 0; } /*-=-=-=-end of ssm.c-=-=-=-*/
贴main函数那部分的时候居然说我贴子里有禁用词!我晕。 查了半天,原来是这么回事。我用的一个变量flag,原来是写的缩写,被当成禁用词了。 继续。下面是测试数据: SyuuI 100 90 80 97 88 FukataKyouko 99 78 96 99 60 MatudaJyonko 89 90 67 89 95 SakuraiHanamiti 78 98 94 79 100 下面是Makefile CC = gcc TAR = ssm BINPATH = $(HOME)/bin PROG: .c.o $(CC) $(TAR).o -o $(TAR) .c.o: $(CC) $(TAR).c -c -o $(TAR).o clean: rm -f core* *~ *.o binclean: rm -f $(TAR) install: cp $(TAR) $(BINPATH)