第六次实验

Part 1

1.1.2

#include <stdio.h>

const int N=5;

// 定义结构体类型struct student,并定义STU为其别名 
typedef struct student {
    long no;
    char name[20];
    int score;     
}STU;

// 函数声明 
void input(STU s[], int n);
int findMinlist(STU s[], STU t[], int n);
void output(STU s[], int n);

int main() {
    STU stu[N], minlist[N];
    int count;
    
    printf("录入%d个学生信息\n", N);
    input(stu, N);
    
    printf("\n统计最低分人数和学生信息...\n");
    count = findMinlist(stu, minlist, N);
    
    printf("\n一共有%d个最低分,信息如下:\n", count);
    output(minlist, count);
     
    return 0;
} 

// 输入n个学生信息,存放在结构体数组s中 
void input(STU s[], int n) {
    int i;
    for(i=0; i<n; i++) 
        scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score);
} 

// 输出结构体s中n个元素信息
void output(STU s[], int n) {
    int i;
    for(i=0; i<n; i++)
        printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score); 
} 

// 在结构体数组s中,查找最低分学生的记录,将其存入结构体数组s中
// 形参n是结构体数组s中元素个数
// 函数返回最低分的学生人数 
int findMinlist(STU s[], STU t[], int n) {
    // 补足函数实现
    int i,j=0;
    int min;
    min=s[0].score;
    for(i=0;i<n;i++){
        if(s[i].score<min){
            min=s[i].score;
        }
    }
    for(i=0;i<n;i++){
        if(min==s[i].score){
            t[j++]=s[i];
        }
    }
    return j;
    
} 

 

1.3

#include <stdio.h>
  #include <string.h>
  const int N = 10;
  
  // 定义结构体类型struct student,并定义其别名为STU 
  typedef struct student {
      long int id;
      char name[20];
      float objective;    /*客观题得分*/
     float subjective;    /*操作题得分*/
     float sum;
     char level[10];     }STU; 
 
 // 函数声明
 void input(STU s[], int n);
 void output(STU s[], int n);
 void process(STU s[], int n); 
 int main() {
     STU stu[N];
     
     printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 
     input(stu, N);
     
    printf("\n对考生信息进行处理: 计算总分,确定等级\n");
     process(stu, N);
    
     printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n");
     output(stu, N); 
     
     return 0;
 } 
 
 // 录入考生信息:准考证号,姓名,客观题得分,操作题得分
 void input(STU s[], int n) {
     // 补足代码
     // ××× 
     int i;
     for(i=0;i<n;i++){
         scanf("%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
     }
 }
 
 //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 void output(STU s[], int n) {
     // 补足代码
     // ××× 
     int i;
     for(i=0;i<n;i++){
         printf("%ld %s %f %f %f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
     }
 }
 
 // 对考生信息进行处理:计算总分,排序,确定等级
 void process(STU s[], int n) {
     // 补足代码
     // ××× 
     int i,j;
     STU t;
     //计算总分 
     for(i=0;i<n;i++){
         s[i].sum=s[i].objective+s[i].subjective;
     }
     //冒泡法排序 
     for(i=0;i<n-1;i++){
         for(j=0;j<n-1-i;j++){
             if(s[j].sum<s[j+1].sum){
                 t=s[j];
                 s[j]=s[j+1];
                 s[j+1]=t;
             }    
         }
     }
     //确定等级
     strcpy(s[0].level,"优秀");
     for(i=1;i<=4;i++){
         strcpy(s[i].level,"合格");
     } 
     for(i=5;i<=9;i++){
         strcpy(s[i].level,"不合格");
     }
 }

 

Part 2

共用体与结构体类型的区别

共用体:几个不同类型的变量共占一段内存。

结构体:内部数据成员在内存中连续存放,每个成员分别占有独立的存储空间。

 

Part 3

枚举类型适用于变量取值有限的情形。

使用注意事项:

1.枚举类型不能直接输入输出。

2.枚举类型可以隐含转换为整形,但整形转换为枚举类型必须显式转换。

 

posted @ 2019-06-10 22:08  qinyaqi  阅读(124)  评论(1编辑  收藏  举报