C

数据结构-C复习-学生成绩表-数组

Array based student list

Posted by xuepro on November 9, 2017

原理请观看数据结构视频课程的“网易云课堂”“腾讯课堂”

版本1 (// copyright by hwdong)

#include "cstring"
#include "malloc.h"
#include "cstdio"


typedef struct{
   char name[30];
   float score;
} student;


int main(){
  student stus[100];
  int i = 0,j = 0,k=0 ;
  do{ 
      printf("请输入学生姓名和分数:\n");
      scanf("%s", stus[i].name);
      scanf("%f", &(stus[i].score)); 
      if(stus[i].score>=60)  j++;     
  }while(stus[i++].score>=0);
  i--;

  for(k=0;k<i;k++){
     printf("name:%s  score:%3.2f\n",
           stus[k].name, stus[k].score);
  }
  printf("num of passed:%d\n",j);
}

版本2 (// copyright by hwdong)

#include "cstring"
#include "malloc.h"
#include "cstdio"


typedef struct{
   char name[30];
   float score;
} student;


void In_student(student &s){
   scanf("%s",s.name);
   scanf("%f",&(s.score));
}
void Out_student(const student s){
  printf("name:%s  score:%3.2f\n",
           s.name, s.score);
}


int main(){
  student stus[100];
  int i = 0,j = 0,k=0 ;
  do{
     printf("请输入学生姓名和分数:\n");
     In_student(stus[i]); 
     if(stus[i].score>=60)  j++;   
  }while(stus[i++].score>=0);
  i--;

  for(k=0;k<i;k++)
   Out_student(stus[k]);

  printf("num of passed: %d\n",j);
}

版本3 (// copyright by hwdong)

#include "cstring"
#include "malloc.h"
#include "cstdio"


typedef struct{
   char name[30];
   float score;
} student;


void In_student(student &s){
   scanf("%s",s.name);
   scanf("%f",&(s.score));
}
void Out_student(const student s){
  printf("name:%s  score:%3.2f\n",
           s.name, s.score);
}

const int INITSIZE = 33;
const int INC = 30;

int main(){
  int size = INITSIZE; 
  int i = 0,j = 0,k=0 ;
  student *stus = (student *)malloc(size*sizeof(student));
  do{
      if(i>=size){
          student *stus_new =(student *)realloc(stus,(size+INC)*sizeof(student));
          if(stus_new) {
              free(stus); stus = stus_new;
          }
          else{
              //...
          }
      }
      printf("请输入学生姓名和分数:\n");
      In_student(stus[i]); 
      if(stus[i].score>=60)  j++;   
  }while(stus[i++].score>=0);
  i--;

  for(k=0;k<i;k++)   Out_student(stus[k]);
  printf("num of passed:%d\n",j); free(stus);
}

支付宝打赏 微信打赏

您的打赏是对我最大的鼓励!