(例如,’A’(90~100), ‘B’(80~89),’C’(70~79),’D’(60~69),’F’(60以下))后, 写入一个新创建的文件
CS1GRAGE.TXT.
数据文件CS1STU.TXT
0090 90 90 90
0089 88 90 89
0081 80 82 81
0079 79 79 79
0070 70 70 70
0069 69 69 69
0060 60 60 60
0059 59 59 59
结果文件:
Output----
0090 90 A \n
0089 89 B \n
0081 81 B \n
0079 79 C \n
0070 70 C \n
0069 69 D \n
0060 60 D \n
0059 59 F \n
----------------解决方案--------------------------------------------------------
/* 已知数据文件CS1STU.TXT, 其中第一列为学生的学号,其他各列为成绩, 求平均成绩和学分 (例如,’A’(90~100), ‘B’(80~89),’C’(70~79),’D’(60~69),’F’(60以下))后, 写入一个新创建的文件 CS1GRAGE.TXT.
数据文件CS1STU.TXT 0090 90 90 90 0089 88 90 89 0081 80 82 81 0079 79 79 79 0070 70 70 70 0069 69 69 69 0060 60 60 60 0059 59 59 59
结果文件: Output---- 0090 90 A \n 0089 89 B \n 0081 81 B \n 0079 79 C \n 0070 70 C \n 0069 69 D \n 0060 60 D \n 0059 59 F \n
*/
#include <stdio.h> #include <stdlib.h>
void operatefile();
void operatefile() { FILE *fpr; FILE *fpw; char stunum[5]; int grade1; int grade2; int grade3; int avegrade; char rank; if((fpr=fopen("CS1STU.TXT","r"))==NULL) { printf("can not open this file .\n"); exit(1); } if((fpw=fopen("CS1GRAGE.TXT","w"))==NULL) { printf("can not open this file .\n"); exit(1); } while(!feof(fpr)) { fscanf(fpr,"%s %d %d %d",stunum,&grade1,&grade2,&grade3); avegrade=(grade1+grade2+grade3)/3; if(avegrade>=90&&avegrade<=100) rank='A'; else if(avegrade>=80&&avegrade<=89) rank='B'; else if(avegrade>=70&&avegrade<=79) rank='C'; else if(avegrade>=60&&avegrade<=69) rank='D'; else rank='F'; fprintf(fpw,"%s %d %c\n",stunum,avegrade,rank); }
fclose(fpr); fclose(fpw); }
void main() { operatefile(); }
----------------解决方案--------------------------------------------------------
//看看下面的 //两个输出有点不同 //这个和你要求吻合 /* 已知数据文件CS1STU.TXT, 其中第一列为学生的学号,其他各列为成绩, 求平均成绩和学分 (例如,’A’(90~100), ‘B’(80~89),’C’(70~79),’D’(60~69),’F’(60以下))后, 写入一个新创建的文件 CS1GRAGE.TXT.
数据文件CS1STU.TXT 0090 90 90 90 0089 88 90 89 0081 80 82 81 0079 79 79 79 0070 70 70 70 0069 69 69 69 0060 60 60 60 0059 59 59 59
结果文件: Output---- 0090 90 A \n 0089 89 B \n 0081 81 B \n 0079 79 C \n 0070 70 C \n 0069 69 D \n 0060 60 D \n 0059 59 F \n
*/
#include <stdio.h> #include <stdlib.h>
void operatefile();
void operatefile() { FILE *fpr; FILE *fpw; char stunum[5]; int grade1; int grade2; int grade3; int avegrade; char rank; if((fpr=fopen("CS1STU.TXT","r"))==NULL) { printf("can not open this file .\n"); exit(1); } if((fpw=fopen("CS1GRAGE.TXT","w"))==NULL) { printf("can not open this file .\n"); exit(1); } while(!feof(fpr)) { fscanf(fpr,"%s %d %d %d",stunum,&grade1,&grade2,&grade3); avegrade=(grade1+grade2+grade3)/3; if(avegrade>=90&&avegrade<=100) rank='A'; else if(avegrade>=80&&avegrade<=89) rank='B'; else if(avegrade>=70&&avegrade<=79) rank='C'; else if(avegrade>=60&&avegrade<=69) rank='D'; else rank='F'; fprintf(fpw,"%s %d %c %c%c\n",stunum,avegrade,rank,'\\','n'); }
fclose(fpr); fclose(fpw); }
void main() { operatefile(); }
----------------解决方案--------------------------------------------------------
Thank you!!
----------------解决方案--------------------------------------------------------