回复 23楼 aKARL
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
#define CMD_START printf ("
<<<======================= start =========================>>>\n");
#define CMD_END
printf ("
<<<======================= end =========================>>>\n");
#define
DATA_FILE "data.dat"
#define TEMP_FILE
"temp.dat"
typedef struct tagStudent
{
char ID[30];
char Name[30];
char Class[255];
char Sex;
int Math;
int English;
int Compute;
int Philosophy;
int
PE;
struct tagStudent *next;
} Student;
int ShowMenu();
int WriteData(FILE* , Student* );
void Output_Rec(Student*);
void Input_Rec(Student*);
void CopyRec(Student* , Student* );
void Print();
void Add();
void Delete();
void Statistics();
void Find(int);
int quit;
main()
{
int cmd;
quit = 0;
while( ! quit )
{
cmd = ShowMenu();
CMD_START
switch( cmd )
{
case 1:
Print();getch();break;
case 2:
Add(); getch();break;
case 3:
Delete();getch();break;
case 4:
Statistics();getch();break;
case 5:
Find(5);getch();break;
case 6:
Find(6);getch();break;
case 7:
quit = 1;
getch();break;
default:
printf(" input digit \n");
printf ("
<<<======================== end ==========================>>>\n");
getch();break;
}
CMD_END
system("cls");
if (quit != 1)
{
printf(" Menu(click) ....\n");
getch();
}
}
}
int ShowMenu()
{
int cmd = 0;
char Menu_SeeData[]
= "\t1 .\tView the Records in the data file\n";
char Menu_Add[]
= "\t2 .\tAdd New Record\n";
char Menu_Delete[]
= "\t3 .\tDelete an old Record\n";
char Menu_Statistics[]
= "\t4 .\tMake a Statistics\n";
char Menu_Find_ID[]
= "\t5 .\tFind a Record from the ID\n";
char Menu_Find_Name[] = "\t6 .\tFind a Record from the Name\n";
char Menu_Quit[]
= "\t7 .\tQuit\n";
system("COLOR e");
system("cls");
printf ("\n
*
start
* \n");
printf ("\n
*
*\n");
printf ("
<<<============================= * ==================================>>>\n");
printf ("
>\t\t\t
1.Student information
\t\t\t<\n");
printf ("
>\t\t\t
2.Write to student information
\t\t\t<\n");
printf ("
>\t\t\t
3.Delete student information
\t\t\t<\n");
printf ("
>\t\t\t
4.Student achievement
\t\t\t<\n");
printf ("
>\t\t\t
5.Student id
\t\t\t<\n");
printf ("
>\t\t\t
6.Student name
\t\t\t<\n");
printf ("
>\t\t\t
7.Break
\t\t\t<\n\n");
printf ("
<<----------------------------- * ---------------------------------->>\n");
printf("\n\n\tinput digit:");
scanf("%d" , &cmd);
printf("\n");
system("cls");
return cmd;
}
void Print()
{
FILE* fp = NULL;
Student rec;
int i = 0;
fp = fopen(DATA_FILE , "rb");
if(fp == NULL)
{
printf(" open error %s\n" , DATA_FILE);
getch();
return ;
}
while(ReadData(fp , &rec))
{
Output_Rec(&rec);
printf(" ---------------------------------------------------------------\n");
i++;
if(i>1 && i % 3 == 0)
{
printf("\n end(click) ... \n");
getch();
}
}
printf("\nHave\t%d\tInformation .\n" , i );
fclose(fp);
system("pause");
}
void Add()
{
Student rec;
FILE* fp = NULL;
char a;
Input_Rec( &rec );
fp = fopen(DATA_FILE ,"ab");
if( fp == NULL)
{
printf(" Open Error \n");
return ;
}
if( WriteData(fp, &rec) == 1)
printf("\n\nOK\n\n");
else
printf("\n\n Open Error \n");
printf("GO ON?:y/n\n");
scanf("%c",&a);
scanf("%c",&a);
if(a=='N' || a=='n')
{
fclose(fp);
system("cls");
ShowMenu();
}
else
Add();
}
void Delete()
{
Student rec;
FILE* fpr,*fpw;
char buf[30];
char cmd[255];
char num1;
int del_count;
del_count = 0;
printf("\n Delete Student ID: .");
printf("\n ID : ");
scanf("%s" , buf);
fpr = fopen(DATA_FILE ,"rb");
while(ReadData(fpr ,&rec))
{
if(strcmp(rec.ID , buf) == 0)
{
del_count++;
Output_Rec(&rec);
printf(" ---------------------------------------------------------------\n");
}
}
printf("Are you sure?Y/N\n");
scanf("%c",&num1);
scanf("%c",&num1);
if(num1=='N' || num1=='n')
{
system("cls");
ShowMenu();
}
if( fpr == NULL)
{
printf(" Open Error ... \n");
return ;
}
fpw = fopen(TEMP_FILE,"wb");
if( fpw == NULL)
{
printf(" Open Error \n");
return ;
}
while(ReadData(fpr , &rec))
{
if(strcmp(rec.ID , buf) != 0)
{
WriteData(fpw, &rec);
}
else
{
del_count++;
}
}
fclose(fpr);
fclose(fpw);
strcpy(cmd , "del ");
strcat(cmd ,DATA_FILE);
system(cmd);
rename(TEMP_FILE,DATA_FILE);
printf("\n\t%dInformation have delete \n",del_count);
system("pause");
}
void Statistics()
{
int i50,i60,i70,i80,i90;
float avg;
int sum ,sum_high,sum_low;
Student stu_high,stu_low;
Student stu_math_high,stu_english_high;
Student stu_compute_high,stu_philosophy_high,stu_PE_high;
Student stu_math_low,stu_english_low;
Student stu_compute_low, stu_philosophy_low,stu_PE_low;
FILE* fp;
Student rec;
int count;
count = sum = sum_high = sum_low = i50 = i60 = i60 = i70 =i80 = i90 = 0;
fp = NULL;
fp = fopen(DATA_FILE ,"rb");
if(fp == NULL)
{
printf("\nRead Error \n");
return;
}
while(ReadData(fp , &rec))
{
count++;
sum = rec.Math + rec.English + + rec.PE+ rec.Philosophy;
avg = ((float)sum) / 5;
if(avg < 60)
i50++;
else if(avg <70)
i60++;
else if(avg < 80)
i70++;
else if(avg < 90)
i80++;
else
i90++;
if(count <= 1)
{
sum_high = sum_low = sum;
CopyRec(&stu_high , &rec);
CopyRec(&stu_low ,&rec);
}
else
{
if(sum > sum_high)
{
sum_high = sum;
CopyRec(&stu_high , &rec);
}
if(sum < sum_low)
{
sum_low = sum;
CopyRec(&stu_low , &rec);
}
}
if(count == 1)
{
CopyRec(&stu_math_high,&rec);
CopyRec(&stu_english_high , &rec);
CopyRec(&stu_compute_high, &rec);
CopyRec(&stu_philosophy_high,&rec);
CopyRec(&stu_PE_high , &rec);
CopyRec(&stu_math_low,&rec);
CopyRec(&stu_english_low , &rec);
CopyRec(&stu_compute_low, &rec);
CopyRec(&stu_philosophy_low,&rec);
CopyRec(&stu_PE_low , &rec);
}
else
{
if(rec.Math > stu_math_high.Math)
CopyRec(&stu_math_high ,&rec);
if(rec.English > stu_english_high.English)
CopyRec(&stu_english_high ,&rec);
if( > stu_compute_)
CopyRec(&stu_compute_high , &rec);
if(rec.Philosophy > stu_philosophy_high.Philosophy)
CopyRec(&stu_philosophy_high , &rec);
if(rec.PE > stu_PE_high.PE)
CopyRec(&stu_PE_high , &rec);
if(rec.Math < stu_math_low.Math)
CopyRec(&stu_math_low ,&rec);
if(rec.English < stu_english_low.English)
CopyRec(&stu_english_low ,&rec);
if( < stu_compute_)
CopyRec(&stu_compute_low , &rec);
if(rec.Philosophy < stu_philosophy_low.Philosophy)
CopyRec(&stu_philosophy_low , &rec);
if(rec.PE < stu_PE_low.PE)
CopyRec(&stu_PE_low , &rec);
}
}
if(count < 1)
{
printf("\n NO RECORD \n");
}
else
{
printf("\n section distribute:\n");
printf("\t
<
60\t:\t%d\n",i50);
printf("\t60
-
69\t:\t%d\n",i60);
printf("\t70
-
79\t:\t%d\n",i70);
printf("\t80
-
90\t:\t%d\n",i80);
printf("\t
>=
90\t:\t%d\n",i90);
printf(" ---------------------------------------------------------------\n");
system("pause");
printf("\n High Student\n");
printf(" Score: %d\n" , sum_high);
printf(" Student :\n");
Output_Rec(&stu_high);
printf("\n Low Student\n");
printf(" Score : %d\n" , sum_low);
printf(" Student :\n");
Output_Rec(&stu_low);
printf(" ---------------------------------------------------------------\n");
printf(" Math High:\n");
Output_Rec(&stu_math_high);
printf(" Math Low :\n");
Output_Rec(&stu_math_low);
printf(" ---------------------------------------------------------------\n");
printf(" English High :\n");
Output_Rec(&stu_english_high);
printf(" English Low :\n");
Output_Rec(&stu_english_low);
printf(" ---------------------------------------------------------------\n");
printf(" Compute High :\n");
Output_Rec(&stu_compute_high);
printf(" Compute Low :\n");
Output_Rec(&stu_compute_low);
printf(" ---------------------------------------------------------------\n");
printf(" Philosophy High :\n");
Output_Rec(&stu_philosophy_high);
printf(" Philosophy Low :\n");
Output_Rec(&stu_philosophy_low);
printf(" ---------------------------------------------------------------\n");
printf(" Pe High :\n");
Output_Rec(&stu_PE_high);
printf(" Pe Low :\n");
Output_Rec(&stu_PE_low);
printf(" ---------------------------------------------------------------\n");
system("pause");
}
fclose(fp);
}
void Find(int isFind_From_ID)
{
char buf[30];
Student rec;
int find_count;
FILE* fp;
find_count = 0;
fp = fopen(DATA_FILE ,"rb");
if( fp == NULL)
{
printf("\n open error ...\n");
return;
}
switch(isFind_From_ID)
{
case 5:
printf("\n writr student id: ");
scanf("%s",buf);
while(ReadData(fp ,&rec))
{
if(strcmp(rec.ID , buf) == 0)
{
find_count++;
Output_Rec(&rec);
printf(" ------------------------------------------------------\n");
}
}
getch();
break;
case 6:
printf("\n writr student id : ");
scanf("%s",buf);
while(ReadData(fp , &rec))
{
if(strcmp(rec.Name, buf) >=0)
{
find_count++;
Output_Rec(&rec);
printf(" -----------------------------------------------------\n");
}
}
getch();
break;
default:
printf(" \n pleace ...\n");
}
if(find_count >0)
{
printf("\n have\t%d\t information\n",find_count);
}
else
{
printf("\n sorry\n NO have \n");
printf("\n Suggest to use keywords \n");
}
fclose(fp);
}
int ReadData(FILE* fp, Student* Dest_Rec)
{
int r;
if(( fp == NULL ) || ( Dest_Rec == NULL))
return 0;
r = fread(Dest_Rec ,sizeof(Student) ,1 ,fp);
if(r != 1)
return 0;
return 1;
}
int WriteData(FILE* fp, Student* Src_Rec)
{
int r;
if((fp == NULL) || (Src_Rec == NULL))
return 0;
r = fwrite(Src_Rec , sizeof(Student) , 1, fp);
if(r != 1)
return 0;
return 1;
}
void Output_Rec( Student* stu)
{
printf("\n");
printf("
Name : %s", stu->Name);
printf("\n
Sex : ");
if( stu->Sex == 'M' || stu->Sex == 'm' )
printf("Male");
else
printf("Female");
printf("
ID : %s\t\tClass : %s\n",stu->ID,stu->Class);
printf("
Math = %d\t\tEnglish = %d\t\tCompute =%d\n" ,stu->Math ,stu->English, stu->Compute);
printf("
Philosophy = %d\t\tPE = %d\n", stu->Philosophy ,stu->PE);
printf("\n");
}
void Input_Rec( Student* stu)
{
if(stu == NULL)
return;
printf("\n Name = ");
scanf("%s",stu->Name);
printf("\tSex = (F|M) ");
while(1)
{
stu->Sex = getch();
if(stu->Sex == 'F' || stu->Sex == 'f' || stu->Sex == 'M' || stu->Sex == 'm')
{
printf("%c",stu->Sex);
break;
}
}
printf("\n ID = ");
scanf("%s" , stu->ID);
printf("\n Class = ");
scanf("%s" , stu->Class);
printf("\n Math = ");
scanf("%d", &(stu->Math));
printf("\n English = ");
scanf("%d" ,&(stu->English));
printf("\n Compute = ");
scanf("%d" , &(stu->Compute));
printf("\n Philosophy = ");
scanf("%d" , &(stu->Philosophy));
printf("\n PE = ");
scanf("%d" , &(stu->PE));
printf("\n");
}
void CopyRec(Student* dest_stu, Student* src_stu)
{
strcpy(dest_stu->Name ,src_stu->Name);
strcpy(dest_stu->ID ,src_stu->ID);
strcpy(dest_stu->Class ,src_stu->Class);
dest_stu->Sex = src_stu->Sex;
dest_stu->Math = src_stu->Math;
dest_stu->English = src_stu->English;
dest_stu->Compute = src_stu->Compute;
dest_stu->Philosophy = src_stu->Philosophy;
dest_stu->PE = src_stu->PE;
}