# 結構體數組的定義和引用（二）

void order(arr,n)
struct stu arr[];
int n;
{ struct stu temp;
int i,j,x,y;
f o r ( i = 0 ; i < n - 1 ; i + + )
f o r ( j = 0 ; j < n - 1 - i ; j + + )
if (arr[j].score[3]>arr[j+1].score[3])
{ temp=arr[j]; /結*構體類型變數不允許以整體輸入或輸出，但允許相互賦值*/
arr[j]=arr[j+1]; /*進行交換* /
a r r [ j + 1 ] = t e m p ;
}
}

void output(arr,n) /*以表格形式輸出有n個元素的結構體類型數組各成員* /
int n;
struct stu arr[];
{int i,j;
printf("********************TABLE********************\n")打;印 /表*頭*/
printf("----------------------------------------------------\n");
/ *輸出一條水平線* /
p r i n t f ( " | % 1 0 s | % 8 s | % 7 s | % 7 s | % 7 s | % 7 s | \ n " , " N a m e " , " N u m b e r " , " E n g l i s h " , " M a t h e m a " ,
" p h y s i c s " , " a v e r a g e " ) ;
/ * 輸出效果為：| Name| Number|English|Mathema|Physics|Average|*/
p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n " ) ;
for (i=0;i<N;I++)
{
p r i n t f ( " | % 1 0 s | % 8 l d | " , a r r [ i ] . n a m e , a r r [ i ] . n u m b e r ) ; / * 輸出姓名、學號* /
f o r ( j = 0 ; j < 4 ; j + + )
p r i n t f ( " % 7 . 2 f | " , a r r [ i ] . s c o r e [ j ] ) ; / * 輸出三科成績及三科的平均* /
p r i n t f ( " \ n " ) ;
p r i n t f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n " ) ;
}
}

void out_row(arr,n) / *對n個元素的結構體類型數組求單項平均* /
int n;
struct stu arr[];
{
float row[4]={0,0,0,0};/ *定義存放單項平均的一維數組* /
int i,j;
f o r ( i = 0 ; i < 4 ; i + + )
{
f o r ( j = 0 ; j < n ; j + + )
r o w [ i ] = r o w [ i ] + a r r [ j ] . s c o r e [ i ] ; / * 計算單項總和* /
row[i]=row[i]/n; 計/*算單項平均*/
}
printf("|%19c|",' '); 按/表* 格形式輸出*/
for (i=0;i<4;i++)
p r i n t f ( " % 7 . 2 f | " , r o w [ i ] ) ;
p r i n t f ( " \ n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n " ) ;
}

#include
#include
struct stu
{
char name[20];
long number;
float score[4];
} ;
m a i n ( )
{
void input(); / *函式宣告* /
void aver();
void order();
void output();
void out_row();
struct stu stud[4]; / * 定義結構體數組* /
float row[3];
i n p u t ( s t u d , 4 ) ; / *依此調用自訂函數* /
a v e r ( s t u d , 4 ) ;
o r d e r ( s t u d , 4 ) ;
o u t p u t ( s t u d , 4 ) ;
o u t _ r o w ( s t u d , 4 ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
void input(arr,n)
struct stu arr[];
int n;
{ int i,j;
char temp[30];
for (i=0;i<N;I++)
{
printf("\nInput Name,Number,English,Mathema,Physic\n");
g e t s ( a r r [ i ] . n a m e ) ;
g e t s ( t e m p ) ;
a r r [ i ] . n u m b e r = a t o l ( t e m p ) ;
f o r ( i = 0 ; i < 4 ; i + + )
{
f o r ( j = 0 ; j < n ; j + + )
r o w [ i ] = r o w [ i ] + a r r [ j ] . s c o r e [ i ] ; / * 計算單項總和* /
row[i]=row[i]/n; 計/*算單項平均*/
}
printf("|%19c|",' '); 按/表* 格形式輸出*/
for (i=0;i<4;i++)
p r i n t f ( " % 7 . 2 f | " , r o w [ i ] ) ;
p r i n t f ( " \ n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n " ) ;
}

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD