送給你們看一下
#include
#include
#include
struct stud {
int stdId;
char name[20];
int mathScore;
int engScore;
};
FILE *fp;
int read_data_from_file(FILE *fp, struct stud students[] ){
int i = 0;
while( 1 ){
if( fread(&students[ i ], sizeof(struct stud), 1, fp) < 1)break;
printf("%dt", students[i].stdId);
printf("%st", students[i].name);
printf("%dt", students[i].mathScore);
printf("%dt", students[i].engScore);
printf("n");
i++;
}
return i;
}
void listing(struct stud students[], int num){
int i;
printf("nn學號t姓名t數學t英文n");
printf("----t----t-----t----tn");
for(i = 0; i < num; i++){
printf("%dt", students[i].stdId);
printf("%st", students[i].name);
printf("%dt", students[i].mathScore);
printf("%dt", students[i].engScore);
printf("n");
}
}
int add_data(struct stud students[], int num){
int sid, math, eng;
char name[20];
printf("n請輸入學號(1-100): ");
scanf("%d", &sid);
while(1){
if(getchar() == ’n’) break;
}
printf("請輸入學生姓名: ");
gets(name);
printf("請輸入數學成績: ");
scanf("%d", &math);
printf("請輸入英文成績: ");
scanf("%d", &eng);
students[num].stdId = sid;
strcpy(students[num].name, name);
students[num].mathScore = math;
students[num].engScore = eng;
num++;
return num;
}
void process_data(struct stud students[], int num){
int i, mathmax, mathmin, engmax, engmin;
printf("nn學號t姓名t數學t英文t總分t平均n");
printf("----t----t-----t----t----t----n");
for(i = 0; i < num; i++){
int sum = 0;
printf("%dt", students[i].stdId);
printf("%st", students[i].name);
printf("%dt", students[i].mathScore);
printf("%dt", students[i].engScore);
sum = students[i].mathScore + students[i].engScore;
printf("%dt%5.2f", sum, sum / 2.0);
printf("n");
}
printf("----t----t-----t----t----t----n");
printf("最低分tt");
mathmin = engmin = 100;
for(i = 0; i < num; i++){
if(students[i].mathScore < mathmin)
mathmin = students[i].mathScore;
if(students[i].engScore < engmin)
engmin = students[i].engScore;
}
printf("%dt%dt", mathmin, engmin);
printf("n最高分tt");
mathmax = engmax = 0;
for(i = 0; i < num; i++){
if(students[i].mathScore > mathmax)
mathmax = students[i].mathScore;
if(students[i].engScore > engmax)
engmax = students[i].engScore;
}
printf("%dt%dt", mathmax, engmax);
printf("nn");
}
void save_file(char file[], struct stud students[], int num){
int i;
fp = fopen(file, "wb");
printf("output file name is %s...n", file);
printf("current num = %dn", num);
for(i = 0; i < num; i++){
fwrite(&students[i], sizeof(struct stud), 1, fp);
}
fclose(fp);
}
void main(void){
struct stud students[100];
int num = 0;
char filename[80], choice;
while( 1 ){
printf("n (1) listing (2) add data (3) data processing (4) save file > ");
scanf("%d", &choice);
switch( choice ){
case 1: listing(students, num); break;
case 2: num = add_data(students, num); break;
case 3: process_data(students, num); break;
case 4: exit(0);
default: printf("ninvalid choice ...n");
}
}
system("pause");
exit(0);
}
好看嗎?
我眼睛都快瞎了
文章定位: