c语言管理系统
牛~~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{char num[12]; char name[20]; char sex[2]; int score[3]; float avg; int sum;
}Student;Student studentAll[80];
int student_number = 0;
int menu_select()
{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*| 0. 保存退出 *\n");printf("\t*****************************************\n");printf("请输入选择(0-9):");scanf("%d",&check_number); }while(check_number<0||check_number>9);return(check_number);
}
void Input(Student student[])
{int i=0,j;char flag,clear[10]; char studentNum[12]; while(flag!='n'&&flag!='N') {printf("请输入学生学号: ");scanf("%s",studentNum);while(strcmp(student[i].num,studentNum)!=0&&i<student_number){ i++; }if(i<student_number)printf("已有相同学号的学生,请重新输入\n");else { if(i==student_number) {memset(student[i].num, 0, sizeof(student[i].num));strcpy(student[i].num,studentNum);printf("学生姓名:");scanf("%s",student[student_number].name);printf("学生性别:");scanf("%s",student[student_number].sex); printf("c语言成绩:");scanf("%d",&student[student_number].score[0]);printf("数据结构成绩:");scanf("%d",&student[student_number].score[1]);printf("数据库成绩:");scanf("%d",&student[student_number].score[2]);student[student_number].sum=0;for(j=0;j<3;j++)student[student_number].sum+=student[student_number].score[j];student[student_number].avg = (float)student[student_number].sum/3;gets(clear); printf("此学生信息录入完毕,是否继续?(Y/N)");scanf("%c",&flag); student_number++;i=0;}} }}/*显示所有记录*/
void Display(Student student[])
{int i;printf("共有%d名同学\n",student_number);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\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);}system("pause");}
void Sort_by_num(Student student[])
{ int i,j;for(i=0;i<student_number-1;i++){for(j=0;j<student_number-1-i;j++){if(strcmp(student[j].num,student[j+1].num)){Student t;t=student[j];student[j]=student[j+1];student[j+1]=t;}}}}
void Sort_by_sum(Student student[])
{ int i,j;for(i=0;i<student_number-1;i++){for(j=0;j<student_number-1-i;j++){if(student[j].sum<student[j+1].sum){Student t;t=student[j];student[j]=student[j+1];student[j+1]=t;}}}}
void Delete_a_record(Student student[])
{char studentName[20];int i=0;printf("请输入要删除记录的姓名");scanf("%s",studentName);while(strcmp(student[i].name,studentName)!=0&&i<student_number){i++; }int t=i;if(i<student_number){int j=student_number-1;for(;j>i;i++){student[i]=student[i+1];}student_number--;}if(t==student_number){printf("没有此人,请重新输入");}
}
void Query_a_record(Student student[])
{char studentName[20];int i=0;printf("请输入要查找记录的姓名");scanf("%s",studentName);while(strcmp(student[i].name,studentName)!=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\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);}if(i==student_number){printf("没有此人,请重新输入");} }
void modifyStudentinformation(Student student[])
{char studentName[20];int i=0;printf("请输入要修改记录的姓名");scanf("%s",studentName);while(strcmp(student[i].name,studentName)!=0&&i<student_number){i++; }if(i<student_number){printf("修改前的记录:");printf("------------------------------------------------------------------------------------------------------------------------\n");printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");printf("------------------------------------------------------------------------------------------------------------------------\n");printf("%s\t\t%s\t\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学生学号:");scanf("%s",student[i].num);printf("学生姓名:");scanf("%s",student[i].name);printf("学生性别:");scanf("%s",student[i].sex); printf("c语言成绩:");scanf("%d",&student[i].score[0]);printf("数据结构成绩:");scanf("%d",&student[i].score[1]);printf("数据库成绩:");scanf("%d",&student[i].score[2]);for(int j=0;j<3;j++)student[i].sum+=student[i].score[j];student[i].avg = (float)student[i].sum/3;printf("\n修改后的记录:\n");printf("------------------------------------------------------------------------------------------------------------------------\n");printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");printf("------------------------------------------------------------------------------------------------------------------------\n");printf("%s\t\t%s\t\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);}if(i==student_number){printf("没有此人,请重新输入");} }
void showClass(Student student[])
{ char studentName[20];int i=0;printf("请输入要查看各门课程成绩的学生的姓名");scanf("%s",studentName);while(strcmp(student[i].name,studentName)!=0&&i<student_number){i++; }if(i<student_number){printf("***********\n");printf("c语言成绩:%d\n",student[i].score[0]);printf("***********\n\n");printf("**************\n");printf("数据结构成绩:%d\n",student[i].score[1]);printf("**************\n\n");printf("**************\n");printf("数据库成绩:%d\n",student[i].score[2]);printf("**************\n");}if(i==student_number){printf("没有此人,请重新输入");}
}
void AddfromText(Student student[])
{ int i=0;int number = 0;FILE *fp; if((fp=fopen("D:\\student.txt","rb"))==NULL) { fp=fopen("D:\\student.txt","rb") ;}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); }
WritetoText(Student student[])
{FILE *fp;if((fp=fopen("D:\\student.txt","wb"))==NULL){printf("error");}else{fprintf(fp,"%d",student_number);for(int i=0;i<student_number;i++){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);}fclose(fp);}
}
int main()
{AddfromText(studentAll);for(;;){switch(menu_select()) {case 1:printf("插入学生信息\n"); Input(studentAll);break;case 2:printf("显示记录\n"); Display(studentAll);break;case 3:printf("按学号排序\n");Sort_by_num(studentAll); printf("按学号排序成功!\n");Display(studentAll);system("pause");break;case 4:printf("按总成绩排序\n");Sort_by_sum(studentAll); printf("按总成绩排序成功!\n");Display(studentAll);system("pause");break;case 5:printf("查找单个学生\n");Query_a_record(studentAll); system("pause");break;case 6:printf("删除指定学生\n");Delete_a_record(studentAll); system("pause");break;case 7:printf("修改学生信息\n");modifyStudentinformation(studentAll); system("pause");break;case 8:printf("查看各门课程的成绩情况\n");showClass(studentAll); system("pause");break;case 9:printf("待定功能\n");printf("功能描述");system("pause");break;case 0:WritetoText(studentAll);printf("已保存,退出系统\n"); system("pause");}}
}
靓仔点赞