当前位置:编程学习 > C/C++ >>

问题一百三十八:指针操作学生结构体

[plain]  #include <stdio.h> 
#include <stdlib.h> 
 
void print(struct students *p);        //打印函数print的定义  
void max_score(struct students *p);    //函数max的定义  
 
struct students   //结构体students的定义  

       char   number[15]; 
       float score; 
}; 
 
int main(int argc, char *argv[]) 

      struct students num[5]={{"121", 88.8}, {"122", 77.7}, {"123", 48.4}, {"124", 99.1}, {"125", 78.3}}; 
                           
      print(num); 
      max_score(num); 
 
      system("PAUSE");   
      return 0; 

 
//  Definition of function 
 
//**print 
void print(struct students *p) 

      int i; 
       
      i=0; 
       
      while(i<5) 
      { 
           printf("%d student scored is %0.2f\n", i+1, p[i]);    
           i+=2;     
      }      

 
// ** max_score 
void max_score(struct students *p) 

     struct students t;    // 用于保存最大数  
     int i;  
     int j; 
       
     for(i=0; i<4; i++)                //按分数排序 
     { 
         for(j=0; j<5; j++) 
         { 
             if( p[i].score> p[j].score)     
              { 
                   t= p[i];          //学号和分数也交换 
                   p[i]=p[j]; 
                   p[j]=t; 
              }          
         }          
     }  
      
     printf("The highest score is %f, and he number is %s\n", p[0].score, p[0].number);    

#include <stdio.h>
#include <stdlib.h>

void print(struct students *p);        //打印函数print的定义
void max_score(struct students *p);    //函数max的定义

struct students   //结构体students的定义
{
       char   number[15];
       float score;
};

int main(int argc, char *argv[])
{
      struct students num[5]={{"121", 88.8}, {"122", 77.7}, {"123", 48.4}, {"124", 99.1}, {"125", 78.3}};
                         
      print(num);
   max_score(num);

      system("PAUSE"); 
      return 0;
}

//  Definition of function

//**print
void print(struct students *p)
{
      int i;
     
      i=0;
     
      while(i<5)
      {
           printf("%d student scored is %0.2f\n", i+1, p[i]);  
           i+=2;   
      }    
}

// ** max_score
void max_score(struct students *p)
{
     struct students t;    // 用于保存最大数
     int i;
     int j;
     
     for(i=0; i<4; i++)                //按分数排序
     {
         for(j=0; j<5; j++)
         {
    if( p[i].score> p[j].score)   
              {
                   t= p[i];          //学号和分数也交换
       p[i]=p[j];
       p[j]=t;
              }        
         }        
     }
    
     printf("The highest score is %f, and he number is %s\n", p[0].score, p[0].number);  
}

 

 \
 

 

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,