#include
#include
void order();
void average();
void inquirexh();
int x, y, z;
struct Student
{
long xuehao;
char xingming[30];
float shuxue;
float yuwen;
float yingyu;
float zongcj;
};
struct Student student[200],stu;
struct Student *p=student;
int main()
{
char l;
FILE *fout;
printf("请问要输入几个学生的成绩: \n");
scanf("%d", x);
if ((fout = fopen("d:\stud.txt", "wb")) == NULL)
{
printf("不能打开文件! \n");
exit(0);
}
for (y = 0; y < x; y++, p++)
{
printf("学号: \n");
scanf("%ld", &p->xuehao);
fwrite(&p->xuehao, sizeof(long), 1, fout);
printf("姓名 \n");
scanf("%s", &p->xingming);
fwrite(&p->xingming, sizeof(char), 1, fout);
printf("数学 \n");
scanf("%f", &p->shuxue);
fwrite(&p->shuxue, sizeof(float), 1, fout);
printf("语文 \n");
scanf("%f", &p->yuwen);
fwrite(&p->yuwen, sizeof(float), 1, fout);
printf("英语 \n");
scanf("%f", &p->yingyu);
fwrite(&p->yingyu, sizeof(float), 1, fout);
p->zongcj = p->shuxue + p->yuwen + p->yingyu;
fwrite(&p->zongcj, sizeof(float), 1, fout);
}
abc:printf
("请问要进行什么运算 \n1.排序 \n2.输出全班同学的平均成绩 \n3.通过学号查询某同学成绩 \n");
scanf("%d", z);
switch (z)
{
case 1:
order();
break;
case 2:
average();
break;
case 3:
inquirexh();
break;
}
printf
("是否继续 y(yes)or n(no)");
scanf("%d", &l);
if (l == y)
goto abc;
else
printf("谢谢使用!请关闭窗口!");
}
void order()
{
int i, j, k, g;
char h;
loop:printf
("请问进行什么排序 \n1.按学号排序 \n2.按总成绩排序 \n3.按数学成绩排序 \n4.按语文成绩排序 \n5.按英语成绩排序 \n");
scanf("%d", &g);
switch (g)
{
case 1:
for (i = 0; i < x - 1; i++)
{
for (j = 0; j < x - i - 1; j++, p++)
{
if ((p->xuehao) > ((p + 1)->xuehao))
{
stu = student[j] ;
student[j] = student[j+1];
student[j+1] = stu;
}
}
}
break;
case 2:
for (i = 0; i < x - 1; i++)
{
for (j = 0; j < x - i - 1; j++, p++)
{
if ((p->zongcj) > ((p + 1)->zongcj))
{
stu = student[j] ;
student[j] = student[j+1];
student[j+1] = stu;
}
}
}
break;
case 3:
for (i = 0; i < x - 1; i++)
{
for (j = 0; j < x - i - 1; j++, p++)
{
if ((p->shuxue) > ((p + 1)->shuxue))
{
stu = student[j] ;
student[j] = student[j+1];
student[j+1] = stu;
}
}
}
break;
case 4:
for (i = 0; i < x - 1; i++)
{
for (j = 0; j < x - i - 1; j++, p++)
{
if ((p->yuwen) > ((p + 1)->yuwen))
{
stu = student[j] ;
student[j] = student[j+1];
student[j+1] = stu;
}
}
}
break;
case 5:
for (i = 0; i < x - 1; i++)
{
for (j = 0; j < x - i - 1; j++, p++)
{
if ((p->yingyu) > ((p + 1)->yingyu))
{
stu = student[j] ;
student[j] = student[j+1];
student[j+1] = stu;
}
}
}
break;
}
printf
("学号\t姓名\t数学\t语文\t英语\t总成绩\n");
for (k = 0; k < x; k++, p++)
{
printf("%ld\t", p->xuehao);
printf("%s\t", p->xingming);
printf("%5.1f\t", p->shuxue);
printf("%5.1f\t", p->yuwen);
printf("%5.1f\t", p->yingyu);
printf("%5.1f\n", p->zongcj);
}
printf("是否继续排序 y(yes)or n(no)");
scanf("%s", &h);
if (h == y)
goto loop;
else
printf("谢谢使用 \n");
}
void average()
{
int i, j, k, a = 0, b = 0, c = 0;
for (i = 0; i < x; i++, p++)
{
a = a + p->shuxue;
}
a = a / x;
printf("全班同学数学平均成绩 \n", a);
for (i = 0; i < x; i++, p++)
{
b = b + p->yuwen;
}
b = b / x;
printf("全班同学语文平均成绩 \n", a);
for (i = 0; i < x; i++, p++)
{
c = c + p->yingyu;
}
c = c / x;
printf("全班同学英语平均成绩 \n", a);
}
void inquirexh()
{
int i, j, k;
char e;
def:printf
("请输入要查询的同学的学号 \n");
scanf("%ld", &k);
for (i = 0; i < x; i++, p++)
{
if (p->xuehao == k)
printf
("学号%ld姓名%s数学%f语文%f英语%f总成绩%f",
p->xuehao, p->xingming, p->shuxue, p->yuwen, p->yingyu,
p->zongcj);
}
printf("是否继续查询?y(yes)or n(no)");
scanf("%s", &e);
if (e == y)
goto def;
else
printf("谢谢使用 \n");
}
这个基本符合楼主的要求,我以前写的。