程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於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);  
}

 

 \
 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved