#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "math.h"
#include "conio.h"
#define SIZE 3
int STUM;
int TEAM;
int i, j;
int sum[SIZE];
void studate();
void teadate();
void save();
void cxstu();
void cxtea();
void del();
void edit();
void sort();
struct student
{
int num;
char name[10];
int score[3];
int ave;
}stud[SIZE], change;
struct teacher
{
int number;
char name[10];
int gongz;
}teac[SIZE], tchang;
void main()
{
FILE *fp;
char choice;
if ((fp = fopen("g:\\st\\alltest.dat", "rb")) == NULL)
{
printf("文件打不开");
exit(0);
}
fread(&STUM, sizeof(STUM), 1, fp);
fread(&TEAM, sizeof(TEAM), 1, fp);
for (i = 0; i < STUM; i++)
fread(&stud[i], sizeof(struct student), 1, fp);
for (i = 0; i < TEAM; i++)
fread(&teac[i], sizeof(struct teacher), 1, fp);
printf("\n\n\n\n\n");
printf("\t\t 1.输入学生数据\n");
printf("\t\t 2.输入老师数据\n");
printf("\t\t 3.查询学生数据\n");
printf("\t\t 4.查询教师数据\n");
printf("\t\t 5.删除 数据\n");
printf("\t\t 6.修改 数据\n");
printf("\t\t 7.学生成绩名次\n");
printf("\t\t 8.退 出\n");
printf("\n\n\n请选择(1、2、3、4、5、6、7、8)");
choice = _getch();
switch (choice)
{
case '1': studate();
break;
case '2':teadate();
break;
case '3':cxstu();
break;
case '4':cxtea();
break;
case '5':del();
break;
case '6':edit();
break;
case '7':sort();
break;
case '8':break;
}
fclose(fp);
}
void studate()
{
char yn;
//输入学生数据
printf("\t输入学生数据\n");
printf("\n NAME No.SCORE1 SCORE2 SCORE3 AVE\n");
printf("====================================================\n");
i = STUM;
scanf("%s %d %d %d %d %d", stud[i].name, &stud[i].num, &stud[i].score[0], &stud[i].score[1], &stud[i].score[2], &stud[i].ave);
sum[i] = stud[i].score[0] + stud[i].score[1] + stud[i].score[2];
stud[i].ave = sum[i] / 3;
STUM++;
/*i++;
printf("是否继续输入:y/n");
yn=getcher();
if(yn=='y'||yn=='Y')
goto loop;
else
break;
}while(1);*/
/*for (i = 0; i < SIZE; i++)
{
scanf("%s %d %d %d %d %d", stud[i].name, &stud[i].num, &stud[i].score[0], &stud[i].score[1], &stud[i].score[2], &stud[i].ave);
sum[i] = stud[i].score[0] + stud[i].score[1] + stud[i].score[2];
stud[i].ave = sum[i] / 3;
STUM=STUM++;
} */
save();
}
void teadate()
{
//输入教师数据
printf("\t输入教师数据\n");
printf("\n NAME No gongz\n");
printf("====================================================\n");
i = TEAM;
scanf("%s %d %d", teac[i].name, &teac[i].number, &teac[i].gongz);
TEAM++;
save();
}
void cxstu()
{
printf("输出原有学生数据");
printf("\n NAME No.SCORE1 SCORE2 SCORE3 AVE\n");
printf("====================================================\n");
for (i = 0; i<STUM; i++)
{
printf("%-10s %10d %10d %10d %10d %10d\n", stud[i].name, stud[i].num, stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].ave);
}_getch();
}
void cxtea()
{
printf("\t输入教师数据\n");
printf("\n\t NAME No gongz\n");
printf("====================================================\n");
for (i = 0; i < TEAM; i++)
{
printf("%-10s %10d %10d \n", teac[i].name, teac[i].number, teac[i].gongz);
}_getch();
}
void del()
{
int n = STUM;//为文件中有多少个学生的数据
int m = TEAM;
FILE *fp;
int i, f, t;
char dname[10];
printf("\t请输入删除人名:");
gets(dname);
if ((fp = fopen("g:\\st\\alltest.dat", "wb")) == NULL)
{
printf("the file can not open \n");
return;
}
for (t = 0; strcmp(stud[t].name, dname) != 0 && t < n; t++);
for (f = 0; strcmp(teac[f].name, dname) != 0 && f < m; f++);
if (t<n || f<m)
//删除记录
{
if (t<n)
{
STUM--;
fwrite(&STUM, sizeof(STUM), 1, fp);
fwrite(&TEAM, sizeof(TEAM), 1, fp);
for (i = 0; i < t; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
for (i = t + 1; i < n; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
for (i = 0; i < TEAM; i++)
fwrite(&teac[i], sizeof(struct teacher), 1, fp);
}
if (f<m)
{
TEAM--;
fwrite(&STUM, sizeof(STUM), 1, fp);
fwrite(&TEAM, sizeof(TEAM), 1, fp);
for (i = 0; i < STUM; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
for (i = 0; i < f; i++)
fwrite(&teac[i], sizeof(struct teacher), 1, fp);
for (i = f + 1; i < m; i++)
fwrite(&teac[i], sizeof(struct teacher), 1, fp);
}
}
else
//没有该记录
{
fwrite(&STUM, sizeof(STUM), 1, fp);
fwrite(&TEAM, sizeof(TEAM), 1, fp);
for (i = 0; i < STUM; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
for (i = 0; i < TEAM; i++)
fwrite(&teac[i], sizeof(struct teacher), 1, fp);
}
fclose(fp);
}
void edit()
{
int n = STUM;//为文件中有多少个学生的数据
int m = TEAM;
FILE *fp;
int i, f, t;
char dname[10];
printf("\t请输入修改人名:");
gets(dname);
if ((fp = fopen("g:\\st\\alltest.dat", "wb")) == NULL)
{
printf("the file can not open \n");
return;
}
for (t = 0; strcmp(stud[t].name, dname) != 0 && t<n; t++);
for (f = 0; strcmp(teac[f].name, dname) != 0 && f<m; f++);
if (t<n || f<m)
//修改记录
{
if (t<n)
{
printf("输出原有学生数据");
printf("\n NAME No.SCORE1 SCORE2 SCORE3 AVE\n");
printf("====================================================\n");
printf("%-10s %10d %10d %10d %10d %10d\n", stud[t].name, stud[t].num, stud[t].score[0], stud[t].score[1], stud[t].score[2], stud[t].ave);
_getch();
printf("\n NAME No.SCORE1 SCORE2 SCORE3 AVE\n");
printf("====================================================\n");
scanf("%s %d %d %d %d %d", stud[t].name, &stud[t].num, &stud[t].score[0], &stud[t].score[1], &stud[t].score[2], &stud[t].ave);
sum[t] = stud[t].score[0] + stud[t].score[1] + stud[t].score[2];
stud[t].ave = sum[t] / 3;
fwrite(&STUM, sizeof(STUM), 1, fp);
fwrite(&TEAM, sizeof(TEAM), 1, fp);
for (i = 0; i <STUM; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
for (i = 0; i < TEAM; i++)
fwrite(&teac[i], sizeof(struct teacher), 1, fp);
}
if (f<m)
{
printf("====================================================\n");
printf("%-10s %10d %10d \n", teac[f].name, teac[f].number, teac[f].gongz);
_getch();
printf("\n NAME No gongz\n");
printf("====================================================\n");
scanf("%s %d %d", teac[f].name, &teac[f].number, &teac[f].gongz);
fwrite(&STUM, sizeof(STUM), 1, fp);
fwrite(&TEAM, sizeof(TEAM), 1, fp);
for (i = 0; i <STUM; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
for (i = 0; i < TEAM; i++)
fwrite(&teac[i], sizeof(struct teacher), 1, fp);
}
}
else
//没有该记录
{
fwrite(&STUM, sizeof(STUM), 1, fp);
fwrite(&TEAM, sizeof(TEAM), 1, fp);
for (i = 0; i < STUM; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
for (i = 0; i < TEAM; i++)
fwrite(&teac[i], sizeof(struct teacher), 1, fp);
}
fclose(fp);
}
void sort()
{
int n = STUM;
int i, j;
FILE *fp, *fp1;
if ((fp = fopen("g:\\st\\stud_sort.dat", "wb")) == NULL)
{
printf("the file can not open \n");
return;
}
/*排序*/
for (i = 0; i<n - 1; i++)
for (j = i + 1; j<n; j++)
if (stud[i].ave<stud[j].ave)
{
change = stud[i]; stud[i] = stud[j]; stud[j] = change;
}
fwrite(&STUM, sizeof(STUM), 1, fp);
for (i = 0; i < STUM; i++)
fwrite(&stud[i], sizeof(struct student), 1, fp);
fclose(fp);
if ((fp1 = fopen("g:\\st\\stud_sort.dat", "rb")) == NULL)
{
printf("the file can not open \n");
return;
}
fread(&STUM, sizeof(STUM), 1, fp1);
for (i = 0; i < STUM; i++)
fread(&stud[i], sizeof(stru