该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
师父给的是这个
#include
#include
#define MAX_COUNT 5
#define MAX_NAME_LEN 20
#define BOOL int
#define TRUE 1
#define FALSE 0
#define CMD_ADD 'a'
#define CMD_QUERY 'f'
#define CMD_SORT 's'
#define CMD_QUIT 'q'
#define DATA_FILE "stds.dat"
struct StudentScore
{
int chineseScore;
int mathScore;
int englishScore;
};
struct StudentInfo
{
char name[MAX_NAME_LEN];
struct StudentScore scores;
};
int getScore(char * hint);
void getAStd(struct StudentInfo * std);
void getStds(struct StudentInfo stds[], int arrayCount);
void sortTotalScore(struct StudentInfo stds[], int arrayCount);
int getTotal(struct StudentScore * score);
BOOL fileExists(char * file);
void saveStds();
void loadStds();
void usage();
void add();
void find();
void displayStd(struct StudentInfo* std);
int gCount = 0;
struct StudentInfo gStds[MAX_COUNT];
void main()
{
char cmd;
int i, num;
num = MAX_COUNT;
loadStds();
while(1)
{
fflush(stdin);
printf("please input cmd:");
cmd = getchar();
switch(cmd)
{
case CMD_ADD:
add();
break;
case CMD_QUERY:
find();
break;
case CMD_SORT:
sortTotalScore(gStds, MAX_COUNT);
for (i = 0; i < gCount; i++)
printf("name %s : totoal %d \n", gStds[i].name, getTotal(&(gStds[i].scores)));
printf("\n");
break;
case CMD_QUIT:
return;
default:
usage();
break;
}
printf("\n");
}
}
void usage()
{
printf("find is %c\n", CMD_QUERY);
printf("quit is %c\n", CMD_QUIT);
printf("add is %c\n", CMD_ADD);
printf("sort is %c\n", CMD_SORT);
}
void saveStds()
{
int i;
FILE * fp;
fp = fopen(DATA_FILE, "wb");
if (fp == NULL)
return;
for(i = 0; i < gCount; i++)
fwrite(&gStds[i], sizeof(struct StudentInfo), 1, fp);
fclose(fp);
}
BOOL fileExists(char * file)
{
FILE * fp;
fp = fopen(DATA_FILE, "rb");
if (fp == NULL)
return FALSE;
return TRUE;
}
void loadStds()
{
FILE * fp;
int isEnd;
struct StudentInfo temp;
if (MAX_COUNT == 0)
{
printf("no space\n");
}
if (!fileExists(DATA_FILE))
return;
gCount = 0;
fp = fopen(DATA_FILE, "rb");
if (fp == NULL)
return;
isEnd = 0;
while(1)
{
if (fread(&gStds[gCount], sizeof(struct StudentInfo), 1, fp) == 1)
gCount++;
else
isEnd = 1;
if (isEnd)
{
break;
}
else
{
if (gCount == MAX_COUNT)
{
if (fread(&temp, sizeof(struct StudentInfo), 1, fp) == 1)
printf("no space\n");
break;
}
}
}
fclose(fp);
}
void add()
{
if (gCount == MAX_COUNT)
{
printf("over limit\n");
return;
}
gCount++;
getAStd(&gStds[gCount - 1]);
saveStds();