码迷,mamicode.com
首页 > 其他好文 > 详细

实验六

时间:2019-06-11 00:58:54      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:变量   共用体   自定义   声明   include   实现   float   png   sum   

part 1
ex 1
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,k=0,min;
 min=s[0].score;
  for(i=1;i<n;i++)
 if(s[i].score<min)
  min=s[i].score; 
   for(i=0;i<n;i++)
 if(s[i].score==min)
 t[k++]=s[i];
 return k;
    
} 

技术图片

 ex 2

#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 %.2f %.2f %.2f %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 temp;
for(i=0;i<n-1;i++)
    s[i].sum=s[i].objective+s[i].subjective;
for(i=0;i<n-1;i++){
    for(j=0;j<n-i-1;j++){    
    if(s[j+1].sum>s[j].sum){
         temp=s[j];
         s[j]=s[j+1];
         s[j+1]=temp;
         
    }
  }
}
 
for(i=0;i<n;i++){
    if(i==0)
    strcpy(s[i].level,"优秀");
    else if(1<=i<=4)
    strcpy(s[i].level,"合格");
    else
    strcpy(s[i].level,"不合格");
    } 
    
}

技术图片

part 2

结构是用户自定义的类型,比数组更灵活,同一个结构可以存储多种类型的数据。

结构的创建包括两步,首先定义结构描述---描述并标记了能够存储在结构中的各种数据类型。

共用体也是一种数据格式,它能够存储不同的数据类型,但只能同时存储其中的一种类型。也就是说,结构可以同时存储int、long和double,共用体只能存储int、long或者double中的一种。共用体的句法与结构类似,但含义不同。

part 3

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

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

枚举类型可以隐含转换为整型,但整型转换为枚举类型,必须显示转换。

附;

https://www.cnblogs.com/0522GY1025/p/11001199.html

https://www.cnblogs.com/74520qslm/p/11001090.html

https://www.cnblogs.com/xh66/p/11000858.html

实验六

标签:变量   共用体   自定义   声明   include   实现   float   png   sum   

原文地址:https://www.cnblogs.com/mgl1999/p/11001234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!