新人博主不易,希望看完点赞
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#define MAXSIZE 50
typedef struct
{
char num[12];
char name[20];
char sex[4];
int score[3];
float avg;
int sum;
}Student;
int student_number=0;
Student student[MAXSIZE];
int Menu()
{
int check_number;
do{
system("cls");
printf("\t************学生成绩管理系统*************\n");
printf("\t*| 1. 添加学生信息 *\n");
printf("\t*| 2. 显示学生信息 *\n");
printf("\t*| 3. 按学号排序 *\n");
printf("\t*| 4. 按总成绩排序 *\n");
printf("\t*| 5. 查找单个学生 *\n");
printf("\t*| 6. 删除指定学生 *\n");
printf("\t*| 7. 修改学生信息 *\n");
printf("\t*| 8. 查看各门课程的平均分 *\n");
printf("\t*| 9. 查看不及格的学生的信息 *\n");
printf("\t*| 10. 清空已保存数据 *\n");
printf("\t*| 0. 保存退出 *\n");
printf("\t*****************************************\n");
printf("请输入选择(0-10):");
scanf("%d",&check_number);
if(check_number<0||check_number>10)
{
printf("错误选择");
system("pause");
}
}while(check_number<0||check_number>10);
return check_number;
}
void Input()
{
int i;
char choice='y';
char clear[10];
char student_num[12];
do
{
i=0;
printf("请输入学号\t: ");
scanf("%s",student_num);
while(strcmp(student[i].num,student_num)!=0&&i<student_number)
{
i++;
}
if(i<student_number)
{
printf("学号已存在,请重新输入\n");
}
else
{
strcpy(student[i].num,student_num);
printf("学生姓名\t: ");
scanf("%s",student[i].name);
printf("学生性别\t: ");
scanf("%s",student[i].sex);
printf("c语言成绩\t: ");
scanf("%d",&student[i].score[0]);
printf("数据结构成绩\t: ");
scanf("%d",&student[i].score[1]);
printf("数据库成绩\t: ");
scanf("%d",&student[student_number].score[2]);
student[i].avg=(float)(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;
student[i].sum=student[i].score[0]+student[i].score[1]+student[i].score[2];
gets(clear);
printf("此学生信息录入完毕,是否继续?(Y/N) :");
scanf("%c",&choice);
student_number++;
gets(clear);
}
} while (choice=='Y'||choice=='y');
}
void Show()
{
int i;
printf("---------------------------------------------------------------------------------------------------------------------\n");
printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
printf("---------------------------------------------------------------------------------------------------------------------\n");
for(i=0;i<student_number;i++)
{
printf("%s\t\t%s \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
}
}
void SortByNum()
{
Student temp;
int i,j;
for(i=0;i<student_number;i++)
{
for(j=0;j<student_number-i-1;j++)
{
if(strcmp(student[j].num,student[j+1].num)>0)
{
temp=student[j];
student[j]=student[j+1];
student[j+1]=temp;
}
}
}
Show();
}
void SortBySum()
{
Student temp;
int i,j;
for(i=0;i<student_number;i++)
{
for(j=0;j<student_number-i-1;j++)
{
if(student[j].sum<student[j+1].sum)
{
temp=student[j];
student[j]=student[j+1];
student[j+1]=temp;
}
}
}
Show();
}
void Search()
{
int i=0;
int choice;
char testnum[12];
start :
printf("请输入学号 : ");
scanf("%s",testnum);
i=0;
while(strcmp(testnum,student[i].num)!=0&&i<student_number)
{
i++;
}
if(i<student_number)
{
printf("------------------------------------------------------------------------------------\n");
printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
printf("------------------------------------------------------------------------------------\n");
printf("%s\t\t%s \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
printf("若继续查找按1,退出按0 :");
}
else
{
printf("查无此人,若继续查找按1,退出按0 :");
}
scanf("%d",&choice);
if(choice)
{
goto start;
}
}
void Delete()
{
int i=0,j;
int choice;
char testnum[12];
start :
printf("请输入学号 : ");
scanf("%s",testnum);
while(strcmp(testnum,student[i].num)!=0&&i<student_number)
{
i++;
}
if(i<student_number)
{
for(j=i;j<student_number-1;j++)
{
student[j]=student[j+1];
}
student_number--;
printf("删除成功,若继续查找按1,退出按0 :");
}
else
{
printf("查无此人,若继续查找按1,退出按0 :");
}
scanf("%d",&choice);
if(choice)
{
goto start;
}
}
void Modify()
{
int i=0,j;
int choice;
char testnum[12];
start :
printf("请输入学号 : ");
scanf("%s",testnum);
while(strcmp(testnum,student[i].num)!=0&&i<student_number)
{
i++;
}
if(i<student_number)
{
printf("-----------------------------------------修改前的数据------------------------------------------------------------------\n");
printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
printf("-----------------------------------------------------------------------------------------------------------------------\n");
printf("%s\t\t%s \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
printf("开始修改\n");
printf("学生学号\t: ");
scanf("%s",student[i].num);
printf("学生姓名\t: ");
scanf("%s",student[i].name);
printf("学生性别\t: ");
scanf("%s",student[i].sex);
printf("c语言成绩\t: ");
scanf("%d",&student[i].score[0]);
printf("数据结构成绩\t: ");
scanf("%d",&student[i].score[1]);
printf("数据库成绩\t: ");
scanf("%d",&student[i].score[2]);
student[i].avg=(float)(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;
student[i].sum=student[i].score[0]+student[i].score[1]+student[i].score[2];
printf("-----------------------------------------修改后的数据------------------------------------------------------------------\n");
printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
printf("-----------------------------------------------------------------------------------------------------------------------\n");
printf("%s\t\t%s \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
printf("修改成功,若继续查找按1,退出按0 :");
}
else
{
printf("查无此人,若继续查找按1,退出按0 :");
}
scanf("%d",&choice);
if(choice)
{
goto start;
}
}
void Show_avg()
{
int temp0=0,temp1=0,temp2=0;
float a,b,c;
int i;
for(i=0;i<student_number;i++)
{
temp0=temp0+student[i].score[0];
temp1=temp1+student[i].score[1];
temp2=temp2+student[i].score[2];
}
a=(float)(temp0/student_number);
b=(float)(temp1/student_number);
c=(float)(temp2/student_number);
printf("c语言成绩平均分是%-.2f\n数据结构成绩平均分是%-.2f\n数据库成绩平均分是%-.2f\n",a,b,c);
}
void Show_Max_And_Min()
{
int i,j=0,max,min;
int keep_score[MAXSIZE];
for(i=0;i<student_number;i++)
{
if(student[i].score[0]<60||student[i].score[1]<60||student[i].score[2]<60)
{
keep_score[j]=i;
j++;
}
}
j--;
printf("------------------------------------------------------------------------------------\n");
printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
printf("------------------------------------------------------------------------------------\n");
for(i=0;i<=j;i++)
{
printf("%s\t\t%s \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[keep_score[i]].num,student[keep_score[i]].name,student[keep_score[i]].sex,student[keep_score[i]].score[0],student[keep_score[i]].score[1],student[keep_score[i]].score[2],student[keep_score[i]].avg,student[keep_score[i]].sum);
}
}
void AddFromText()
{
FILE *fp;
int i=0;
if((fp=fopen("D:\\student.txt","r"))==NULL)
{
printf("打开文件失败,无读取数据");
Sleep(1000);
}
else
{
fscanf(fp,"%d",&student_number);
while(i<student_number)
{
fscanf(fp,"%s%s%s%d%d%d%f%d",student[i].num,student[i].name,student[i].sex,&student[i].score[0],&student[i].score[1],&student[i].score[2],&student[i].avg,&student[i].sum);
i++;
}
}
fclose(fp);
}
void Write()
{
int i=0;
FILE *fp;
if((fp=fopen("D:\\student.txt","w"))==NULL)
{
printf("保存失败");
system("pause");
}
else
{
fprintf(fp,"%d",student_number);
while(i<student_number)
{
fprintf(fp,"\t%s\t%s\t%s\t%d\t%d\t%d\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
i++;
}
fclose(fp);
printf("保存成功,正在退出系统");
Sleep(1000);
exit(0);
}
}
void FreeAll()
{
FILE *fp;
int choice;
printf("请确认是否清除,保存的信息删除无法恢复,确认清除输入1,否则输入0返回系统 : ");
scanf("%d",&choice);
if(choice)
{
if((fp=fopen("D:\\student.txt","w"))==NULL)
{
printf("清除失败");
system("pause");
}
else
{
printf("清除成功\n");
fclose(fp);}
}
}
int main()
{
AddFromText();
for(;;)
{
switch(Menu())
{
case 1:Input();system("pause");continue;
case 2:Show();system("pause");continue;
case 3:SortByNum();system("pause");continue;
case 4:SortBySum();system("pause");continue;
case 5:Search();system("pause");continue;
case 6:Delete();system("pause");continue;
case 7:Modify();system("pause");continue;
case 8:Show_avg();system("pause");continue;
case 9:Show_Max_And_Min();system("pause");continue;
case 10:FreeAll();system("pause");continue;
case 0:Write();
}
}
}