12.11函数 结构体 多文件编译

发布于:2024-12-18 ⋅ 阅读:(42) ⋅ 点赞:(0)

 1.脑图

定义一个数组,用来存放从终端输入的5个学生的信息【学生的信息包含学生的姓名、年纪、性别、成绩】

1>封装函数 录入5个学生信息

2>封装函数 显示学生信息

3>封装函数 删除第几个学生信息,删除后调用显示学生信息函数 显示

4> 封装函数 按照学生的成绩 进行降序,降序后调用显示学生信息函数 显示

要求:多文件编译完成。

头文件、源文件、测试文件(mian)

1. test.头文件

#ifndef  __TEST_H__
#define  __TEST_H__

struct student {
    char name[30];
    int age;
    char sex[10];
    float score;
};

void input_data(struct student list[],int n);
void show_data(struct student list[],int n);
//void delete_data()
void sort(struct student list[],int n);

#endif

2.test.c文件 

#include <stdio.h>
#include "test.h"
void input_data(struct student list[],int n)
{
	for(int i=0;i<n;i++)
	{
        printf("enter the %d name ,age,sex,score:\n",i+1);
		scanf("%s %d %s %f",list[i].name,&list[i].age,list[i].sex,&list[i].score);
	}

}
void show_data(struct student list[],int n)
{
	for(int i=0;i<n;i++)
	{
		printf("name:%s age:%d  sex:%s  score:%f\n",list[i].name,list[i].age,list[i].sex,list[i].score);
	}

}
//void delete_data()


void sort(struct student list[],int n)
{
	for (int i=0;i<n;i++)
	{
		for(int j=0;j<n-1-i;j++)
		{
			if (list[j].score<list[j+1].score)
			{ struct student temp=list[j];
				list[j]=list[j+1];
				list[j+1]=temp;
			}
		}
	}
}

3.main.c 

#include <stdio.h>
#include "test.h"

int main(int argc, const char *argv[])
{
	struct student list[5];

	printf("录入学生信息:\n");
    input_data(list,5);

	printf("显示学生信息\n");
	show_data(list,5);



	printf("按照成绩排序\n");
	sort(list,5);
	show_data(list,5);

	return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到