【无标题】

  1. 从堆区申请能存5个结构体变量的数组的空间,完成数组中成员的输入,根据学生成绩,用选择排序的方式,对学生排序并输出。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct 
{
    char name[20];
    float score;
}stu;

void input(stu p[],int n);
void paixu(stu p[],int n);
void output(stu p[],int n);

int main(int argc, const char *argv[])
{
   stu *p=(stu*)malloc(sizeof(stu)*5);
   input(p,5);
   paixu(p,5);
   output(p,5);
    return 0;
}

void input(stu p[],int n)
{
    for(int i=0;i<n;i++)
    {
        printf("请输入第%d个学生姓名:",i+1);
        scanf("%s",p[i].name);
         printf("请输入第%d个学生成绩:",i+1);
        scanf("%f",&p[i].score);
    }

}
void paixu(stu p[],int n)
{
    for(int i=0;i<n-1;i++)
    {
        for (int j=i;j<n-1;j++ )
        {
            if(p[i].score>p[j+1].score)
            {
                int temp=p[j+1].score;
                p[j+1].score=p[i].score;
                p[i].score=temp;
            }
        }    
    }
}
void output(stu p[],int n)
{
    for(int i=0;i<n;i++)
        printf("姓名:%s 成绩:%f\n",p[i].name,p[i].score);
}
 

  1. 求以下结构体的大小
typedef struct {
    int id;         //4
    char name[50];  //50
    char grade[3];  //3    //3
} Student;  //60

typedef struct {
    int id;           //4
    char name[50];    //50   //2
    Student student;  //60   //4
} Teacher; //120

typedef struct {
    int id;
    char name[50];
    Teacher teacher;
} Course;

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值