学生信息管理

来源:互联网 发布:js实现点击显示和隐藏 编辑:程序博客网 时间:2024/05/01 21:38

Main.cpp

#include<stdio.h>
#include"linklist.h"
#include"nodeitem.h"
#define N 3


int main()
{
linklist l;
stu s[N];
stu temp;
int i,num;
char name[100];
char ch;
init_list(&l);
input_data(s,N);
for(i=0;i<N;i++)
insert_list(&l,&s[i]);
display_list(&l);
printf("please input what you want!\n新增:A   删除:D  查询:I 退出:Q  \n");
ch=getchar();
while(ch!='\0'){
switch(ch){
case 'A':
        input_single_stu(&temp);
insert_list(&l,&temp);
printf("the information after adding are:\n");
display_list(&l);
ch=getchar();
break;
case 'D':
printf("\n please input the name of deleting student!\n");
getchar();
gets(name);
delete_namelist(&l,name);
display_list(&l);
ch=getchar();
break;
case 'I':
printf("\n please input the name of seeking student!\n");
getchar();
getstu_name_list(&l,name);
ch=getchar();
break;
/*
printf("\n please input the num of seeking student!\n");
           getstu_num_list(&l,num);
*/
case 'Q':
ch='\0';
break;
default:
printf("please input again!");
getchar();
ch=getchar();
}
}


return 0;
}


分文件:

nodeitem.h

#ifndef nodeitem_h
#define nodeitem_h
#define LEN 100


typedef struct {
int num;
char sex;
char name[LEN];
}stu,*pstu;
typedef int Bool;
typedef stu nodeitem;


void input_data(stu s[],int n);
Bool is_equal(nodeitem *p1, nodeitem *p2);
void display_nodeitem(nodeitem *pn);
void input_single_stu(stu *ps);
void copy_nodeitem(nodeitem *pn1,nodeitem *pn2);
void destroy_nodeitem(nodeitem *pn);
Bool is_equal_name(char str1[],char str2[]);
Bool is_equal_num(int i,int j);


#endif


nodeitem.cpp

#include"nodeitem.h"
#include<stdio.h>
#include<string.h>


void input_data(nodeitem s[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("please input the information of student %d\n",i+1);
printf("please input the number!");
scanf("%d",&(s[i].num));
getchar();
printf("please input the sex!");
scanf("%c",&(s[i].sex));
getchar();
printf("please input the name!");
gets(s[i].name);

}
}


Bool is_equal(nodeitem *p1, nodeitem *p2)
{
return (p1->num==p2->num)&&(p1->sex==p2->sex)&&(!strcmp(p1->name,p2->name));
}
Bool is_equal_name(char str1[],char str2[])
{
return (!(strcmp(str1,str2)));
}
Bool is_equal_num(int i,int j)
{
return i==j;
}


void input_single_stu(stu *ps)
{
    printf("please input the number!");
scanf("%d",&(ps->num));
getchar();
printf("please input the sex!");
scanf("%c",&(ps->sex));
getchar();
printf("please input the name!");
gets(ps->name);
}


void display_nodeitem(nodeitem *pn)
{
printf("\nthe student information is as follows!\n");
printf("num=%d,sex=%c and name=%s\n",pn->num,pn->sex,pn->name);
}


void copy_nodeitem(nodeitem *pn1,nodeitem *pn2)
{
pn1->num=pn2->num;
pn1->sex=pn2->sex;
strcpy(pn1->name,pn2->name);
}


void destroy_nodeitem(nodeitem *pn)
{


}

node.h

#ifndef node_h
#define node_h
#include"nodeitem.h"


typedef struct node{
    stu elem;
struct node* next;
}node,*pnode;


pnode create_node(nodeitem *pn);


#endif

node.cpp


#include<stdio.h>
#include<stdlib.h>
#include"node.h"


pnode create_node(nodeitem *pn)
{
pnode p=(pnode)malloc(sizeof(node));
if(p==NULL){
printf("cannot allocate such memeory!");
exit(EXIT_FAILURE);
}
copy_nodeitem(&(p->elem),pn);
p->next=NULL;
return p;
}


linklist.h

#ifndef linklist_h
#define linklist_h
#include"node.h"


typedef struct{
node* head;
}linklist,*plinklist;
typedef int Bool;


void init_list(plinklist pl);
void insert_list(plinklist pl,nodeitem *pn);
void display_list(plinklist pl);
void delete_list(plinklist pl,nodeitem *pn);
Bool is_inlist(plinklist pl,nodeitem *pn);
void destory_list(plinklist pl);
Bool is_nameinlist(plinklist pl,char name[]);
Bool is_numinlist(plinklist pl,int num);
void getstu_name_list(plinklist pl,char str[]);
void getstu_num_list(plinklist pl,int num);
void delete_namelist(plinklist pl,char str[]);


#endif


linklist.cpp


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"linklist.h"


void init_list(plinklist pl)
{
pl->head=NULL;
}


void insert_list(plinklist pl,nodeitem *pn)
{
pnode p=create_node(pn);

p->next=pl->head;
pl->head=p;
}


Bool is_inlist(plinklist pl,nodeitem *pn)
{
pnode p=pl->head;
while(p)
{
     if(is_equal(&(p->elem),pn))
return 1;
p=p->next;
}
return 0;
}
Bool is_nameinlist(plinklist pl,char name[])
{
pnode p=pl->head;
while(p)
{
if(is_equal_name((p->elem).name,name))
return 1;
p=p->next;
}
return 0;
}
Bool is_numinlist(plinklist pl,int num)
{
pnode p=pl->head;
while(p){
if(is_equal_num((p->elem).num,num))
return 1;
p=p->next;
}
return 0;
}




void delete_list(plinklist pl,nodeitem* pn)
{
pnode curr,prev;
if(is_inlist(pl,pn)){
    
for(curr=pl->head,prev=NULL;curr!=NULL&& !is_equal(&(curr->elem),pn);prev=curr,curr=curr->next)
;
if(prev==NULL)
pl->head=curr->next;
else

        prev->next=curr->next;
destroy_nodeitem(&(curr->elem));
    free(curr);
}
else{
printf("this item is not in the list!");
return ;
}
}


void display_list(plinklist pl)
{
pnode p=pl->head;
while(p)
{
display_nodeitem(&p->elem);
p=p->next;
}
}


void destory_list(plinklist pl)
{
pnode p=pl->head;
while(p)
{
destroy_nodeitem(&(p->elem));
pnode temp=p;
p=p->next;
free(temp);
}
}
void getstu_name_list(plinklist pl,char str[])
{
pnode curr,prev;
gets(str);
    if(is_nameinlist(pl,str)){
for(curr=pl->head,prev=NULL;(curr!=NULL)&&(strcmp((curr->elem).name,str));prev=curr,curr=curr->next)
;
display_nodeitem(&(curr->elem));
}
    else{
printf("this item is not in the list!");
return ;
}
}


void getstu_num_list(plinklist pl,int num)
{
pnode curr,prev;
scanf("%d",&num);
if(is_numinlist(pl,num)){
for(curr=pl->head,prev=NULL;(curr!=NULL)&&(!((curr->elem).num==num));prev=curr,curr=curr->next)
;
display_nodeitem(&(curr->elem));
}
else{
printf("this item is not in the list!");
return ;
}
}
 void delete_namelist(plinklist pl,char str[])
 {
 
if(is_nameinlist(pl,str)){
pnode curr,prev;
for(curr=pl->head,prev=NULL;(curr!=NULL)&&(strcmp((curr->elem).name,str));prev=curr,curr=curr->next)
;
if(prev==NULL){
prev=pl->head;
pl->head=pl->head->next;
free(prev);
}
prev->next=curr->next;
destroy_nodeitem(&(curr->elem));
}
else{
printf("the item is not in the list!\n");
return ;
}
 
 }