📄 library management.txt
字号:
/*源程序*/
/*头文件*/
#include "stdio.h" /*I/O函数*/
#include "stdlib.h" /*其它说明*/
#include "string.h" /*字符串函数*/
#include "conio.h" /*屏幕操作函数*/
#include "mem.h" /*内存操作函数*/
#include "ctype.h" /*字符操作函数*/
#include "alloc.h" /*动态地址分配函数*/
/*常量定义*/
#define NULL 0
#define OK 1
/*数据类型*/
typedef struct node
{
int num;
char name[9];
int Scholarship;
int english;
int math;
int computer;
int average;
struct node *next;
}student_info;
/*函数原型*/
int menu();
int showInfo(student_info *p);
int addInfo(student_info *p);
int deleInfo(student_info *p);
int update(student_info *p);
int update(student_info *p);
int updateInfo(student_info *p);
int sortInfo(student_info *p);
int saveInfo(student_info *p);
int show_file(student_info *stu);
char quitSys();
/*主函数*/
void main()
{
int ch;
char mark;
student_info *p;
p=(student_info *)malloc(sizeof(student_info));
p->next=NULL;
textcolor(YELLOW);
textbackground(BLUE);
printf("\n\n\n**WELLCOME to NORTHESTEN UNIVERSITY SOFTWARE COLLEGE'S LIBRARY Management!**\n\n");
show_file(p);
ch=menu();
while(1)
{
switch(ch)
{
case'0':
{
printf("\nInput error!Press any key contiune!...");
getch();
break;
}
case'1':
{
showInfo(p);
printf("\nPress any key to continue!...");
getch();
break;
}
case'2':
{
addInfo(p);
printf("\nPress any key to continue!...");
getch();
break;
}
case'3':
{
deleInfo(p);
printf("\nPress any key to continue!...");
getch();
break;
}
case'4':
{
sortInfo(p);
printf("\nPress any key to continue!...");
getch();
break;
}
case'5':
{
updateInfo(p);
printf("\nPress any key to continue!...");
getch();
break;
}
case'6':
{
saveInfo(p);
printf("\nPress any key to continue!...");
getch();
break;
}
case'7':
{
mark=quitSys();
if(mark==OK)
{
saveInfo(p);
freeInfo(p);
exit(1);
}
else break;
}
}
}
}
/*程序界面*/
int menu()
{
int i=0;
int c=8;
char s[3];
char menu[]={"\n\n\nStudents' Information Management\n\n\n"
"***************MENU**************\n"
"1,Show the students' information\n"
"2,Add a new students' information\n"
"3,Delet a student's information\n"
"4,Show the average marks of student\n"
"5,Update a student's information\n"
"6,Save the student's information\n"
"7,quit the system\n"
};
gotoxy(1,25);
printf("press any key enter menu......\n");
getch();
clrscr();
gotoxy(1,1);
textcolor(YELLOW);
textbackground(BLUE);
gotoxy(10,2); /*移动光标*/
putch(0xc9);
for(i=1;i<64;i++)
putch(0xcd); /*输出上边框水平线*/
putch(0xbb); /*输出右上角边框┓*/
for(i=3;i<20;i++)
{
gotoxy(10,i);
putch(0xba); /*输出左右垂直线*/
gotoxy(74,i);
putch(0xba);
}
gotoxy(10,20);putch(0xc8); /*输出左下角边框┗ */
for(i=1;i<64;i++)
putch(0xcd); /*输出下边框水平线*/
putch(0xbc); /*出右下角边框┛*/
window(11,3,73,19); /*制作显示菜单的窗口,大小根据菜单条数设计*/
clrscr();
for(i=0;i<9;i++) /*输出主菜单数组*/
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLUE);
window(1,1,80,25);
gotoxy(10,21);
do
{
printf("\n Enter you choice(0~7):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>7);
return c;
}
/*显示学生的基本情况*/
int showInfo(student_info *p)
{
student_info *stu;
stu=p->next;
if(stu==NULL)
{
printf("The List is empty!...");
return;
}
textcolor(RED);
textbackground(GREEN);
printf("\n\n\n*********students**********\n\n");
printf("|Num: |Name: |Scholarship:|English:|Math: |Computer:|Average:|");
while(stu)
{
printf("|%d |%s |%d |%d |%d |%d |%d |",stu->num,stu->name,stu->Scholarship,stu->english,stu->math,stu->computer,stu->average);
stu=stu->next;
}
}
/*添加一个学生记录*/
int addInfo(student_info *p)
{
student_info *stu,*t;
stu=(student_info *)malloc(sizeof(student_info));
printf("Please enter the information of the student:");
printf("|Num: |Name: |Scholarship:|English:|Math: |Computer:|");
scanf("|%d |%s |%d |%d |%d |%d |",&stu->num,&stu->name,&stu->Scholarship,&stu->english,&stu->math,&stu->computer);
stu->average=(stu->english+stu->math+stu->computer)/3;
t=p->next;
if(t==NULL)
{
p->next=stu;
stu->next=NULL;
}
else
{
p->next=stu;
stu->next=t;
}
}
/*删除一个学生的资料*/
int deleInfo(student_info *p)
{
student_info *stu,*t;
int i,mark=0;
char ch;
t=p;
stu=t->next;
if(stu==NULL)
{
printf("The list is empty!...");
return ;
}
printf("Please enter the num of the student:\n");
scanf("%d",&i);
while(stu)
{
if(stu->num==i)
{
printf("Really delet it ?(Y or N):\n");
ch=getch();
if(ch=='y'||ch=='Y')
{
printf("Wait.....\n");
t->next=stu->next;
free(stu);
mark=1;
printf("Success delet the student's information!");
}
else return;
}
else
{
t=t->next;
stu=stu->next;
}
}
if(mark==0)
printf("The student of this num is not exit!");
}
/*修改一个学生的资料*/
int update(student_info *p)
{
student_info *stu;
char ch;
stu=p;
clrscr();
printf("\nWhich item do u wanna update:\n\n");
printf("1,num /2,name /3,Scholarship /4,english /5,math /6,computer(end with 0)\n\n");
while((ch=getch())!='0')
{
switch(ch)
{
case'1':
{
printf("Please enter the new num:\n");
scanf("%d",&stu->num);
printf("which item do u want to update:\n");
break;
}
case'2':
{
printf("Please enter the new name:\n");
scanf("%s",&stu->name);
printf("which item do u want to update:\n");
break;
}
case'3':
{
printf("Please enter the new Scholarship:\n");
scanf("%d",&stu->Scholarship);
printf("which item do u want to update:\n");
break;
}
case'4':
{
printf("Please enter the new english:\n");
scanf("%d",&stu->english);
printf("which item do u want to update:\n");
break;
}
case'5':
{
printf("Please enter the new math:\n");
scanf("%d",&stu->math);
printf("which item do u want to update:\n");
break;
}
case'6':
{
printf("Please enter the new computer:\n");
scanf("%d",&stu->computer);
printf("which item do u want to update:\n");
break;
}
default:
{
printf("what u input is error");
printf("which item do u want to update:\n");
break;
}
}
}
return;
}
int updateInfo(student_info *p)
{
student_info *stu,*t;
int i,mark=0;
t=p;
stu=t->next;
if(stu==NULL)
{
printf("The list is empty!...");
return;
}
printf("Please enter the num of the student:\n");
scanf("%d",&i);
while(stu)
{
if(stu->num==i)
{
update(stu);
mark=1;
return;
}
else
{
t=t->next;
stu=stu->next;
}
}
if(mark==0)
printf("The student of this num is not exit!");
}
/*学生成绩排序*/
int sortInfo(student_info *p)
{
int i=1;
student_info *stu,*t1,*t2;
stu=p;
if(stu->next==NULL)
{
printf("There is no student's information!");
return;
}
t1=stu->next;
t2=stu->next;
textcolor(RED);
textbackground(GREEN);
while(t1)
{
while(t2)
{
if(t1->average>t2->average)
{
stu->next=t2;
t1->next=t2->next;
t2->next=t1;
}
t2=t2->next;
}
t1=t1->next;
stu=stu->next;
}
stu=p->next;
printf("---------------------------------------");
printf("|No: |Num: |Name: |Average: |");
while(stu)
{
printf("|%d |%d |%s |%d |",i,stu->num,stu->name,stu->average);
stu=stu->next;
i++;
}
printf("---------------------------------------");
}
/*保存信息到磁盘*/
int saveInfo(student_info *p)
{
student_info *stu;
FILE *fp;
stu=p->next;
if(stu==NULL)
{
printf("The list is empty!");
return;
}
fp=fopen("student.txt","wt");
if(fp==NULL)
{
printf("The file open error!");
return;
}
if(stu==NULL)
{
printf("The list is empty!");
fputc('#',fp);
fclose(fp);
return;
}
printf("\n\nWait......\n\n");
while(stu)
{
fputc('*',fp);
fprintf(fp,"%d\n%s\n%d\n%d\n%d\n%d\n%d\n",stu->num,stu->name,stu->Scholarship,stu->english,stu->math,stu->computer,stu->average);
stu=stu->next;
}
fputc('#',fp);
fclose(fp);
printf("Success to save the file!\n");
}
/*释放整个链表*/
int freeInfo(student_info *p)
{
student_info *stu,*t;
t=p;
stu=t->next;
while(stu)
{
t->next=stu->next;
free(stu);
/*t=t->next;*/
stu=t->next;
}
free(p);
}
/*从磁盘读取文件*/
int show_file(student_info *stu)
{
FILE *fp;
student_info *p,*q=NULL;
char ch,file[10];
int i=0;
q=stu;
printf("please input the file name:\n");
scanf("%s",file);
if((fp=fopen(file,"r"))==NULL)
{
printf("\n\nFile error!\nPress any key to exit!...");
getch();
exit(1);
}
printf("dfffdfgfdddf");
ch=fgetc(fp);
while(ch!='#')
{
p=(student_info *)malloc(sizeof(student_info));
fscanf(fp,"%d\n%s\n%d\n%d\n%d\n%d\n%d\n",&stu->num,&stu->name,&stu->Scholarship,&stu->english,&stu->math,&stu->computer,&stu->average);
q->next=p;
i++;
q=p;
p++;
ch=fgetc(fp);
}
if(i==0)
{
printf("\nThe file is empty\nPress any key to continue...");
getch();
return;
}
else
{
q->next=NULL;
printf("There are %d students' information!\nPress any key to continue...",i);
getch();
return;
}
}
/*退出系统*/
char quitSys()
{
char ch;
printf("Quit the system?(Y or N):");
ch=getch();
if(ch=='Y'||ch=='y')
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -