基于动态顺序表实现病历存储项目

  基于动态顺序表实现通讯录项目icon-default.png?t=O83Ahttps://blog.csdn.net/Eristic0618/article/details/135718230?spm=1001.2014.3001.5506

原文在这里嗷,我进行了小小修改,快去关注这位佬阿瑾0618icon-default.png?t=O83Ahttps://blog.csdn.net/Eristic0618?type=blog

(1)Seqlist.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<string.h>
#include<Windows.h>
#include<assert.h>
#include "Contact.h"

typedef Info SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	size_t size;
	size_t capicity;
}patient;

void SeqListInit(patient* psl);

void CheckCapacity(patient* psl);

void SeqListPushBack(patient* ps1, SLDataType x);

void SeqListErase(patient* psl, size_t pos);

void SeqListDestory(patient* psl);

 (2)Seqlist.c

#include"SeqList.h"

void SeqListDestory(patient* psl)
{
	assert(psl);
	free(psl->a);
	psl->a = NULL;
	psl->capicity=0;
	psl->size = 0;
}

void SeqListInit(patient* psl)
{
	assert(psl);
	psl->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (psl->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	psl->size = 0;
	psl->capicity = 4;
}

void CheckCapacity(patient* psl)
{
	assert(psl);
	if (psl->size == psl->capicity)
	{
		SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * psl->capicity * 2);
		if (tmp == NULL)
		{
			perror("malloc fail");
			return;
		}
		psl->a = tmp;
		psl->capicity *= 2  ;
	}
} 

void SeqListPushBack(patient* psl, SLDataType x)
{
	assert(psl);
	CheckCapacity(psl);
	psl->a[psl->size++] = x;
}

void SeqListErase(patient *psl, size_t pos)
{
	assert(psl);
	assert(0 <= pos && pos < psl->size);
	while (pos < psl->size - 1)
	{
		psl->a[pos] = psl->a[pos + 1];
		pos++;
	}
	psl->size--;
}

(3)Contact.h

#pragma once
#define NAME_MAX 100
#define SYMPTOM_MAX 10


struct SeqList ;
typedef struct SeqList Case_History;

typedef struct PersonInfo
{
	int num;
	char name[NAME_MAX];
	char symtop[SYMPTOM_MAX];
}Info;

void InitCase_History(Case_History* pch);//初始化病历

void DestoryCase_History(Case_History* pch);//销毁病历

int FindByName(Case_History* pch, char* name);//通过姓名找病人

void AddCase_History(Case_History*pch);//添加病人

void DeleteCase_History(Case_History*pch);//删除病人

void ModifyCase_History(Case_History* pch);//修改病人信息

void FindCase_History(Case_History*pch);//查找病人

void ShowCase_History(Case_History* pch); //展示病人列表

void ClearCase_History(Case_History* pch);//清空病历

void SaveCase_History(Case_History* pch);//保存病历

void LoadCase_History(Case_History* pch);//载入历史病历

(4)Contact.c

#include "SeqList.h"

void InitCase_History(Case_History* pch)//初始化病历
{
	SeqListInit(pch);
}

void DestoryCase_History(Case_History* pch)//销毁病历
{
	SeqListDestory(pch);
}

int FindByName(Case_History *pch, char* name)//通过姓名找病人
{
	 for(size_t i=0;i<pch->size;i++)
		 if (strcmp(name, pch->a[i].name)==0)
		 {
			 return i;
		 }
	 return -1;
}

void AddCase_History(Case_History* pch)//添加病人
{
	CheckCapacity(pch);
	printf("请输入姓名:\n");
	scanf("%s", pch->a[pch->size].name);
	printf("请输入病历号:\n");
	scanf("%d", &(pch->a[pch->size].num));
	printf("请输入症状:\n");
	scanf("%s", pch->a[pch->size].symtop);

	pch->size++;
	system("cls");
	printf("添加成功!\n");
}

void DeleteCase_History(Case_History* pch)//删除病人
{
	char name[100];
	printf("请输入要删除的病人:\n");
	scanf("%s", name);

	int index = FindByName(pch, name);
	if (index == -1)
	{
		printf("要删除的病人不存在!\n");
		return;
	}
	SeqListErase(pch, index);
	system("cls");
	printf("删除成功!\n");
}

void ModifyCase_History(Case_History* pch)//修改病人信息
{
	char name[100];
	printf("请输入要修改的病人:\n");
	scanf("%s", name);
	int index = FindByName(pch, name);
	if (index == -1)
	{
		printf("要删除的病人不存在!\n");
		return;
	}
	printf("请输入修改后的姓名:\n");
	scanf("%s", pch->a[index].name);
	printf("请输入修改后的病历号:\n");
	scanf("%d", &(pch->a[index].num));
	printf("请输入修改后的症状:\n");
	scanf("%s", pch->a[index].symtop);
	system("cls");
	printf("修改成功!\n");
}

void FindCase_History(Case_History* pch)//查找病人
{
	char name[100];
	printf("请输入要寻找的病人:\n");
	scanf("%s", name);

	int index = FindByName(pch, name);
	if (index == -1)
	{
		printf("要寻找的病人不存在\n");
		return;
	}

	system("cls");

	printf("查找成功!\n");
	printf("姓名:%s\n", pch->a[index].name);
	printf("病历号:%d\n", pch->a[index].num);
	printf("症状:%s\n", pch->a[index].symtop);

}

void ShowCase_History(Case_History* pch) //展示病人列表
{
	if (pch->size == 0)
	{
		printf("系统为空!\n");
		return;
	}
	printf("姓名 病历号 症状\n");
	for (size_t i = 0; i < pch->size; i++)
	{
		printf("%s %d %s\n",
			pch->a[i].name,
			pch->a[i].num,
			pch->a[i].symtop);
	}
}

void ClearCase_History(Case_History* pch)//清空病历
{
	pch->size = 0;
	printf("系统清空成功!\n");
}

void SaveCase_History(Case_History* pch)//保存病历
{
	FILE* pf = fopen("Case_History.txt", "wb");
	if (pf == NULL)
	{
		perror("fopen fail");
		return;
	}
	for (size_t i = 0; i < pch->size; i++)
	{
		fwrite(pch->a + i, sizeof(Info), 1, pf);
	}
	printf("病历数据保存成功!\n");
	fclose(pf);
}

void LoadCase_History(Case_History* pch)//载入历史病历
{
	FILE* pf = fopen("Case_History.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen fail");
		return;
	}
	Info info;
	while (fread(&info, sizeof(Info), 1, pf))
	{
		SeqListPushBack(pch, info);
	}
	printf("病历数据载入成功!\n");
	fclose(pf);
}

(5)text.c

#include "SeqList.h"

void Menu()
{
	printf("**********病人病历信息系统**********\n");
	printf("****** 1.添加病人   2.删除病人******\n");
	printf("****** 3.修改病人   4.查找病人******\n");
	printf("****** 5.查看系统   6.清空系统******\n");
	printf("****** 0.退出系统             ******\n");
	printf("************************************\n");
}
int main()
{
	Case_History ch;
	InitCase_History(&ch);
	LoadCase_History(&ch);
	int option = -1;
	do {
		Menu();
		printf("请选择:\n");
		scanf("%d", &option);
		system("cls");
		switch (option)
		{
		case 1://添加病人信息
			AddCase_History(&ch);
			break;

		case 2://删除病人信息
			DeleteCase_History(&ch);
				break;

		case 3://修改病人信息
			ModifyCase_History(&ch);
			break;

		case 4://查找病人
			FindCase_History(&ch);
			break;

		case 5://查看病人信息
			ShowCase_History(&ch);
			break;
		case 6://清空病历
			ClearCase_History(&ch);
			break;
		case 0://退出
			printf("系统退出中……\n");
			break;

		default:
			printf("输入错误,请重新输入\n");
			break;
		}

	} while (option);
	SaveCase_History(&ch);
	DestoryCase_History(&ch);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值