📄 学生成绩管理系统20.cpp
字号:
#include "stdio.h"
#include "string.h"
struct data
{
char names[20];
long number;
float score;
}student;
char filename[20];
char names[20];
void main()
{
void createfile();
void querbynames();
void querybynumbers();
void scotbynumber();
void scotbyscore();
void add();
void cansel();
int a;
while(1)
{
printf("\n");
printf("\n");
printf(" STUDENT SCORES MANAGE SYSTEM");
printf(" \n");
printf("\n");
printf("\n");
printf(" MENU \n");
printf("\n");
printf("\n");
printf(" +====================================================+\n");
printf(" +=========1.Input data and keep score in file========+\n");
printf(" +=========2.Sort by numbers==========================+\n");
printf(" +=========3.Sort by score ===========================+\n");
printf(" +=========4.Query by numbers=========================+\n");
printf(" +=========5.Query by names===========================+\n");
printf(" +=========6.Add======================================+\n");
printf(" +=========7.Delete===================================+\n");
printf(" +=========8.EXIT=====================================+\n");
printf("\n");
printf("Please input your choose:");
scanf("%d",&a);
clrscr();
switch (a)
{
case 1: createfile();break;
case 2: scotbynumber();break;
case 3: scotbyscore();break;
case 4: querybynumbers();break;
case 5: querbynames();break;
case 6: add();break;
case 7: cansel();break;
case 8: exit(0);break;
default:printf("look carefully!!\n");getch();break;
}
}
}
void createfile()
{
FILE *fp;
int count=1;
printf("Please input filename,the computer can creat file!\n");
scanf("%s",filename);
fp=fopen(filename,"w");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while(count<=50)
{
printf("Please input number,input 0 the porgarm can end\n");
scanf("%ld",&student.number);
if(student.number==0)
break;
printf("please input name:");
scanf("%s",student.names);
printf("please input score:");
scanf("%f",&student.score);
if(fwrite(&student,sizeof(student),1,fp)!=1)
{
printf("file write error/n");
}
count++;
}
if (count>50)
printf("\nsorry the record number can't more than 50\n");
fclose(fp);
printf("Your data is like this :");
if(( fp=fopen(filename,"r"))==NULL)
{
printf("cannot open this file\n");
getch();
exit(1);
}
printf("\n number\t\tname\tscore\n");
while(fread(&student,sizeof(student),1,fp)!=NULL)
{
printf("\n%3ld\t%12s\t%7f\n",student.number,student.names,student.score);
}
fclose(fp);
getch();
}
void querbynames()
{
FILE *fp;
struct data
{
char names[20];
long number;
float score;
}stu[50];
char next;
int result;
printf("please input filename what record student data\n");
scanf("%s",filename);
do
{ result=0;
printf("please input names:");
scanf("%s",names);
fp=fopen(filename,"r");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while(feof(fp)!=1)
{
if((fread(&student,sizeof(student),1,fp)!=NULL))
if(strcmp(names,student.names)==0)
{
result=1;
printf("\n%3ld\t%12s\t%7f\n",student.number,student.names,student.score);
break;
}
}
fclose(fp);
if(result==0)
printf("data not found\n");
printf("coutinue??(y/n) ");
printf("\n");
next=getche();
printf("\n");
}while(next=='y'||next=='Y');
}
void querybynumbers()
{
FILE *fp;
struct data
{
char names[20];
long number;
float score;
}stu[50];
char next;
long no;
int result;
printf("please input filename what record student data\n");
scanf("%s",filename);
do
{ result=0;
printf("please input number:");
scanf("%ld",&no);
fp=fopen(filename,"r");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while(feof(fp)!=1)
{
if((fread(&student,sizeof(student),1,fp)!=NULL))
if(student.number==no)
{
result=1;
printf("\n%3ld\t%12s\t%7f\n",student.number,student.names,student.score);
break;
}
}
fclose(fp);
if(result==0)
printf("data not found\n");
printf("coutinue??(y/n) ");
printf("\n");
next=getche();
printf("\n");
}while(next=='y'||next=='Y');
}
void scotbynumber()
{
FILE *fp;
struct data
{
char names[20];
long number;
float score;
}stu[50];
int count=0;
int min_a,temp,i,j;
printf("please input filename what record student data\n");
scanf("%s",filename);
fp=fopen(filename,"r");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while((fread(&student,sizeof(student),1,fp)!=NULL))
{
stu[count].number=student.number;
strcpy(stu[count].names,student.names);
stu[count].score=student.score;
count++;
}
if(count>1)
{
for(i=0;i<count;i++)
{
min_a=i;
for(j=i+1;j<count;j++)
if(stu[j].number<stu[min_a].number)
min_a=j;
temp=stu[min_a].number;
stu[min_a].number=stu[i].number;
stu[i].number=temp;
}
}
fclose(fp);
printf("SORT BY NUMBER RESULT IS\n");
for(i=0;i<count;i++)
{
printf("\n%3ld\t%12s\t%7f\n",stu[i].number,stu[i].names,stu[i].score);
}
getch();
}
void scotbyscore()
{
FILE *fp;
struct data
{
char names[20];
long number;
float score;
}stu[50];
int count=0;
int f,temp,k,max,i;
printf("please input filename what record student data\n");
scanf("%s",filename);
fp=fopen(filename,"r");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while((fread(&student,sizeof(student),1,fp)!=NULL))
{
stu[count].number=student.number;
strcpy(stu[count].names,student.names);
stu[count].score=student.score;
count++;
}
fclose(fp);
if(count>1)
{
for(k=0;k<count;k++)
{
max=k;
for(f=k+1;f<count;f++)
if(stu[f].score>stu[max].score)
max=f;
temp=stu[max].score;
stu[max].score=stu[k].score;
stu[k].score=temp;
}
}
fclose(fp);
printf("SORT BY SCORE RESULT IS\n");
for(i=0;i<count;i++)
{
printf("\n%3ld\t%12s\t%7f\n",stu[i].number,stu[i].names,stu[i].score);
}
getch();
}
void add()
{
FILE *fp;
struct data
{
char names[20];
long number;
float score;
}stu[50];
int count=0;
printf("please input filename what record student data\n");
scanf("%s",filename);
fp=fopen(filename,"r");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while((fread(&student,sizeof(student),1,fp)!=NULL))
{
stu[count].number=student.number;
strcpy(stu[count].names,student.names);
stu[count].score=student.score;
count++;
}
fclose(fp);
fp=fopen(filename,"ab+");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while(count<=50)
{
printf("Please input number,input 0 the porgarm can end\n");
scanf("%ld",&student.number);
if(student.number==0)
break;
printf("please input name:");
scanf("%s",student.names);
printf("please input score:");
scanf("%f",&student.score);
if(fwrite(&student,sizeof(student),1,fp)!=1)
{
printf("file write error/n");
}
count++;
}
if (count>50)
printf("\nsorry the record number can't more than 50\n");
fclose(fp);
printf("Your data is like this :");
if (( fp=fopen(filename,"r"))==NULL)
{
printf("cannot open this file\n");
getch();
exit(1);
}
printf("\n number\t\tname\tscore\n");
while(fread(&student,sizeof(student),1,fp)!=NULL)
{
printf("\n%3ld\t%12s\t%7f\n",student.number,student.names,student.score);
}
fclose(fp);
getch();
}
void cansel()
{
FILE *fp;
struct data
{
char names[20];
long number;
float score;
}stu[50];
int count=0;
int i,n;char y_n,next;
printf("please input filename what record student data\n");
scanf("%s",filename);
fp=fopen(filename,"r");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
while((fread(&student,sizeof(student),1,fp)!=NULL))
{
stu[count].number=student.number;
strcpy(stu[count].names,student.names);
stu[count].score=student.score;
count++;
}
fclose(fp);
while(1)
{
if(count==0)
{ printf("No data in file Any key to continue");
getch();
break;
}
printf(" sequence number name score ");
printf("\n");
for(i=0;i<count;i++)
{
printf("\n%6d\t%12ld\t%12s\t%12f",i+1,stu[i].number,stu[i].names,stu[i].score);
}
printf("\n");
printf("\n");
for(i=0;i<count;i++)
{
printf("please input sequence number:");
scanf("%d",&n);
printf("%ld\t%s\t%f",stu[n-1].number,stu[n-1].names,stu[n-1].score);
printf("\n");
printf("\n");
printf("will be deleted, are you sure?(y/n):");
getchar();
y_n=getche();
if(y_n=='y')
{
count--;
for(i=n-1;i<count;i++)
stu[i]=stu[i+1];
}
}
printf("\n");
printf("\n");
printf("continue to delete?(y/n):");
next=getche();
putchar('\n');
if(next!='Y'&&next!='y')
break;
}
fp=fopen(filename,"w");
if (fp==NULL)
{
printf("can't open file");
getch();
exit(1);
}
for(i=0;i<count;i++)
{
fwrite(&stu[i],sizeof(student),1,fp);
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -