#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef struct List{
char name[8];
int number;
char sex[2];
struct List*pNext;
}NODE,*PNODE;
void menu(void);
PNODE insert_student(); /*添加学生信息*/
void search_student(PNODE pH); /*查询学生信息*/
void delate_student(PNODE pHead); /*删除学生信息*/
void print_student_additon_order(PNODE); /*以添加顺序输出学生信息*/
void print_student_number_order(PNODE); /*以学号输出学生信息*/
void save_to_file(PNODE); /*保存信息至文件*/
void exit_system(void); /*退出程序*/
void read_from_file(void); /*从文件读取信息*/
int len = 0; /*统计学生人数*/
int main(void)
{
char choice;
PNODE pHead = NULL;
PNODE q = NULL;
while(1)
{
menu();
switch(choice - '0')
{
case 1:pHead = insert_student();break;
case 2:search_student(pHead);break;
case 3:delate_student(pHead);break;
case 4:print_student_additon_order(pHead);break;
case 5:print_student_number_order(pHead);break;
case 6:save_to_file(pHead);break;
case 7:read_from_file();break;
case 8:exit_system();break;
default:
break;
}
getch();
scanf("%c",&choice);
fflush(stdin);
}
return 0;
}
PNODE insert_student(void)
{
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if(pHead == NULL)
exit(0);
else
{
PNODE p = pHead;
PNODE pTail = pHead;
pTail->pNext = NULL;
int count = 1,s;
char c;
while(1)
{
printf("请输入第%d个人的信息\n",len+1);
printf("请输入姓名\n");
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(pNew == NULL)
exit(0);
scanf("%s",pNew->name);
fflush(stdin);
printf("请输入他的学号\n");
scanf("%d",&pNew->number);
printf("请输入他的性别(0 = 女,1 = 男)\n");
scanf("%d",&s);
fflush(stdin);
if(s == 1)
strcpy(pNew->sex,"男");
else
strcpy(pNew->sex,"女");
pTail->pNext = pNew; /*链表的尾插法*/
pNew->pNext = NULL;
pTail = pNew;
printf("continued ? (y or n)");
scanf("%c",&c);
++count;
++len;
if(c == 'y' || c == 'Y')
continue;
else
break;
}
return p;
}
}
void menu(void)
{
puts("这是一个学生管理系统\n");
puts("1.添加学生\n");
puts("2.查询学生信息\n");
puts("3.删除学生记录\n");
puts("4.按添加顺序列出记录\n");
puts("5.按学号顺序列出记录\n");
puts("6.保存至文件\n");
puts("7.读取文件\n");
puts("8.退出\n");
puts("********************\n");
puts("请输入功能序号\n");
}
void print_student_additon_order(PNODE pH)
{
PNODE p = pH->pNext;
while(p != NULL)
{
printf("%s\n",p->name);
printf("%d\n",p->number);
printf("%s\n",p->sex);
p = p->pNext;
}
return;
}
void print_student_number_order(PNODE pH)
{
PNODE p,q;
int i,j;
for(i = 0,p = pH->pNext;i < len - 1;i++,p = p->pNext)
{
for(j = i+1,q = p->pNext;j<len;j++,q = q->pNext)
{
if(p->number > q->number)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
strcpy(pNew->name,p->name);
pNew->number = p->number;
strcpy(pNew->sex,p->sex);
strcpy(p->name,q->name);
p->number = q->number;
strcpy(p->sex,q->sex);
strcpy(q->name,pNew->name);
q->number = pNew->number;
strcpy(q->sex,pNew->sex);
}
}
}
print_student_additon_order(pH);
return;
}
void search_student(PNODE pH)
{
PNODE p = pH->pNext;
int type;
char st[8];
int flag;
printf("请输入要查询的形式\n");
printf("1.姓名\n");
printf("2.学号\n");
scanf("%d",&type);
if(type == 1){
printf("请输入要查找学生的姓名\n");
scanf("%s",st);}
else{
printf("请输入要查找学生的学号\n");
scanf("%d",&flag);}
while(p!= NULL)
{
if(type == 1){
if(strcmp(p->name,st) == 0)
break;
else
p = p->pNext;
}
else
{
if(p->number == flag)
break;
else
p = p->pNext;
}
}
if(p == NULL){
printf("没有所查询的任何信息\n");
return;}
printf("%s\n",p->name);
printf("%d\n",p->number);
printf("%s\n",p->sex);
printf("任意键继续...\n");
}
void delate_student(PNODE pH)
{
PNODE p = pH->pNext;
int type;
char st[8];
int flag;
printf("请输入要查询的形式\n");
printf("1.姓名\n");
printf("2.学号\n");
scanf("%d",&type);
if(type == 1){
printf("请输入要删除学生的姓名\n");
scanf("%s",st);}
else{
printf("请输入要删除学生的学号\n");
scanf("%d",&flag);}
while(p->pNext!= NULL)
{
if(type == 1){
if(strcmp(p->pNext->name,st) == 0)
{
PNODE r = p->pNext;
p->pNext = r->pNext;
free(r);
r = NULL;
len--;
}
}
else
{
if(p->pNext->number == flag)
{
PNODE q = p->pNext;
p->pNext = q->pNext;
free(q);
q = NULL;
len--;
}
}
}
if(p == NULL)
{
printf("没有所查询的任何信息\n");
return;
}
else
printf("成功删除,任意键继续.....");
return;
}
void save_to_file(PNODE pH)
{
FILE*fp;
char filename[20];
PNODE p = pH->pNext;
printf("请输入文件完整路径\n");
fflush(stdin);
gets(filename);
if((fp = fopen(filename,"wt")) == NULL)
printf("文件写入失败\n");
else
{
while(p!= NULL){
fprintf(fp,"%s,%d,%s",p->name,p->number,p->sex);
p = p->pNext;}
printf("文件保存成功,任意键继续...\n");
getch();
fclose(fp);
return;
}
}
void exit_system(void)
{
printf("退出中.....\n");
system("pause");
exit(0);
}
void read_from_file(void)
{
FILE*fp;
PNODE pHead,pNew;
char filename[20];
int i = 0;
printf("请输入文件完整路径\n");
gets(filename);
if((fp = fopen(filename,"rt")) == NULL)
printf("文件读取失败\n");
pHead = (PNODE)malloc(sizeof(NODE));
PNODE pTail = pHead;
pTail->pNext = NULL;
while(!feof(fp))
{
pNew = (PNODE)malloc(sizeof(NODE)); /*存放读取信息*/
fflush(stdin);
fscanf(fp,"%s,%d,%s",pNew->name,&pNew->number,pNew->sex);
pTail->pNext = pNew;
pTail = pNew;
}
printf("文件已成功读出,任意键继续...\n");
getch();
fclose(fp);
return;
}