📄 student.cpp
字号:
#include"stdio.h"
#include"conio.H"
#include"alloc.h"
#include"stdlib.h"
/*struct date
{
int month;
int day;
int year;
};*/
#define LEN sizeof(struct student)
struct student
{
long int num; /*学号*/
char name[20]; /*姓名*/
int age; /*年龄*/
char sex[6]; /*性别*/
char address[80]; /*家庭住址*/
char entertime[12]; /*入学时间*/
char telephonenumber[17]; /*电话号码*/
int achievement /*成绩*/ ;
struct student *next;
};
FILE *fp; /*实现从文件装入的功能*/
int I;
struct student *loadfromfile(struct student *head)
{
struct student *p1,*p2,*p3;
int i,j;
int k;
fp=fopen("daqiang.txt","rb+");
head=NULL;
p1=p2=(struct student *)malloc(LEN);
if(fp!=NULL)
{
for (I=1;I<50;I++)
{
if (((fgetc(fp))==EOF))
{
p2->next=NULL;
p3=p2->next;
return(head);
}
else
{
k=I-1;
fseek(fp,k*LEN,SEEK_SET);
fread(p1,LEN,1,fp);
if(p1==NULL)
{
p2->next;
return head;
}
else
{
if (I==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
}
p1=(struct student *)malloc(LEN);
}
}
p2->next=NULL;
p3=p2->next;
}
else
{
fp=fopen("daqiang.txt","wb");
I=1;
}
printf("records=%d",i);
fclose(fp);
return head;
}
struct student *saveintofile(struct student *head) /*实现保存学生信息的功能*/
{
int i,j;
struct student *p1;
p1=head;
fp=fopen("daqiang.txt","wb");
if (p1!=NULL)
{
for (j=1;j<I;j++)
{
if (fwrite(p1,LEN,1,fp)!=1)
printf("file write Error!\n");
p1=p1->next;
}
}
else return head;
fclose(fp);
return head;
}
struct student *getstudentmessages(struct student *p) /*实现录入的功能*/
{
clrscr();
getchar();
printf("Please input the sex of the student\n");
gets(p->sex);
printf("Please input the name of the student\n");
gets(p->name);
printf("Please input the address of the student\n");
gets(p->address);
printf("Please input the entertime of the student\n");
gets(p->entertime);
printf("Please input the telephonenumber of the student\n");
gets(p->telephonenumber);
printf("Please input the number of the student\n");
scanf("%ld",&p->num);
printf("Please input the age of the student\n");
scanf("%d",&p->age);
printf("Please input the achievement of the student\n");
scanf("%d",&p->achievement);
printf("the messages of the student are:\n\n");
printf("The name of the student:%s\n",p->name);
printf("The number of the student:%ld\n",p->num);
printf("The age of the student:%d\n",p->age);
printf("The sex of the student:%s\n",p->sex);
printf("The address of the student:%s\n",p->address);
printf("The entertime of the student:%s\n",p->entertime);
printf("The telephonenumber of the student:%s\n",p->telephonenumber);
printf("The achievement of the student:%4d\n",p->achievement);
getch();
}
void insertstudentmessages(struct student *p) /*实现插入学生信息的功能*/
{
clrscr();
getchar();
printf("Please input the sex of the inserted student\n");
gets(p->sex);
printf("Please input the name of the inserted student\n");
gets(p->name);
printf("Please input the address of the inserted student\n");
gets(p->address);
printf("Please input the entertime of the inserted student\n");
gets(p->entertime);
printf("Please input the telephonenumber of the inserted student\n");
gets(p->telephonenumber);
printf("Please input the number of the inserted student\n");
scanf("%ld",&p->num);
printf("Please input the age of the inserted student\n");
scanf("%d",&p->age);
printf("Please input the achievement of the inserted student\n");
scanf("%d",&p->achievement);
}
struct student *repeatgetstudentmessaage()/*重复得到学生的信息*/
{
int i;
char YN;
struct student *head,*p1,*p2;
clrscr();
p1=p2=(struct student*)malloc(LEN);
head=NULL;
i=0;
do
{
getstudentmessages(p1);
i++;
if(i==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student*)malloc(LEN);
printf("Do you want input another studnet's message?input'Y'for yes or'N'for no.\n");
scanf("%c",&YN);
printf("%c\n",YN);
while((YN!='Y')&&(YN!='N'))
{ printf("\n");
printf("Please input 'Y'or'N'\n");
scanf("%c",&YN);
}
}while(YN=='Y');
p2->next=NULL;
return head;
}
struct student *order4(struct student *head1) /*实现按学好排序的功能*/
{
struct student *orderhead;
struct student *p1,*p2;
struct student *p3,*p4,*savemin,*saveminfront;
int i,j,min;
i=0;
j=0;
orderhead=NULL;
while(head1!=NULL)
{
p3=p4=head1;
while(p3!=NULL)
{
if(i==0)
{
savemin=p3;
saveminfront=head1;
}
else
{
if((savemin->num)>(p3->num))
{
savemin=p3;
saveminfront=p4;
}
i=1;
}
p4=p3;
p3=p3->next;
}
if(j==0)
{
orderhead=savemin;
p1=p2=savemin;
}
else
{
p1=savemin;
p2->next=p1;
p2=p1;
}
saveminfront->next=savemin->next;
free(savemin);
}
return orderhead;
}
struct student *search(struct student*head,long int searchnum)/*实现查找的功能*/
{
struct student *searchpoint;
struct student *p1,*p2;
p1=p2=head;
while((p1!=NULL)&&((p1->num)!=searchnum))
{
p1=p1->next;
}
searchpoint=p1;
return searchpoint;
}
struct student *alter(struct student *head)/*实现修改的功能*/
{
struct student *alterpoint;
long int num;
char YN;
do
{
printf("Please input student's number which you want to alter\n");
scanf("%ld",&num);
alterpoint=search(head,num);
if(alterpoint==NULL)
{
printf("Not Exit!!!\n");
}
else
{
getstudentmessages(alterpoint);
}
do
{
printf("Do you want to input student's number which you want to alter again 'Y' or 'N'\n");
scanf("%c",&YN);
}while((YN!='Y')&&(YN!='N'));
}while((YN=='Y'));
return head;
}
struct student *del(struct student *head) /*实现按学好排序的功能*/
{
struct student *deletepoint;
struct student *p1,*p2;
long int deletenum;
char YN;
do
{
printf("Please input student's number which you want to delte\n");
scanf("%ld",&deletenum);
p1=p2=head;
while((p1!=NULL)&&((p1->num)!=deletenum))
{
p2=p1;
p1=p1->next;
}
if(p1==NULL)
{
printf("Not Exit\n");
}
else
{
if(p1==head)
{
head=head->next;
I--;
}
else
{
p2->next=p1->next;
I--;
}
}
printf("Do you want input student's number which you want to alter again 'Y' or 'N'\n");
do
{
printf("Please input 'Y' or 'N'");
/* getchar()*/
scanf("%c",&YN);
}while((YN!='Y')&&(YN!='N'));
}while((YN=='Y'));
return head;
}
struct student *insert(struct student *head) /*实现插入学生信息的功能*/
{
struct student *p1,*p2,*p3;
char YN;
clrscr();
do
{
p1=p2=head;
/* if(head==NULL) printf("NULL");*/
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
}
p3=(struct student*)malloc(LEN);
printf("Please input the student's messages you insert\n");
getchar();
insertstudentmessages(p3);
I++;
if(head==NULL)
{
head=p3;
p2=p3;
}
else
{
p2->next=p3;
p2=p3;
}
p2->next=NULL;
printf("Do you want input student's number which you want to alter again 'Y' or 'N'\n");
do
{
printf("Please input 'Y' or 'N'\n");
/* getchar();*/
scanf("%c",&YN);
/* getchar();*/
}while((YN!='Y')&&(YN!='N'));
}while((YN=='Y'));
return head;
}
void print(struct student *p)
{
printf("the messages of the student are:\n\n");
printf("The name of the student:%s\n",p->name);
printf("The number of the student:%ld\n",p->num);
printf("The age of the student:%d\n",p->age);
printf("The sex of the student:%s\n",p->sex);
printf("The address of the student:%s\n",p->address);
printf("The entertime of the student:%s\n",p->entertime);
printf("The telephonenumber of the student:%s\n",p->telephonenumber);
printf("The achievement of the student:%d\n",p->achievement);
getch();
}
main()
{
struct student *head,*p1,*p2,*searchpoint;
long int searchnum,deletenum;
char option;
clrscr();
head=loadfromfile(head);
if(head==NULL)
{
printf("NULL!!\n");
}
do
{
do
{ printf("**********************************************************************\n");
printf("\n");
printf(" Welcome to my students management system\n");
printf("\n");
printf(" name: XieQiang qiang\n");
printf(" studentnumber: 20010498\n");
printf("**********************************************************************\n");
printf("Please input your choice:\n");
printf("1 insert\n");
printf("2 alter\n");
printf("3 delete \n");
printf("4 order\n");
printf("5 loadfromfile\n");
printf("6 saveintofile\n");
printf("7 getstudentmessge\n");
printf("8 search\n");
printf("9 display\n");
scanf("%c",&option);
}while((option!='1')&&(option!='2')&&(option!='3')&&(option!='4')&&(option!='5')&&(option!='6')&&(option!='7')&&(option!='8')&&(option!='9'));
switch(option)
{
case '1': head=insert(head); break;
case '2': head=alter(head); break;
case '3': head=del(head); break;
case '4': head=order4(head);break;
case '5': head=loadfromfile(head);break;
case '6': head=saveintofile(head);break;
case '7': head=getstudentmessages(head);break;
case '8':{
printf("please input the search number:\n");
/* getchar();*/
scanf("%ld",&searchnum);
searchpoint=search(head,searchnum);
print(searchpoint);
break;
}
default:break;
}
}while(option!='9');
p1=head;
while(p1!=NULL)
{
print(p1);
p1=p1->next;
}
saveintofile(head);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -