新人博主不易,希望看完點贊
#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();
}
}
}