C语言实现学生信息管理系统(结构体指针、动态内存)

一.功能:

1.连续增加学生信息

2.删除学生信息

3.修改学生信息

4.查找学生信息

5.列出学生信息

6.保存学生信息到文件

7.从文件中加载学生信息

二.学生信息结构体

首先定义一个学生结构体,再直接定义一个全局指针变量stu,让下面的函数都可以访问到这个动态内存
学生类结构体的信息大家可以自己添加。

struct Student{
	int no;				//学号
	char name[40];		//名字
	int age;			//年龄
	char sex;			//性别
	double score[3];	//语数英三门成绩
}*stu;

再定义两个全局变量当前学生人数cnt和学生学号初始学号(用在学生学号自动生成)make_id;

int id = 20191100;	//学号 自动生成
int count = 0;		//学生人数

三.增加学生函数

STU *add(){		//增加学生
	STU *p;
	p = stu;
	p+=count;	//指针指向第count个学生的位置
	if(stu == NULL){		//如果动态内存失败
		printf("applied failed!\n");
		back();
		return;
	}
	int flag = 1;
	while(flag){
		p->no = id+count;		//自动生成学号
		printf("please input student's name:");
		scanf("%s",p->name);
		printf("please input student's age:");
		scanf("%d",&p->age);
		printf("please input student's sex:");
		scanf("%*[^\n]");
		scanf("%*c");
		scanf("%c",&p->sex);
		printf("please input student's score:");
		scanf("%lf%lf%lf",&p->score[0],&p->score[1],&p->score[2]);
		printf("add student more?(1-YES,0-NO)\n");
		scanf("%d",&flag);
		p++;
		count++;
	}
	back();
	return stu;
}

这是一个返回学生结构体类型指针的函数,back()是模拟按任意键返回功能,后面会讲.

四.删除学生

if(stu == NULL){
		printf("the list is empty!\n");
		back();
		return stu;
	}
	STU *p;
	p = stu;
	int num;
	printf("please input student id that you want to delete:");
	scanf("%d",&num);
	int i = 0;
	while(p[i].no !=num && i<count ){	//	查找被删除学生是否存在
		i++;
	}
	if(num == p[i].no){		//删除操作即对需要删除的学生信息进行覆盖
		for(;i<count;i++){		//删除可以选择从后往前覆盖也可以直接把最后一个学生覆盖被删除学生,为了保持学号有序我选择第一种方式
			p[i] = p[i+1];	
		}
		count--;
	}else{
		printf("student No:%d is not exist!\n",num);	
	}
	back();
	return stu;
}

五.修改学生

STU *modif(){	//修改学生信息
	if(stu == NULL){
		printf("the list is empty!\n");
		back();
		return stu;
	}
	printf("please input student's id that you want to modify:");
	int num;
	scanf("%d",&num);
	STU *p;
	p = stu;
	int i = 0;
	while(p->no != num && i<count){	//查找被修改的学生
		i++;
		p++;
	}
	if(p->no == num){
		printf("student No:%d 's information showed as below:\n",num);
		printf("ID:%d    name:%s    age:%d     sex:%c\n",p->no,p->name,p->age,p->sex);
		printf("chinese:%g  	math:%g 	english:%g\n",p->score[0],p->score[1],p->score[2]);
		printf("Please select the items you want to change:\n");
		printf("1~name  2~age  3~sex 4~chinese score  5~math score 6~english score 0~done\n");
		while(1){
			int opt;
			scanf("%d",&opt);
			switch(opt){
				case 1:
					printf("input new name:");
					scanf("%s",p->name);
					break;
				case 2:
					printf("input new age:");
					scanf("%d",&p->age);
					break;
				case 3:
					printf("input new sex:");
					scanf("%*[^\n]");
					scanf("%*c");
					scanf("%c",&p->sex);
					break;
				case 4:
					printf("input new chinese score:");
					scanf("%lf",&p->score[0]);
					break;
				case 5:
					printf("input new math score:");
					scanf("%lf",&p->score[1]);
					break;
				case 6:
					printf("input new english score:");
					scanf("%lf",&p->score[2]);
					break;
				case 0:
					printf("Modification completed!\n");
					back();
					return stu;
				default:
					printf("illegal input!\n");
					break;
			}
			printf("Please continue to select:");
		}
	}else{
		printf("student No:%d is not exist!\n",num);	
	}
	back();
	return stu;
}

六.查找学生

STU *find(){	//查找学生
	if(stu == NULL){
		printf("the list is empty!\n");
		back();
		return stu;
	}
	int i=0;
	STU *p;
	p = stu;
	printf("please input the No that you want to find:");
	int num;
	scanf("%d",&num);
	while(p->no != num&&i<count){	//查找学生
		i++;
		p++;
	}
	if(p->no == num){
		printf("student No:%d 's information showed as below:\n",num);
		printf("ID:%d    name:%s    age:%d     sex:%c\n",p->no,p->name,p->age,p->sex);
		printf("chinese:%g  	math:%g 	english:%g\n",p->score[0],p->score[1],p->score[2]);
	}else{
		printf("student No:%d is not exist!\n",num);	
	}
	back();
	return stu;
}

七.列出学生信息

void print(){	//打印学生
	if(stu == NULL){
		printf("the list is empty!\n");
		back();
		return;
	}
	STU *p;
	p = stu;
	int i=0;
	printf("there are %d student's information:\n",count);
	for(i<0;i<count;i++){
		printf("ID:%d    name:%s    age:%d     sex:%c\n",p->no,p->name,p->age,p->sex);
		printf("chinese:%g  	math:%g 	english:%g\n",p->score[0],p->score[1],p->score[2]);
		p++;
	}
	back();
}

其他菜单操作函数

1.文件保存函数

void save(){		//保存文件
	FILE *f = fopen("file_stu.dat","w");
		if(f != NULL){
			fwrite(&count,sizeof(count),1,f);
			fwrite(stu,LEN,count,f);
			fclose(f);
		}
}

2.文件加载

void load(){		//加载文件
	FILE *f = fopen("file_stu.dat","r");
	if(f != NULL){		
		fread(&count,sizeof(count),1,f);
		fread(stu,LEN,count,f);
		fclose(f);
	}
}

3.按0键退出

void back(){
	printf("please press 0 to continue~\n");
	int press;
	while(1){
		scanf("%d",&press);
		if(press == 0){
			break;	
		}
	}
}

源代码

点解下载https://download.csdn.net/download/weixin_42617375/11997141

发布了14 篇原创文章 · 获赞 84 · 访问量 2798

猜你喜欢

转载自blog.csdn.net/weixin_42617375/article/details/103244063