赞
踩
不知道是我理解错了,还是这个题目有问题,主函数使用菜单调用函数,是可以这样写没错,但是,这整个程序都是连接起来的,就比方说,你不输入学生信息,怎么求平均分数呢,不输入,哪里来的输出呢。那这样的话,这个菜单根本就没啥用处啊,只能从2开始输入
#include<stdio.h> struct CLASS { int g1 ; int g2 ; int g3 ; int a ; } ; struct CLASS grades ; struct STUDENT { int num ; char name[20] ; struct CLASS grades ; } ; struct STUDENT str[10] ; void print ( struct STUDENT str[] )/*输出所有学生的信息*/ { for ( int i = 0 ; i < 10 ; i ++ ) { printf("%d\t%s\t%d\t%d\t%d\t%d\n",str[i].num , str[i].name , str[i].grades.g1 , str[i].grades.g2 , str[i].grades.g3 , str[i].grades.a ) ; } } void sort ( struct STUDENT str[] )/*用平均成绩排序*/ { struct STUDENT max ; for ( int i = 0 ; i < 9 ; i ++ ) { for ( int j = i + 1 ; j < 10 ; j ++ ) { if ( str[i].grades.a < str[j].grades.a ) { max = str[j] ; str[j] = str[i] ; str[i] = max ; } } } print ( str ) ; } void aver ( struct CLASS grades )/*求平均分数*/ { for ( int i = 0 ; i < 10 ; i ++ ) { str[i].grades.a = ( str[i].grades.g1 + str[i].grades.g2 + str[i].grades.g3 ) / 3 ; } sort ( str ) ; } void input ( struct STUDENT str[] )/*输入数据*/ { for ( int i = 0 ; i < 10 ; i ++ ) { scanf("%d %s %d %d %d",&str[i].num , str[i].name , &str[i].grades.g1 , &str[i].grades.g2 , &str[i].grades.g3 ) ; } aver ( grades ) ; } int main () { int a ; printf("请输入你想调用函数的序号:") ; scanf("%d",&a); switch ( a ) { case 2 : input ( str ) ; break ; case 3 : aver ( grades ) ; break ; case 4 : sort ( str ) ; break ; case 5 : print ( str ) ; break ; } return 0; }
//输入:
2
1 q 1 1 1
2 w 2 2 2
3 e 3 3 3
4 r 4 4 4
5 t 5 5 5
6 y 6 6 6
7 u 7 7 7
8 i 8 8 8
9 o 9 9 9
10 p 10 10 10
//输出:
10 p 10 10 10 10
9 o 9 9 9 9
8 i 8 8 8 8
7 u 7 7 7 7
6 y 6 6 6 6
5 t 5 5 5 5
4 r 4 4 4 4
3 e 3 3 3 3
2 w 2 2 2 2
1 q 1 1 1 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。