A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

结构体跟类有很多共同之处
当然可能类 更简便一些。
这是我自己写的程序
单独写一个set和get方法 就可以当做类来使用了
  1. #include <stdio.h>
  2. typedef struct{
  3.                 int no;
  4.                 char name[20];
  5.                 int score[3];
  6. }Student;
  7. typedef struct{
  8.         Student stu1;
  9.         Student stu2;
  10.         Student stu3;
  11.         Student stu4;
  12.         Student stu5;
  13.         Student stu6;
  14.         Student stu7;
  15.         Student stu8;
  16.         Student stu9;
  17.         Student stu10;
  18. }Students;
  19. //Student stu;
  20. //Student *pStudent=&stu;

  21. Student * student_input(Student *pStudent);
  22. void student_print(const Student *pStudent);
  23. double student_average(const Student *pStudent);
  24. int student_get_score(const Student *pStudent, int index);
  25. double students_average(const Students *pstudents);

  26. int main (int argc, char const *argv[])
  27. {
  28.         int average=0.0;
  29.         Student stu={0,"",{0,0,0}};
  30.         student_input(&stu);
  31.         average=student_average(&stu);
  32.         student_print(&stu);
  33.        
  34.        
  35.        
  36.         return 0;
  37. }
  38. Student * student_input(Student *pStudent){
  39.         scanf ("%d %s %d %d %d",&pStudent->no,&pStudent->name,
  40.         &pStudent->score[0],&pStudent->score[1],&pStudent->score[2]);
  41.         return pStudent;
  42. }
  43. void student_print(const Student *pStudent){
  44.         double average        ;
  45.         average=(pStudent->score[0]+pStudent->score[1]+pStudent->score[2])/3.0;
  46.         printf ("%d\t%s\t%d\t%d\t%d\t%f\n",pStudent->no,pStudent->name,
  47.         pStudent->score[0],pStudent->score[1],pStudent->score[2],average);
  48. }
  49. double student_average(const Student *pStudent){
  50.         double average=0.0;
  51.         average=(pStudent->score[0]+pStudent->score[1]+pStudent->score[2])/3.0;
  52.         return average;
  53. }
  54. int student_get_score(const Student *pStudent, int index){
  55.         int a=0;
  56.         a=pStudent->score[index];
  57.         return a;
  58. }
  59. double students_average(const Students *pstudents,int index){
  60.         double students_average;
  61.         students_average=(pstudents->stu1.score[index]+pstudents->stu2.score[index]+pstudents->stu3.score[index]+pstudents->stu4.score[index]+pstudents->stu5.score[index]+pstudents->stu6.score[index]+pstudents->stu7.score[index]+pstudents->stu8.score[index]+pstudents->stu9.score[index]+pstudents->stu10.score[index])/10.0;
  62.         return students_average;
  63. }
  64. void max_score(const Student *pstudent,int index ){
  65.         int max_score=0;
  66.         int i
  67.         for (i=1;i<=10;i++){
  68.                 if (max_score<=*pstudent->score[index]){
  69.                         max_score=*pstudent->score[index];
  70.                 }
  71.                 pstudent++;
  72.         }
  73.         printf("%d",max_score);
  74. }
复制代码

3 个回复

正序浏览
学习了  
回复 使用道具 举报
OC的本质就是把C给封装了啊,底层还是面向对象的啊!
回复 使用道具 举报
因为面向对象是建立在面向过的基础上的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马