#include<stdlib.h>
#include<string.h>
#include <stdio.h>
#define LEN sizeof(T_Student)
#define FILENAME "studentData.txt"
int menu();
int display();
int addInfo();
int delInfo();
int query();
int modified();
int sort();
int Statistics();
typedef struct student
{ char Name[16];
int Id;
float Chinese;
float Math;
float English;
float Total;
float Avrg;
}T_Student;
T_Student g_TStuArray[128];
static int g_StuNum = 0;
int main()
{ int i;
FILE *fp;
memset(&g_TStuArray, 0x00, sizeof(g_TStuArray));
if((fp=fopen(FILENAME,"ab+"))==NULL)
{
printf("Open File[%s] Error!", FILENAME);
exit(1);
}
while(!feof(fp))
{
if(fread(&g_TStuArray[g_StuNum],LEN,1,fp)==1)
{
g_StuNum++;
}
}
fclose(fp);
printf("\n\n\n-------------------------欢迎来到学生成绩管理系统------------------------------\n\n\n");
while (1)
{
system("cls");
menu();
}
return 0;
}
int menu()
{ int n;
system("cls");
printf("\t\t-----------------------------------------\n");
printf("\t\t| |\n");
printf("\t\t| 成绩管理系统 |\n");
printf("\t\t| |\n");
printf("\t\t| 1.显示成绩信息 2.增加学生信息 |\n");
printf("\t\t| |\n");
printf("\t\t| 3.删除学生信息 4.修改学生信息 |\n");
printf("\t\t| |\n");
printf("\t\t| 5.成绩查询 6.排序 |\n");
printf("\t\t| |\n");
printf("\t\t| 7.成绩统计 8.退出当前界面 |\n");
printf("\t\t| |\n");
printf("\t\t-----------------------------------------\n\n\n");
printf("\t请输入您要选择的操作:");
scanf("%d",&n);
getchar();
printf("\n\n");
switch(n)
{
case 1: display(); break; /* 显示 */
case 2: addInfo(); break; /* 增加 */
case 3: delInfo(); break; /* 删除 */
case 4: modified(); break; /* 修改 */
case 5: query(); break; /* 查询 */
case 6: sort(); break; /* 排序 */
case 7: Statistics(); break; /* 统计 */
case 8: exit(1); break; /* 退出 */
default:
printf("Input Error!\n");
break;
}
return 0;
}
int display()
{ int i=0;
system("cls");
printf("学生成绩信息\n");
printf(" 序号\t姓名\t\t编号\t语文\t数学\t英语\t平均分\t总分\n");
for (i = 0; i < g_StuNum; i++)
{
printf("序号%-4d%-15s%6d\t%.2f \t%.2f \t%.2f \t%.2f \t%.2f\n", i+1,
g_TStuArray[i].Name, g_TStuArray[i].Id,g_TStuArray[i].Chinese,g_TStuArray[i].Math,
g_TStuArray[i].English, g_TStuArray[i].Avrg, g_TStuArray[i].Total);
}
printf("共有记录数:%d\n",g_StuNum);
getchar();
return 0;
}
int addInfo()
{
FILE *fp;
printf("请输入:姓名|序号|语文成绩|数学成绩|英语成绩,以空格分隔\n");
while (1)
{
char ch;
scanf("%s %d %f %f %f", &g_TStuArray[g_StuNum].Name, &g_TStuArray[g_StuNum].Id,
&g_TStuArray[g_StuNum].Chinese, &g_TStuArray[g_StuNum].Math, &g_TStuArray[g_StuNum].English);
getchar();
g_TStuArray[g_StuNum].Total = (g_TStuArray[g_StuNum].Chinese + g_TStuArray[g_StuNum].Math + g_TStuArray[g_StuNum].English);
g_TStuArray[g_StuNum].Avrg = g_TStuArray[g_StuNum].Total / 3;
g_StuNum++;
printf("继续添加:<Y/N>");
ch = getchar();
if (ch == 'Y' || ch == 'y') {
continue;
} else if (ch == 'N' || ch == 'n') {
break;
} else {
printf("输入错误!请输入Y/N\n");
}
getchar();
}
if((fp=fopen(FILENAME,"wb"))==NULL)
{
perror("fopen");
exit(1);
}
fwrite(&g_TStuArray, sizeof(T_Student), g_StuNum, fp);
fflush(fp);
fclose(fp);
// printf("TestLine:%s,%d,%.2f,%.2f,%.2f", tmp.Name, tmp.Id, tmp.Chinese, tmp.Math, tmp.English);
// getchar();
return 0;
}
int delInfo()
{
int i = 0;
int iID = 0;
FILE *fp = NULL;
system("cls");
printf("删除学生成绩信息\n");
printf("请输入待删除学生编号:");
scanf("%d", &iID);
getchar();
printf("姓名\t\t编号\t语文\t数学\t英语\t\n");
for (i = 0; i < g_StuNum; i++)
{
if (iID == g_TStuArray[i].Id)
{
printf("%-15s%6d\t%.2f \t%.2f \t%.2f \t%.2f \t%.2f\n", g_TStuArray[i].Name, g_TStuArray[i].Id,
g_TStuArray[i].Chinese,g_TStuArray[i].Math,g_TStuArray[i].English, g_TStuArray[i].Avrg, g_TStuArray[i].Total);
int j = i;
for (j = i; j < g_StuNum; j++)
{
g_TStuArray[j] = g_TStuArray[j+1];
}
g_StuNum --;
}
}
if((fp=fopen(FILENAME,"w"))==NULL)
{
perror("fopen");
exit(1);
}
fwrite(&g_TStuArray, sizeof(T_Student), g_StuNum, fp);
fflush(fp);
getchar();
return 0;
}
int query()
{
int i = 0;
int iID = 0;
system("cls");
printf("查询学生成绩信息\n");
printf("请输入待查询学生编号:");
scanf("%d", &iID);
getchar();
printf("姓名\t\t编号\t语文\t数学\t英语\t平均分\t总分\n");
for (i = 0; i < g_StuNum; i++)
{
if (iID == g_TStuArray[i].Id)
{
printf("%-15s%6d\t%.2f \t%.2f \t%.2f \t%.2f \t%.2f\n", g_TStuArray[i].Name, g_TStuArray[i].Id,
g_TStuArray[i].Chinese,g_TStuArray[i].Math,g_TStuArray[i].English, g_TStuArray[i].Avrg, g_TStuArray[i].Total);
}
}
getchar();
return 0;
}
int modified()
{
int i = 0;
int iID = 0;
FILE *fp = NULL;
system("cls");
printf("修改学生成绩信息\n");
printf("请输入待修改学生编号:");
scanf("%d", &iID);
getchar();
printf("姓名\t\t编号\t语文\t数学\t英语\n");
for (i = 0; i < g_StuNum; i++)
{
if (iID == g_TStuArray[i].Id)
{
printf("请输入修改内容:姓名|序号|语文成绩|数学成绩|英语成绩,以空格分隔\n");
scanf("%s %d %f %f %f", &g_TStuArray[i].Name, &g_TStuArray[i].Id,
&g_TStuArray[i].Chinese, &g_TStuArray[i].Math, &g_TStuArray[i].English);
getchar();
g_TStuArray[i].Total = (g_TStuArray[i].Chinese + g_TStuArray[i].Math + g_TStuArray[i].English);
g_TStuArray[i].Avrg = g_TStuArray[i].Total / 3;
printf("%-15s%6d\t%.2f \t%.2f \t%.2f\n", g_TStuArray[i].Name, g_TStuArray[i].Id,
g_TStuArray[i].Chinese,g_TStuArray[i].Math,g_TStuArray[i].English, g_TStuArray[i].Avrg, g_TStuArray[i].Total);
}
}
if((fp=fopen(FILENAME,"w"))==NULL)
{
perror("fopen");
exit(1);
}
fwrite(&g_TStuArray, sizeof(T_Student), g_StuNum, fp);
fflush(fp);
getchar();
return 0;
}
inline int cmpName(const void *a, const void *b)
{
return strcmp( (*(T_Student *)a).Name , (*(T_Student *)b).Name) >= 0 ? 1: -1;
}
inline int cmpId(const void *a, const void *b)
{
return (*(T_Student*)a).Id >= (*(T_Student*)b).Id ? 1 : -1;
}
inline int cmpCN(const void *a, const void *b)
{
return (*(T_Student*)a).Chinese <= (*(T_Student*)b).Chinese ? 1 : -1;
}
inline int cmpMath(const void *a, const void *b)
{
return (*(T_Student*)a).Math <= (*(T_Student*)b).Math ? 1 : -1;
}
inline int cmpEnglish(const void *a, const void *b)
{
return (*(T_Student*)a).English <= (*(T_Student*)b).English ? 1 : -1;
}
inline int cmpAvrg(const void *a, const void *b)
{
return (*(T_Student*)a).Avrg <= (*(T_Student*)b).Avrg ? 1 : -1;
}
int sort()
{
int iNum = 0;
system("cls");
printf("排序学生成绩信息\n");
printf("请输入待排序编号:1-按姓名,2-按编号,3-按语文,4-按数学,5-按英语,0-总分/平均分");
scanf("%d", &iNum);
getchar();
switch(iNum)
{
case 0: qsort(g_TStuArray, g_StuNum, sizeof(g_TStuArray[0]),cmpAvrg); break;
case 1: qsort(g_TStuArray, g_StuNum, sizeof(g_TStuArray[0]),cmpName); break;
case 2: qsort(g_TStuArray, g_StuNum, sizeof(g_TStuArray[0]),cmpId); break;
case 3: qsort(g_TStuArray, g_StuNum, sizeof(g_TStuArray[0]),cmpCN); break;
case 4: qsort(g_TStuArray, g_StuNum, sizeof(g_TStuArray[0]),cmpMath); break;
case 5: qsort(g_TStuArray, g_StuNum, sizeof(g_TStuArray[0]),cmpEnglish); break;
default: printf("输入错误\n"); exit(1);
}
display();
return 0;
}
int Statistics()
{
int i = 0;
float CNSum = 0;
float MathSum = 0;
float ENSum = 0;
for (i = 0; i < g_StuNum; i++)
{
CNSum += g_TStuArray[i].Chinese;
MathSum += g_TStuArray[i].Math;
ENSum += g_TStuArray[i].English;
}
system("cls");
printf("统计学生成绩信息\n");
printf("\t语文\t数学\t英语\n");
printf("平均分: %.2f\t%.2f\t%.2f\n", CNSum/g_StuNum, MathSum/g_StuNum, ENSum/g_StuNum);
printf("总分 : %.2f\t%.2f\t%.2f\n", CNSum, MathSum, ENSum);
getchar();
return 0;
}
温馨提示:答案为网友推荐,仅供参考