学生管理系统
功能选择界面如下:
*****************************************************
*** 输入学生信息请按1 ***
*** 删除学生信息请按2 ***
*** 查找学生信息请按3 ***
*** 修改学生信息请按4 ***
*** 按排名打印学生信息请按5 ***
*** 其他任意键退出 ***
*** Thu Jun 20 23:14:13 2019 ***
*****************************************************
整体代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int count=0;
typedef struct Student
{
char name[15];
int acanum;
float score;
int age;
}student;
typedef struct Stu
{
student data;
struct Stu *prev;
struct Stu *next;
}stu;
typedef stu * stu_p;
stu_p init(void)
{
stu_p head=(stu_p)malloc(sizeof(stu));
head->prev=head;
head->next=head;
return head;
}
stu_p insert_stu(stu_p head,student *elem)
{
stu_p node=(stu_p)malloc(sizeof(stu));
memcpy(&node->data,elem,sizeof(student));
node->next=head;
node->prev=head->prev;
head->prev->next=node;
head->prev=node;
count++;
return head;
}
student * getinfo(void)
{
student *info=(student *)malloc(sizeof(student));
printf("请输入姓名:\n");
scanf("%s",info->name);
printf("请输入学号:\n");
scanf("%d",&info->acanum);
printf("请输入年龄:\n");
scanf("%d",&info->age);
printf("请输入分数:\n");
scanf("%f",&info->score);
return info;
}
student * regetinfo(void)
{
student *info=(student *)malloc(sizeof(student));
printf("请重新输入姓名:\n");
scanf("%s",info->name);
printf("请重新输入学号:\n");
scanf("%d",&info->acanum);
printf("请重新输入年龄:\n");
scanf("%d",&info->age);
printf("请重新输入分数:\n");
scanf("%f",&info->score);
return info;
}
stu_p delete_stu(stu_p head,char *name)
{
stu_p temp=head->next;
while(temp!=head)
{
if(strcmp(temp->data.name,name)==0)
{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
count--;
printf("删除完成!\n");
return head;
}
temp=temp->next;
}
printf("找不到该学生\n");
return head;
}
stu_p find_stu(stu_p head,char *name)
{
stu_p temp=head->next;
while(temp!=head)
{
if(strcmp(temp->data.name,name)==0)
{
printf("姓名:%-20s年龄:%-10d",temp->data.name,temp->data.age);
printf("学号:%-10d分数:%-10.1f\n",temp->data.acanum,temp->data.score);
return head;
}
temp=temp->next;
}
printf("找不到该学生\n");
return head;
}
stu_p change_stu(stu_p head,char *name)
{
student * elem;
stu_p temp=head->next;
while(temp!=head)
{
if(strcmp(temp->data.name,name)==0)
{
elem=regetinfo();
memcpy(&temp->data,elem,(sizeof(student)));
return head;
}
temp=temp->next;
}
printf("找不到该学生\n");
return head;
}
void show_stu(stu_p head)
{
int n=1;
stu_p temp=head->next;
printf(" 姓名 年龄 学号 分数 \n");
while(temp!=head)
{
printf("%d:%-20s%-10d",n,temp->data.name,temp->data.age);
printf("%-15d%-10.1f\n",temp->data.acanum,temp->data.score);
n++;
temp=temp->next;
}
}
void write_stu(stu_p head)
{
int fd = open("./studen_info.txt",O_RDWR|O_CREAT|O_TRUNC,0664);
int rec_fd=dup(1);
dup2(fd,1);
stu_p temp=head->next;
while(temp!=head)
{
printf("%s %d ",temp->data.name,temp->data.age);
printf("%d %f\n",temp->data.acanum,temp->data.score);
temp=temp->next;
}
dup2(rec_fd,1);
close(fd);
}
stu_p order(stu_p head)
{
stu_p temp=head->next;
int i,j;
student *temp1=(student *)malloc(sizeof(student));
for(i=0;i<count-1;i++)
{
temp=head->next;
for(j=0;j<count-i-1;j++)
{
if((temp->data.score)<(temp->next->data.score))
{
memcpy(temp1,&temp->data,sizeof(student));
memcpy(&temp->data,&temp->next->data,sizeof(student));
memcpy(&temp->next->data,temp1,sizeof(student));
}
temp=temp->next;
}
}
return head;
}
void read_stu(stu_p head)
{
FILE *fp = fopen("./studen_info.txt","r");
int ret_fscanf;
student temp1;
while(1)
{
ret_fscanf = fscanf(fp,"%s%d%d%f",temp1.name,&temp1.age,&temp1.acanum,&temp1.score);
if(ret_fscanf == -1)
{
break;
}
insert_stu(head,&temp1);
}
fclose(fp);
}
int main()
{
stu_p juzo;
juzo=init();
char name1[20];
char name2[20];
char name3[20];
student * kk1;
char ah;
time_t cur_time;
char *time1;
char *time2;
read_stu(juzo);
start: cur_time=time(NULL);
time1=ctime(&cur_time);
time2=strtok(time1,"\n");
printf("*****************************************************\n");
printf("*** 输入学生信息请按1 ***\n");
printf("*** 删除学生信息请按2 ***\n");
printf("*** 查找学生信息请按3 ***\n");
printf("*** 修改学生信息请按4 ***\n");
printf("*** 按排名打印学生信息请按5 ***\n");
printf("*** 其他任意键退出 ***\n");
printf("*** %s ***\n",time2);
printf("*****************************************************\n");
printf("请输入数字选择:\n");
start1: ah=getchar();
switch(ah)
{
case '1' :
kk1=getinfo();
juzo=insert_stu(juzo,kk1);
write_stu(juzo);
goto start1;
break;
case '2' :
printf("请输入你要删除的学生姓名:\n");
scanf("%s",name1);
juzo=delete_stu(juzo,name1);
write_stu(juzo);
goto start1;
break;
case '3' :
printf("请输入你要查找的学生姓名:\n");
scanf("%s",name2);
juzo=find_stu(juzo,name2);
goto start1;
break;
case '4' :
printf("请输入你要修改的学生姓名:\n");
scanf("%s",name3);
juzo=change_stu(juzo,name3);
write_stu(juzo);
goto start1;
break;
case '5' :
order(juzo);
show_stu(juzo);
write_stu(juzo);
goto start1;
break;
case 10 :
goto start;
break;
}
write_stu(juzo);
return 0;
}
注:学生信息保存在"./studen_info.txt"中,需要手动创建文件;否则会运行错误。