譚浩強C程式設計(第四版)例題中的代碼最佳化

來源:互聯網
上載者:User

標籤:

eg:9.7 有n個結構體變數,內含學生學號,姓名和3門課成績,要求輸出平均成績最高的學生的資訊(內含學生學號,姓名和3門課成績和平均成績)。

2015-01-2022:25:34

 1 #include<stdio.h> 2 #define N 2 3  4 struct Student 5 { 6     int num; 7     char name[20]; 8     float score[3]; 9     float aver;10 };11 12 void input(struct Student *stu)13 {14     int i;15     printf("input the student‘s information:\nname,num,three score\n");16     for(i=0;i<N;i++,stu++)17     {18         scanf("%s %d %f %f %f",stu->name,&stu->num ,19             &stu->score[0],&stu->score[1],&stu->score[2]);20         stu->aver=(stu->score[0]+stu->score[1]+stu->score[2])/3;21     }22 }23 24 struct Student *Max(struct Student *stu)25 {26     int i;27     int m=0;28     for(i=0;i<N;i++)29     {30         if(stu[i].aver>stu[m].aver)31         {32             m=i;33         }34     }35     return &stu[m];36 }37 38 void print_stu(struct Student *stud)39 {40     printf("\nThe average scores of the top students is:\n");41     printf("num:%d\nname:%s\nthe three subject:%5.1f,%5.1f,%5.1f\nthe aver score:%6.2f\n",stud->num,stud->name,stud->score[0],stud->score[1],stud->score[2],stud->aver);    42 }43 44 int main()45 {46     struct Student stu[N];47     struct Student *p = stu;48     input(p);49     print_stu(Max(p));50     return 0;51 }

 

譚浩強C程式設計(第四版)例題中的代碼最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.