C/PTA —— 15.结构体2(课内实践) 7-1 计算职工工资 7-2 计算平均成绩 7-3 找出总分最高的学生 7-4 通讯录的录入与显示
7-1 计算职工工资
# include <stdio.h>
# include <stdlib.h>
typedef struct GZ
{ char name[ 6 ] ; double j; double f; double z; double s;
} GZ; int main ( )
{ int n = 0 ; scanf ( "%d" , & n) ; GZ gz[ 100 ] ; for ( int i = 0 ; i < n; i++ ) { scanf ( "%s %lf %lf %lf" , gz[ i] . name, & gz[ i] . j, & gz[ i] . f, & gz[ i] . z) ; } for ( int i = 0 ; i < n; i++ ) { gz[ i] . s = ( gz[ i] . j + gz[ i] . f) - gz[ i] . z; printf ( "%s %.2lf" , gz[ i] . name, gz[ i] . s) ; } return 0 ;
}
7-2 计算平均成绩
# include <stdio.h>
# include <stdlib.h>
struct student
{ char num[ 20 ] ; char nam[ 20 ] ; int g;
} ;
int main ( )
{ struct student st[ 10 ] ; int n; int i; float sum, aver; scanf ( "%d" , & n) ; for ( i = 0 ; i < n; i++ ) { scanf ( "%s %s %d" , & st[ i] . num, & st[ i] . nam, & st[ i] . g) ; sum += st[ i] . g; } aver = sum / n; printf ( "%.2f\n" , aver) ; for ( i = 0 ; i < n; i++ ) { if ( st[ i] . g < aver) { printf ( "%s %s\n" , st[ i] . nam, st[ i] . num) ; } }
}
7-3 找出总分最高的学生
# include <stdio.h>
# include <stdlib.h>
struct student { char num[ 6 ] ; char name[ 11 ] ; int score1; int score2; int score3;
} ;
int main ( )
{ int i, n; struct student * p; scanf ( "%d" , & n) ; p = ( struct student * ) malloc ( sizeof ( struct student ) ) ; for ( i = 0 ; i < n; i++ ) { scanf ( "%s %s %d %d %d" , p[ i] . num, p[ i] . name, & p[ i] . score1, & p[ i] . score2, & p[ i] . score3) ; } int s[ 20 ] ; for ( i = 0 ; i < n; i++ ) { s[ i] = p[ i] . score1 + p[ i] . score2 + p[ i] . score3; } int max, j; max = s[ 0 ] ; for ( i = 0 ; i < n; i++ ) { if ( s[ i] >= max) { max = s[ i] ; j = i; } } printf ( "%s %s %d" , p[ j] . name, p[ j] . num, max) ; free ( p) ; return 0 ; }
7-4 通讯录的录入与显示
# include <stdio.h>
# include <stdlib.h>
int main ( ) { struct inf { char name[ 20 ] ; char birthday[ 20 ] ; char sex[ 10 ] ; char tel[ 20 ] ; char num[ 20 ] ; } ; int N = 0 ; scanf ( "%d" , & N) ; struct inf list[ N] ; int i = 0 ; for ( ; i < N; i++ ) { scanf ( "%s %s %s %s %s\n" , & list[ i] . name, & list[ i] . birthday, & list[ i] . sex, & list[ i] . tel, & list[ i] . num) ; } int k = 0 ; scanf ( "%d" , & k) ; i = 0 ; int q[ 15 ] = { 0 } ; for ( ; i < k; i++ ) { scanf ( "%d" , & q[ i] ) ; } i = 0 ; for ( ; i < k; i++ ) { if ( q[ i] >= 0 && q[ i] < N) printf ( "%s %s %s %s %s\n" , list[ q[ i] ] . name, list[ q[ i] ] . tel, list[ q[ i] ] . num, list[ q[ i] ] . sex, list[ q[ i] ] . birthday) ; else printf ( "Not Found\n" ) ; } return 0 ; }