欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C项目(文件链表实现管理系统)

程序员文章站 2022-07-14 08:26:45
...

项目核心

管理系统的三大块:内存模型,数据库模型,人机交互界面。

将链表作为内存数据模型,将文件作为数据库,将终端作为交互界面。读文件生成链表,修改链表写入文件。

初始化数据库 此时数据库是文件
读取数据库,生成内存数据模型
增,删,改,查,排序
更新数据库,退出程序

代码实现

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>



typedef struct student  //数据节点
{
	char name[30];
	char sex;
	int age;
	float score;
}Stu;

typedef struct _StuNode      //链表节点
{
	Stu data;               // 数据域
	struct _StuNode* next;  // 指针域
}StuNode;

void InitData2File()         //初始化数据库     此时数据库是文件
{
	Stu s[4] =
	{
		"liudehua",'x',50,100,
		"zhangxueyou",'x', 60,89,
		"guofucheng",'m',50,88,
		"liming",'f',50,90,
	};
	FILE* pf = fopen("stu.data", "w+");
	if (NULL == pf)
		exit(-1);
	fwrite((void*)s, sizeof(s), 1, pf);
	fclose(pf);
	return;
}

StuNode* CreatListFronFile(const char* filePath) //创建文件链表
{
	FILE* pf = fopen(filePath, "r+");
	if (NULL == pf)
		exit(-1);
	StuNode* head = (StuNode*)malloc(sizeof(StuNode));
	head->next = NULL;
	StuNode* tmp = (StuNode*)malloc(sizeof(StuNode));
	while (fread((void*)&tmp->data, sizeof(Stu), 1, pf)) //1    0
	{
		tmp->next = head->next;
		head->next = tmp;
		tmp = (StuNode*)malloc(sizeof(StuNode));
	}
	free(tmp);
	return head;
}

void TraverseStuList(StuNode* head)   //遍历链表
{
	printf("\n\t\t\t========================================================\n");
	printf("\t\t\tname\t\tsex\t\tage\t\tscore\n");
	head = head->next;
	while (head)
	{
		printf("\t\t\t%-10s\t%c\t\t%d\t\t%.2f\n", head->data.name, head->data.sex, head->data.age, head->data.score);
		head = head->next;
	}
	printf("\n\t\t\t========================================================\n");
}

void addListStuNode(StuNode* head)   //增加链表节点
{
	StuNode* tmp = (StuNode*)malloc(sizeof(StuNode));
	printf("name :");
	scanf("%s", tmp->data.name);

	getchar();
	printf("sex  :");
	scanf("%c", &tmp->data.sex);

	getchar();
	printf("age  :");
	scanf("%d", &tmp->data.age);

	getchar();
	printf("score:");
	scanf("%f", &tmp->data.score);

	tmp->next = head->next;
	head->next = tmp;
}

StuNode* SearchStuNode(StuNode* head)   //查找链表节点
{
	printf("please input search name:");
	char name[30];
	scanf("%s", name);
	head = head->next;
	while (head)
	{
		if (strcmp(head->data.name, name) == 0)
		break;
		head = head->next;
	}
	return head;
}


void DeleteListStuNode(StuNode* head)  //删除链表节点
{
	StuNode* tmp = SearchStuNode(head);
	if (!tmp)
	{
		printf("删除的用户不存在\n");
		printf("按任意键返回!\n");
		getchar();
		getchar();
	}
	else
	{
		StuNode* deletePrior = head;
		while (deletePrior->next != tmp)
		{
			deletePrior = deletePrior->next;
		}
		deletePrior->next = tmp->next;
		free(tmp);
	}
}


int lenListStu(StuNode* head)    //求链表长度
{
	int len = 0;
	head = head->next;
	while (head)
	{
		len++;
		head = head->next;
	}
	return len;
}
void SortListStu(StuNode* head)    //链表排序
{
	int len = lenListStu(head);
	StuNode* prep, * p, * q;
	for (int i = 0; i < len - 1; i++)
	{
		prep = head;
		p = prep->next;
		q = p->next;
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (strcmp(p->data.name,q->data.name)>0)
			{
				prep->next = q;
				p->next = q->next;
				q->next = p;

				prep = q;
				q = p->next;
				continue;
			}
			prep = prep->next;
			p = p->next;
			q = q->next;
		}
	}
}

void SaveList2File(StuNode* head,const char * filePath) //更新数据库
{
	FILE * pf = fopen(filePath, "w+");
	if (NULL == pf)
		exit(-1);
	head = head->next;
	while (head)
	{
		fwrite((void *)&head->data,sizeof(Stu),1,pf);
		head = head->next;
	}
	fclose(pf);
}
void DestroyListStu(StuNode* head)   //销毁链表
{
	StuNode* tmp;
	while (head)
	{
		tmp = head;
		head = head->next;
		free(tmp);
	}
}
int main()
{
	//InitData2File();
	StuNode* head = CreatListFronFile("stu.data");
	while (1)
	{
		system("cls");
		TraverseStuList(head);
		printf("\n1-->add\t\t2-->search\t3-->delete\t4-->sort\t5-->exit\n");
		int choice;
		printf("please input choice:");
		scanf("%d", &choice);
		StuNode* FindNode;
		switch (choice)
		{
		case 1:
			addListStuNode(head);
			break;
		case 2:
			
			if (FindNode = SearchStuNode(head))
			{
				printf("find success\n");
				printf("\t\t\t%-10s\t%c\t\t%d\t\t%.2f\n", FindNode->data.name,
					FindNode->data.sex, FindNode->data.age, FindNode->data.score);
			}
			else
			{
				printf("no find");
			}
			printf("按任意键返回!\n");
			getchar();
			getchar();

			break;
		case 3:
			DeleteListStuNode(head);

			break;
		case 4:
			SortListStu(head);
			break;
		case 5:
			SaveList2File(head,"stu.data");
			DestroyListStu(head);
			return 0;
		default:
			printf("输入错误\n");
		}
	}
	return 0;
}

测试结果

C项目(文件链表实现管理系统)

C项目(文件链表实现管理系统)

C项目(文件链表实现管理系统)
C项目(文件链表实现管理系统)

C项目(文件链表实现管理系统)

C项目(文件链表实现管理系统)

C项目(文件链表实现管理系统)

C项目(文件链表实现管理系统)

相关标签: C_review