📄 studentmanage.c
字号:
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"graphics.h"
#include"conio.h"
#define NULL 0
#define len sizeof(struct student)
struct student
{long number;
char name[20];
int age;
char sex[8];
char add[50];
char tele[20];
struct student *pre;
struct student *next;
};
int n=0;
struct student *creat(int i)
{ int j;
struct student *head,*p1,*p2;
head=NULL;
p2=p1=(struct student *)malloc(len);
printf("input record\n");
for(j=1;j<=i;j++)
{printf("number\n");
scanf("%ld",&p1->number);
printf("name\n");
scanf("%s",p1->name);
printf("age\n");
scanf("%d",&p1->age);
printf("sex\n");
scanf("%s",p1->sex);
printf("address\n");
scanf("%s",p1->add);
printf("telephone\n");
scanf("%s",p1->tele);
if(j==1)
head=p1;
else
{ p2->next=p1; p1->pre=p2;}
p2=p1;
p1=(struct student *)malloc(len);
}
p2->next=NULL;
return(head);
}
struct student *sort(struct student *head)
{struct student *p1,*r,*q,*s,*h;
p1=h=(struct student *)malloc(len);
p1->next=head;
while(p1->next!=NULL)
{ q=p1->next;
r=p1;
while(q->next!=NULL)
{if(q->next->number>p1->next->number)
r=q;
q=q->next;
}
if(r!=p1)
{ s=r->next;
r->next=s->next;
s->next=p1->next;
p1->next=s;
}
p1=p1->next;
}
return (h->next);
}
void display(struct student *head)
{ int j=1;
printf("display record\n");
if(head!=NULL)
printf("number name age sex address telephone\n");
do {
printf("%ld %s %d %s %s\n",head->number,head->name,head->age,head->sex,head->add,head->tele);
head=head->next;
if(j%20==0)
getch();
j++;
}while(head!=NULL);
}
struct student *del(struct student *head)
{ struct student *p1,*p2,*q;char ch;
long nu;
p1=head;
if(head==NULL)
printf("there is no record\n");
printf("%ld",nu);
while((p1->number!=nu)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p1->number==nu)
{
printf("it has benn found are you sure to del it or not\n");
ch=getchar();
if(ch==121)
{if(p1==head)
head=p1->next;
else
{ q=p1->next; p2->next=q;q->pre=p2;}
printf("it has been deleted successfully!\n");
}
}
else
printf("it does not exist\n");
return(head);
}
struct student *insert(struct student *head,struct student *insert)
{struct student *p1,*p2;
p1=head;
while((insert->number>p1->number)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(p1->number>=insert->number)
{if(p1==head)
{head=insert; insert->next=p1;}
else
{p2->next=insert;
insert->pre=p2;
insert->next=p1;
p1->pre=insert;
}
}
else
{p1->next=insert;
insert->pre=p1;
insert->next=NULL;
}
return(head);
}
struct student *search(struct student *head)
{struct student *p1,*p2,*q;
long number2;
long a;
char ch;
p1=head;
printf("input number\n");
scanf("%ld",&a);
while(p1->number!=a&&p1->next!=NULL)
{ p2=p1;p1=p1->next;}
if(p1->number==a)
{ printf("what you need has been found what you want to do!\n");
printf("please chose\n");
printf("1.del it\n");
printf("2.edit it\n");
printf("input choice\n");
ch=getch();
switch(ch)
{ case '1': if(p1==head)
head=p1->next;
else if(p1->next!=NULL)
{ q=p1->next; p2->next=q; q->pre=p2;}
else
p2->next=NULL;
break;
case '2': printf("input your record\n");
printf("choice\n");
printf("1.change number 2.change name 3.change age 4.change sex 5.change time 6.change add 7.change telephone");
ch=getch();
switch(ch)
{case '1': printf(" input number\n");
scanf("%ld",&number2);
p1->number=number2;
break;
case '2': printf("input name\n");
scanf("%s",p1->name);
break;
case '3': printf("input age\n");
scanf("%d",&p1->age);
break;
case '4':printf("input sex\n");
scanf("%s",p1->sex);
break;
case '5': printf("input address\n");
scanf("%s",p1->add);
break;
case '6': printf("input telephone\n");
scanf("%s",p1->tele);
break;
default: break;
}
}
printf("what you need to do has been done\n");
}
return(head);
}
void save(FILE *pf,struct student *head)
{
struct student *p;
FILE *pp;
pp=pf;
p=head;
while(p)
{
fwrite(p,len,1,pp);
p=p->next;
}
fclose(pp);
}
main()
{
struct student *p,*q,*p2,*q2,*q3,*q4;
char ch;
FILE *fp;
int i;
clrscr();
if((fp=fopen("c:\\aaa","wb+"))==NULL)
{printf("can not open file");
exit(0);
}
printf("how many students ,please input!\n");
scanf("%d",&i);
p=creat(i);
for(;;)
{ printf("choice\n");
printf("1.insert 2.del 3.search(edit) 4.display 5.sort 6.save \n");
ch=getch();
if(ch==79)
break;
switch(ch)
{ case '1':
p2=(struct student *)malloc(len);
printf("number\n");
scanf("%ld",&p2->number);
printf("name\n");
scanf("%s",p2->name);
printf("age\n");
scanf("%d",&p2->age);
printf("sex\n");
scanf("%s",p2->sex);
printf("address\n");
scanf("%s",p2->add);
printf("telephone\n");
scanf("%s",p2->tele);
p=insert(p,p2);
printf("it has been done!\n");
display(p);
break;
case '2': p=del(p);
printf("it has been done \n");
display(p);
break;
case '3': p=search(p);
printf("it has been done!\n");
display(p);
break;
case '4': display(p);
break;
case '5': p=sort(p); display(p);
break;
case '6': save(fp,p);
default:break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -