基于动态顺序表实现通讯录项目https://blog.csdn.net/Eristic0618/article/details/135718230?spm=1001.2014.3001.5506
原文在这里嗷,我进行了小小修改,快去关注这位佬。阿瑾0618https://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;
}