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

实验六

时间:2019-06-11 01:03:31      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:string   dmi   include   信息保存   struct   oat   输出   scan   col   

Part 1

  ex1-2

在结构体数组stu中查找最低分,查找成绩与最低分相同的学生,将其信息保存在数组t中,返回最低分学生个数。

#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;
}

运行结果如下:

技术图片

ex1-3

#include <stdio.h>
#include <string.h>
const int N = 10;

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("%d %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("%5d %10s %5.1f %5.1f %5.1f %10s\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,k,m=0;
    STU temp;
    for(i=0;i<n;i++)
        s[i].sum=s[i].objective+s[i].subjective;
    for(j=0;j<n;j++){
        for(k=j;k<n;k++){
            if(s[j].sum<s[k].sum){
                temp=s[j];
                s[j]=s[k];
                s[k]=temp;
            }
        }
    }
    while(m<=n){
        if(m<=n*0.1-1)
        strcpy(s[m].level,"优秀");
        else if(m>n*0.1-1&&m<=n*0.5-1)
        strcpy(s[m].level,"合格");
        else
        strcpy(s[m].level,"不合格"); 
        m++;
    };    
}

技术图片

Part2:共用体类型及编程示例

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

1.共用体类型在于变量共占一段内存,然而结构体类型在于每个变量占有不同的内存空间;

2.结构体占用的内存为各变量各自所占内存之和,而共用体类型则是其中一个变量所占最大内存。

Part3:枚举类型及编程示例

枚举类型用于描述哪一类数据?

枚举类型用于描述整型常量,且不能直接输出;int类型数值赋值给枚举类型变量,必须强制类型转换,反过来可以直接赋值。

 

 

 

 

以上。

 

实验六

标签:string   dmi   include   信息保存   struct   oat   输出   scan   col   

原文地址:https://www.cnblogs.com/txaalo/p/11001259.html

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