/*在关系数据中,病人的信息由若干个基本表存储的,为了简便起见,我们假设所要检索和统计病人的信息存储在线性表p_inof中.每个记录即表元素为上述定义的结构体。
1. 根据病人id来检索病人信息,采用顺序比较好,并简述理由.用c描述。**答:因为顺序存储能方便的通过控制数组序号的方式进行检索,而链式存储需要控制指针指向相应的结点来完成。**
2. 若要经常添加和删除病人信息,则采用链式比较好,并且简述理由.用c描述。**答:因为链式存储能方便的通过指针指向要删除结点的下一个结点完成,而顺序存储需要移动要删除添加结点后面所有结点的位置**
3. 在医院信息系统(his)中统计分析时其中一个重要的模块,现要统计病人中患心脏病(假设病重类型为12),用c描述.
4. 在p_inof struct中为什么定义为病人患病的类型而不定义为患病的名称。**答:这样便于检索,归类和处理。**
*/
/*医院信息管理系统-链式实现*/
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define NULL 0
typedef struct
{
int id; /*病人的id号*/
char name[20]; /*病人的姓名*/
int illtype; /*病人患病类型。“12”代表心脏病 */
}ill_man; /*定义链表元素为病人信息的类型*/
typedef struct stack
{
ill_man man; /*病人信息的类型*/
struct stack *next; /*用来指向下一个节点的指针*/
struct stack *end; /*用来指向链表最后一个元素的指针*/
}ill_node; /*链表的节点类型*/
/*创建一个空表的函数*/
ill_node *initlist()
{
ill_node *m;
m=(ill_node *)malloc(sizeof(ill_node));
m->next=NULL; /*最后一个结点不指向任何存储单元*/
m->end=m; /*使“m->end”指向链表中的最后一个元素*/
return m; /*返回一个头指针*/
}
/*输入病人信息到表尾的函数*/
void input(ill_node *m)
{
ill_node *p; /*定义一个节点用来暂时存放输入信息,然后连接到主链表中*/
while(1) /*用来完成一个无限循环的操作,要退出循环只能通过输入id为0*/
{
p=(ill_node *)malloc(sizeof(ill_node));
printf("/nInput the illman's id:");
scanf("%d",&p->man.id);
if(p->man.id==0)break; /*用来控制循环,如果输入的id为0,则退出该函数*/
printf("Input the illman's name:");
scanf("%s",p->man.name);
printf("Input the illman's illtype:");
scanf("%d",&p->man.illtype);
p->next=NULL; /*使新节点的指针指向空*/
m->end->next=p; /*将新节点插入链表中*/
m->end=p; /*使“m->end”始终指向链表的最后一个节点*/
}
}
/*根据病人id查找病人信息的函数*/
void search(ill_node *m)
{
ill_node *p; /*用来查找和输出所要找的病人的信息*/
int id_num;
printf("Input the patien'id you want to search :");
scanf("%d",&id_num); /*输入需要查找病人的id号*/
for(p=m->next;p!=NULL;p=p->next)
if(p->man.id==id_num) /*匹配查找的id*/
{
printf("*************************/n");
printf("The illman's information is:/n");
printf("The illman's id is: %d/n",p->man.id);
printf("The illman's name is: %s/n",p->man.name);
printf("The illman's illtype is: %d/n",p->man.illtype);
printf("*************************/n");
}
}
/*删除链表中指定id的病人信息的函数*/
void del(ill_node *m)
{
ill_node *p; /*查找和输出要删除病人的信息*/
int id_num; /*要删除病人的id号*/
printf("Input the patien'id you want to delete :");
scanf("%d",&id_num); /*输入要删除病人的id号*/
for(p=m;p!=NULL;p=p->next)
if(p->man.id==id_num)/*匹配删除的id*/
{
printf("***********Delete Element**********/n");
printf("The delete illman's information is:/n");
printf("The illman's id is: %d/n",p->man.id);
printf("The illman's name is: %s/n",p->man.name);
printf("The illman's illtype is: %d/n",p->man.illtype);
printf("************************************/n");
p->next=p->next->next; /*实现删除操作*/
}
}
/*插入链表元素在指定病人id号的后面的函数*/
void insert(ill_node *m)
{
ill_node *p,*n;
int id_num;
printf("Input the id:");
scanf("%d",&id_num); /*在id为此输入值的病人后面插入新信息*/
for(p=m;p!=NULL;p=p->next)
if(p->man.id==id_num) /*匹配输入的id*/
{
n=(ill_node *)malloc(sizeof(ill_node));
printf("Input the illman's id:");
scanf("%d",&n->man.id);
printf("Input the illman's name:");
scanf("%s",n->man.name);
printf("Input the illman's illtype:");
scanf("%d",&n->man.illtype);
n->next=p->next; /*实现插入操作*/
p->next=n; /*实现插入操作*/
}
}
/*统计表中患某种病的病人个数的函数*/
void cout_ill(ill_node *m)
{
ill_node *p;
int type; /*患病的类型*/
int cout=0;
printf("Input the illtype :/n");
scanf("%d",&type); /*输入所要统计的患病类型*/
for(p=m;p!=NULL;p=p->next)
{
if(p->man.illtype==type)++cout; /*实现统计功能*/
}
printf("The number is: %d/n",cout);/*输出统计数*/
}
/*输出链式表中病人信息的函数*/
void print(ill_node *m)
{
ill_node *p;
if(m->next==0){printf("There is no record ./n");return;} /*若为空表则给出提示*/
for(p=m;p->next!=0;p=p->next) /*输出链表中的所有元素*/
{
printf("*****************/n");
printf("id: %d/n",p->next->man.id);
printf("name: %s/n",p->next->man.name);
printf("illtype: %d/n",p->next->man.illtype);
}
printf("*****************/n");
}
/*输出菜单的函数*/
void print_mu()
{
printf("*******************--menu--*******************/n");
printf(" Input the patient's information input 1 ./n");
printf(" Output the patien's information input 2 ./n");
printf(" Delete the patien's information input 3 ./n");
printf(" Search the patien with id input 4 ./n");
printf(" Conut the heart ill number input 5 ./n");
printf(" Insert the new record behinde the id input 6 ./n");
printf("*********************menu*********************/n");
printf("Input your choose :");
}
void main()
{
ill_node L;
ill_node *L1=&L;
int a;
print_mu();
L1=initlist();
while(1)
{
scanf("%d",&a);
switch(a)
{
case 1:input(L1);print_mu();break;
case 2:print(L1);print_mu();break;
case 3:del(L1);print_mu();break;
case 4:search(L1);print_mu();break;
case 5:cout_ill(L1);print_mu();break;
case 6:insert(L1);print_mu();break;
}
}
}