2021-05-22

线性表——>顺序表实现一个简单成绩表存储:

2021-05-22_第1张图片

 

#include
#include
#include
#define MAXSIZE 10
#pragma warning(disable:4996)//解决VS对strcpy,scanf的安全警告

//定义学生的存储信息
struct student
{
	char ID[10] ;
	char name[20];
	int linear;
	int physics;
	int datastru;
	int aver;
}; typedef struct student student;

//创建顺序表
struct list
{
	student* stu=NULL;
	int len=0;
	int maxsize=0;
 }; typedef struct list list;

//初始化顺序表
void initlist(list *l)
{
	l->maxsize= MAXSIZE;   //以及确定数据规模不会超过MAXSIZE,故此处不判断是否存储量超过MAXSIZE
	l->stu = (student*)malloc(10*sizeof(student));
	l->len = 0;
}

//求三科成绩的均值(结构体内不能做运算,为方便均值计算,单独定义此函数)
int aver(int a,int b, int c) {
	int aver = (int)((a + b + c) / 3);
	return aver;
}

//将学生信息插到第k号位置
int insertList(list* l, student s,int k=0) {
	if (k<0 || k>l->len+1)printf("输入错误的位置");
	//如果线性表已满,则将其空间扩充一倍
	
	if (l->len == l->maxsize - 1) {
		l->stu = (student*)realloc(l->stu, 2 * sizeof(student) * l->maxsize);
		if (!l->stu) {
			printf("动态存储空间不足。\n"); exit(1);
		}
		l->maxsize = 2 * l->maxsize;
	}
	//规定输入k=0 or len+1为将元素插到表尾
  
  if (k == 0 || k == l->len + 1) {
		strcpy(l->stu[l->len].ID , s.ID);
		strcpy(l->stu[l->len].name , s.name);
		l->stu[l->len].linear = s.linear;
		l->stu[l->len].physics = s.physics;
		l->stu[l->len].datastru = s.datastru;
		l->stu[l->len].aver = aver(s.linear, s.datastru, s.physics);
		l->len++;

	}
	else {
		for (int i = l->len; i >= k; i--) {
			//将i位置元素后移一位
			strcpy(l->stu[i].ID, l->stu[i - 1].ID);
			strcpy(l->stu[i].name, l->stu[i - 1].name);
			l->stu[i].linear = l->stu[i - 1].linear;
			l->stu[i].physics = l->stu[i - 1].physics;
			l->stu[i].datastru = l->stu[i - 1].datastru;
			l->stu[i].aver = l->stu[i - 1].aver;
			//将s赋值给第k位置
			strcpy(l->stu[i - 1].ID, s.ID);
			strcpy(l->stu[i - 1].name, s.name);
			l->stu[i - 1].linear = s.linear;
			l->stu[i - 1].physics = s.physics;
			l->stu[i - 1].datastru = s.datastru;
			l->stu[i - 1].aver = aver(s.linear, s.datastru, s.physics);

		}
		l->len++;
		return 1;
	}

}

//插入现有学生信息
int insertV(list *l ) {
	student stu1 = { "09","刘子祥",80,85,87 }, stu2 = { "05","孟祥欣",81,67,74 }, stu3 = { "03","谷红翠",70,68,57 }, stu4 = { "01","王甜甜",52,61,52 };
	insertList(l, stu1, 1);
	insertList(l, stu2, 2);
	insertList(l, stu3, 3); 
	insertList(l, stu4, 4);
	return 1;
}

//按均分插入某同学成绩
int insertS(list* l,student stu) {
	int aver = stu.aver,i=0;
	while (l->stu[i].aver >= stu.aver && i<=l->len-1) {
		i++;
	}
	insertList(l, stu, i + 1);
	return 1;
}

//搜索有挂科记录的同学
void bujige(list* L) {
	printf("挂科记录:\n");
	printf("--------------------------------------------------------\n");
	for (int i = 0; i <=L->len-1; i++) {
		if (L->stu[i].linear < 60)printf("%s同学线性代数成绩为%d\n", L->stu[i].name, L->stu[i].linear);
		if (L->stu[i].physics < 60)printf("%s同学大学物理成绩为%d\n", L->stu[i].name, L->stu[i].physics);
		if (L->stu[i].datastru < 60)printf("%s同学数据结构成绩为%d\n", L->stu[i].name, L->stu[i].datastru);
	}
	printf("---------------------------------------------------------\n");
}

//搜索均分高于x的同学
void junfen(list* L,int x) {
	printf("平均成绩大于%d的同学有:\n", x);
	for (int i = 0; i <=L->len-1 ; i++) {
		if (L->stu[i].aver > x)printf("%s同学平均成绩为%d\n", L->stu[i].name, L->stu[i].aver);
	}
}

int main()
{
	list  ll;
	list* l=≪//初始化指针l
	initlist(l);
	insertV(l);
	student stu = { "08","赵小苗",70,55,67 };
	insertS(l, stu);
	bujige(l);
	printf("输入成绩x");
	int x ;
	scanf("%d", &x);
	junfen(l, x);
}

 

你可能感兴趣的:(数据结构,数据结构)