📄
字号:
基于文件的学生管理系统
--------------------------------------------------------------------------------
作者:小汀 2004-7-1 6:32:20 已被阅读217次 编辑 | 删除 | 保存本文到我的公文包
本程序用的是菜单操作。已在tc上调试通过大家用是要在相同目录下加一个student.txt文件,里面的第一行加一个#号就可以使用了!
以下是程序的基本情况
学生基本情况:
(学号,姓名,奖学金,英语成绩,数学成绩,计算机成绩,平均成绩)
功能模块:
1.添加一个学生基本情况的输入并保存到文件中
2.删除一个学生基本情况并保存
3.统计每个学生的平均成绩,并排序输出
4.更改一个学生的某一项或多项信息并保存
相关的代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int num;
char name[9];
int money;
int english;
int math;
int computer;
int average;
struct node *next;
}student_info;
/*该程序的界面*/
char menu()
{
char menu[]={" Students' Information Management(v1.0) \n\r"
" \n\r"
"****************************************** \n\r"
"| | \n\r"
"| 1.Show the students' information | \n\r"
"| 2.Add a new student's information | \n\r"
"| 3.Delet a student's information(num)| \n\r"
"| 4.Show the average marks of student | \n\r"
"| 5.Update a student's information | \n\r"
"| 6.Save the students'information | \n\r"
"| 7.Quit the system | \n\r"
"| | \n\r"
"****************************************** \n\r"
"PLEASE ENTER THE NUMBER(1--7):\n"
};
char ch;
clrscr();
puts(menu);
ch=getch();
if(ch!='1'&&ch!='2'&&ch!='3'&&ch!='4'&&ch!='5'&&ch!='6'&&ch!='7')
return '0';
else
return ch;
}
/*学生的基本情况显示出来*/
void showInfo(student_info *p)
{
student_info *stu;
stu=p->next;
if(stu==NULL)
{
printf("The List is empty!...");
return;
}
while(stu)
{
printf("**********\n");
printf("Num:%d\nName:%s\nMoney:%d\nEnglish:%d\nMath:%d\nComputer:%d\nAverage:%d\n",stu->num,stu->name,stu->money,stu->english,stu->math,stu->computer,stu->average);
printf("**********\n");
stu=stu->next;
}
}
/*添加一个学生的情况*/
void addInfo(student_info *p)
{
student_info *stu,*t;
stu=(student_info *)malloc(sizeof(student_info));
/*数据录入*/
printf("Please enter the information of the student:");
printf("\nStudent--num:");scanf("%d",&stu->num);
printf("\nStudent--name:");scanf("%s",stu->name);
printf("\nStudent--money:");scanf("%d",&stu->money);
printf("\nStudent--english:");scanf("%d",&stu->english);
printf("\nStudent--math:");scanf("%d",&stu->math);
printf("\nStudent--computer:");scanf("%d",&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;
}
}
/*删除一个学生的资料(按学号)*/
void 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!");
}
/*更新一个学生的基本资料*/
void update(student_info *p)
{
student_info *stu;
char ch;
stu=p;
clrscr();
printf("What item do you want to update:\n");
printf("1.num||2.name||3.money||4.english||5.math||6.computer(1--6)\n(End with 0)\n");
start: ch=getch();
{
switch(ch)
{
case '0':
{
stu->average=(stu->english+stu->math+stu->computer)/3;
return;
}
case '1':
{
printf("Please enter the new num:\n");
scanf("%d",&stu->num);
printf("What item do you want to update:\n");
goto start;
}
case '2':
{
printf("Please enter the new name:\n");
scanf("%s",stu->name);
printf("What item do you want to update:\n");
goto start;
}
case '3':
{
printf("Please enter the new money:\n");
scanf("%d",&stu->money);
printf("What item do you want to update:\n");
goto start;
}
case '4':
{
printf("Please enter the new english:\n");
scanf("%d",&stu->english);
printf("What item do you want to update:\n");
goto start;
}
case '5':
{
printf("Please enter the new math:\n");
scanf("%d",&stu->math);
printf("What item do you want to update:\n");
goto start;
}
case '6':
{
printf("Please enter the new computer:\n");
scanf("%d",&stu->computer);
printf("What item do you want to update:\n");
goto start;
}
default:
{
printf("Your input is error!");
printf("What item do you want to update:\n");
goto start;
}
}
}
}
void 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!");
}
/*排序并显示学生的平均成绩*/
void 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=t1->next;
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;
while(stu)
{
printf("The NO.%d is:\nNum:%d\nName:%s\nAverage:%d\n",i,stu->num,stu->name,stu->average);
printf("--------");
stu=stu->next;
i++;
}
}
/*保存整个链表信息到文件中*/
void 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("Wait......\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->money,stu->english,stu->math,stu->computer,stu->average);
stu=stu->next;
}
fputc('#',fp);
fclose(fp);
printf("Success to save the file!\n");
}
/*释放整个链表*/
void freeInfo(student_info *p)
{
student_info *stu,*t;
stu=p->next;
while(stu)
{
t=stu;
stu=stu->next;
free(t);
}
free(p);
}
/*函数从文件中调数据上来*/
void show_file(student_info *stu)
{
FILE *fp;
student_info *p,*q;
char ch;
int i=0;
q=stu;
fp=fopen("student.txt","rt");
if(fp==NULL)
{
printf("File error!\npress any key to exit!...");
getch();
exit(1);
}
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",&p->num,p->name,&p->money,&p->english,&p->math,&p->computer,&p->average);
q->next=p;
i++;
q=p;
p++;
ch=fgetc(fp);
}
if(i==0)
{
printf("The file is empty!\npress any key to continue...");
getch();
return;
}
else
{
q->next=NULL;
printf("There is %d students' information!\npress any key...",i);
getch();
return;
}
}
/*退出整个系统*/
char exitSys()
{
char ch;
程序允许你三次输入口令
把此程序加在你的程序的合适位置即可达到要输入口令才行运行程序的目的#include "stdio.h"
#include "string.h"
main()
{int i;
char s[]="QQ962041";\\this is the password
char pwd[9];
printf("please input the password\n");
gets(pwd);
for(i=0;i<2;i++)
if(strcmp(pwd,s)==0)
printf("password OK",i=3);\\就加在这里
else {printf("password error.please input the password again:\n");
gets(pwd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -