C语言学生信息管理系统(参考)

基本功能:录入学生信息,删除、修改、查找学生信息,学生成绩排序,将学生信息保存到文件,读取文件内的学生信息。

功能实现:单向链表、读写文件

基本框架:

1、main.c文件,main函数内实现各项功能,判断输入,功能选择;

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include"function.h"
#include"inset.h"
#include"fileFun.h"

int main() 
{
	char n,arr[10];
	LS* head = HEAD();//创建并初始化头节点
	LS* node ;//创建节点
	LS* p = head;//保护头节点
	redflie(p);//程序运行时将文件内的信息读取到链表
	while (1)
	{
		view();
		printf("请选择:");
	p:	scanf("%c", &n);
		if (n >= '0' && n <= '9' && getchar() == '\n')//解决其他字符输入问题
		{
			switch (n)
			{
			case '0':
				exit(0);
			case '1'://录入
				node = Node();//录入信息时初始化节点
				input(node);//将信息录入节点
				inset(node, p);//将节点连接到链表
				break;
			case '2'://删除
				p = head;
				delet(p);
				break;
			case '3'://查找
				p = head;
				chek(p);
				break;
			case '4'://修改
				p = head;
				mod(p);
				break;
			case '5'://排序
				p = head;
				sort_r(p);
				printf("按成绩降序排序后:\n");
				p = head;
				putout(p);
				break;
			case '6':
				p = head;
				printf("以下是所有学生信息:\n");
				putout(p);
				break;
			case '7'://写入文件
				wrtfile(p);
				break;
			default:
				break;
			}
		}
		else
		{
			fgets(arr, 10, stdin);//清空输入缓冲区的字符,也可以考虑使用清屏system("cls");
			printf("输入格式错误!\n");
			goto p;
		}
	}
	return 0;
}

2、function.c基本功能实现,

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <corecrt_malloc.h>
#include "function.h"
#include"inset.h"
LS* HEAD()//创建头结点
{
	LS* head = (LS*)malloc(sizeof(LS));//使用malloc申请空间,后继可以释放空间,防止内存泄漏
	head->next = NULL;
	memset(head, 0, sizeof(struct student));
	return head;
}
LS* Node()//创建节点
{
	LS* node = (LS*)malloc(sizeof(LS));
	node->next = NULL;
	memset(node, 0, sizeof(struct student));
	return node;
}
void view() //菜单函数
{
	printf("===================\n");
    printf("==  0:退出系统  ==\n");
	printf("==  1:录入信息  ==\n");
	printf("==  2:删除信息  ==\n");
	printf("==  3:查找信息  ==\n");
	printf("==  4:修改信息  ==\n");
	printf("==  5:成绩排序  ==\n");
	printf("==  6:显示全部  ==\n");
	printf("==  7:存到文件  ==\n");
 	printf("===================\n");
}
void  input(LS* node)//输入信息
{
	printf("请输入姓名:");
	scanf("%s", node->stu.name);
	getchar();
	printf("请输入学号:");
	scanf("%s", node->stu.num);
	getchar();
	printf("请输入成绩:");
	scanf("%f", &node->stu.mark);
	getchar();
}
void putout(LS *p)//打印信息
{
	if (p->next == NULL)//通过判断有无节点,判断有无学生信息,
	{
		printf("学生信息为空!\n");
	}
	else
	{
		while (p->next!=NULL)
		{
			printf("姓名:%s学号:%s成绩:%.2f\n", p->next->stu.name, p->next->stu.num, p->next->stu.mark);
			p = p->next;
		}
	}
}
void delet(LS* p)//删除信息
{
	char str[10];
	LS* p1 =NULL;
	printf("请输入要删除学生的学号:\n");
	scanf("%s", str);
	getchar();
	if (p->next == NULL)
	{
		printf("学生信息为空!\n");
	}
	else
	{
		while (p->next != NULL)//到尾结点结束,尾结点指针域为空
		{
			if (strcmp(p->next->stu.num, str) == 0)
			{
				p1 = p->next;//保存需要删除的节点,删除后释放空间
				p->next = p->next->next;//删除节点
				free(p1);//释放已经删除的节点
				break;
			}
			p = p->next;//遍历寻找需要删除的节点
		}
		printf("删除成功!\n");
	}
}
void chek(LS *p)//查找信息
{
	char str[10] = {0};
	printf("请输入要查找学生的学号:\n");//通过学号查找
	scanf("%s", str);
	getchar();  
	if (p->next == NULL)
	{
		printf("学生信息为空!\n");
	}
	else
	{
		while (p->next != NULL)//和删除一样的原理
		{
			if (strcmp(p->next->stu.num, str) == 0)
			{
				printf("姓名:%s学号:%s成绩:%.2f\n", p->next->stu.name, p->next->stu.num, p->next->stu.mark);
				break;
			}
			p = p->next;
		}
	}
}
void sort_r(LS* p)//排序函数
{
	struct student  tmp;
	if (p->next == NULL)
	{
		printf("学生信息为空!\n");
	}
	else
	{
		LS* i = p->next;
		while (i->next != NULL)//冒泡排序
		{
			LS* j = p->next;
			do {
				if (j->stu.mark < j->next->stu.mark)
				{
					tmp = j->stu;
					j->stu = j->next->stu;
					j->next->stu = tmp;
				}
				j = j->next;
			} while (j->next != NULL);
			i = i->next;
		}
	}
}
void mod(LS *p) //修改信息
{
	char str[10];
	printf("请输入要修改学生的学号:\n");
	scanf("%s", str);
	getchar();
	if (p->next == NULL)
	{
		printf("学生信息为空!\n");
	}
	else
	{
		while (p->next != NULL)
		{
			if (strcmp(p->next->stu.num, str) == 0)
			{
				printf("请输入姓名:");
				scanf("%s", p->next->stu.name);
				getchar();
				printf("请输入学号:");
				scanf("%s", p->next->stu.num);
				getchar();
				printf("请输入成绩:");
				scanf("%f", &p->next->stu.mark);
				getchar();
				break;
			}
			p = p->next;
		}
		printf("修改成功!\n");
	}
}

 function.h定义结构体,声明函数

#ifndef _FUN_H_  //防止文件重引用
#define _FUN_H_
struct student {
	float mark;
	char name[20];
	char num[20];
};
typedef struct ls {
	struct student stu;
	struct ls* next;
}LS;
LS* HEAD();//head
LS* Node();//node
void input(LS* node);//输入信息
void putout( LS* p);
void view();
void delet(LS* p);
void chek(LS* p);
void sort_r(LS* p);
void mod(LS* p);
#endif // !_FUN_H_

3、inset.c链接节点 

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"function.h"
//节点链接,找到尾结点,将已经录入信息的节点挂到后面
void inset(LS* node, LS* p)//传参:信息节点和头指针
{
	
	while (p->next != NULL)
	{
		p = p->next;
	}
	p->next = node;
	
}

inset.h声明函数

#ifndef _INSET_H_
#define _INSET_H_
#include "function.h"
void inset(LS* node, LS* p);
#endif // !_INSET_H_

 4、fileFun.c实现文件读写功能

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"fileFun.h"
#include"function.h"
#include"inset.h"
void redflie(LS* p) //地址传参,传的头指针的地址
{
	//创建文件指针
	FILE* file = fopen("studata.txt", "r");//打开只读文件
	if (file == NULL)//判断文件是否打开成功,避免异常报错
	{
		perror("文件打开错误!");//输出错误
	
	}
	else
	{
		LS* node = Node();//创建并初始化节点
		while (fscanf(file, "%f%s%s", &node->stu.mark, node->stu.num, node->stu.name) !=EOF)
			{
			p->next=node;
			p = p->next;
			node = Node();
			}
		}
		if (file != NULL)
		{
			fclose(file);
		}
	}

void wrtfile(LS* p)
{
	FILE* file = fopen("studata.txt", "w");//打开只写文件;//创建文件指针
	if (file == NULL)
	{
		perror("文件打开错误!");
	}
	else
	{
		while (p->next != NULL)//判断是不是尾结点
		{
			fprintf(file, "%.2f\t%s\t%s\n", p->next->stu.mark, p->next->stu.num, p->next->stu.name);//写入文件
			p = p->next;
		}
	}
	if (file != NULL)
	{
		fclose(file);
	}
}

 fileFun.h声明函数

#ifndef _FILEFUN_H_  //防止文件重引用
#define _FILEFUN_H_
#include"function.h"
void wrtfile(LS* p);
void redflie(LS* p);
#endif // !_FILEFUN_H_

基础的链表和读取文件操作,实现简单的信息处理功能,在此基础上还有很多功能可以拓展,比如同时输入多个学生信息,通过数组将信息分班处理等等; 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值