5.編程序,對上一題中名為“CCW.TXT”的磁盤文件中“@”之前的所有字符解密并顯示。
「解」要編寫的程序完成與上一程序功能相反的工作,將上述程序中的DALTA定義成一10即可。完整程序略。
6.輸入100個學生的信息(含學號、姓名、年齡、七門課程成績、總分),其中學生的總分由程序計算產(chǎn)生。先將學生信息存入磁盤二進制數(shù)據(jù)文件student.dat中。然后再讀取該文件,尋找總分高的學生并輸出該生的所有信息。
「解」程序要定義一個結(jié)構(gòu),逐一輸入學生信息,并完成學生的總分計算,將學生信息存到該結(jié)構(gòu)中,然后將該結(jié)構(gòu)信息存入文件。設(shè)學生信息的輸入以學生的學號為負數(shù)結(jié)束。然后,程序再用循環(huán)從這文件讀入學生信息,將其中總分更高的學生信息存入另一結(jié)構(gòu)中。循環(huán)結(jié)束后,結(jié)構(gòu)中存儲的就是總分高學生的信息。后,顯示這總分高的學生信息。程序如下:
# include
# include
# include
struct stu {
long no;
char name[20];
int age;
int score[7];
int total;
} student, maxs;
FILE *fp;
void main()
{ char buf[120];int total,i,t;long lt;
if((fp= fopen(“student.dat”,“wb”))== NULL) {
printf(“Can‘t open file %s.\n”,“student.dat” );exit(0);
}
printf(“輸入學生的學號、姓名、年齡、七門課程成績(學號小于0結(jié)束) \n”);
while(1) {
scanf(“%ld”, <); student.no = lt;
if(student.no< 0) break;
scanf(“%s”, buf); strncpy( student.name, buf,19);
scanf(“%d”, &t); student.age = t;
for( total = 0, i = 0; i < 7;i++) {
scanf(“%d”,&t); student.score= t;
total+= t;
}
student.total= total;
fwrite(char *) & student,sizeof student, 1, fp);
}
fclose(fp);
if((fp = fopen(“student.dat”,“rb”))== NULL) {
printf(“Can‘t open file %s.\n”,“student.dat”);exit(0);
}
maxs.total=-100;
While( !feof(fp)) {
fread((char *) & student,sizeof student,l,fp);
if( student.total> maxs.total) maxs=student;
}
fclose(fp);
printf(“總分高的學生信息:\n”);
printf(“學號:%ld”, maxs.no);
printf(“ \t姓名:%s”,maxs,name);
printf(“\t年齡::%d\n成績:”, maxs.age)
for( i=0; i< 7; i++)
printf(“m”,maxs.score);
printf(“\nTOTAL = %d\n”, maxs. total);
}
「解」要編寫的程序完成與上一程序功能相反的工作,將上述程序中的DALTA定義成一10即可。完整程序略。
6.輸入100個學生的信息(含學號、姓名、年齡、七門課程成績、總分),其中學生的總分由程序計算產(chǎn)生。先將學生信息存入磁盤二進制數(shù)據(jù)文件student.dat中。然后再讀取該文件,尋找總分高的學生并輸出該生的所有信息。
「解」程序要定義一個結(jié)構(gòu),逐一輸入學生信息,并完成學生的總分計算,將學生信息存到該結(jié)構(gòu)中,然后將該結(jié)構(gòu)信息存入文件。設(shè)學生信息的輸入以學生的學號為負數(shù)結(jié)束。然后,程序再用循環(huán)從這文件讀入學生信息,將其中總分更高的學生信息存入另一結(jié)構(gòu)中。循環(huán)結(jié)束后,結(jié)構(gòu)中存儲的就是總分高學生的信息。后,顯示這總分高的學生信息。程序如下:
# include
# include
# include
struct stu {
long no;
char name[20];
int age;
int score[7];
int total;
} student, maxs;
FILE *fp;
void main()
{ char buf[120];int total,i,t;long lt;
if((fp= fopen(“student.dat”,“wb”))== NULL) {
printf(“Can‘t open file %s.\n”,“student.dat” );exit(0);
}
printf(“輸入學生的學號、姓名、年齡、七門課程成績(學號小于0結(jié)束) \n”);
while(1) {
scanf(“%ld”, <); student.no = lt;
if(student.no< 0) break;
scanf(“%s”, buf); strncpy( student.name, buf,19);
scanf(“%d”, &t); student.age = t;
for( total = 0, i = 0; i < 7;i++) {
scanf(“%d”,&t); student.score= t;
total+= t;
}
student.total= total;
fwrite(char *) & student,sizeof student, 1, fp);
}
fclose(fp);
if((fp = fopen(“student.dat”,“rb”))== NULL) {
printf(“Can‘t open file %s.\n”,“student.dat”);exit(0);
}
maxs.total=-100;
While( !feof(fp)) {
fread((char *) & student,sizeof student,l,fp);
if( student.total> maxs.total) maxs=student;
}
fclose(fp);
printf(“總分高的學生信息:\n”);
printf(“學號:%ld”, maxs.no);
printf(“ \t姓名:%s”,maxs,name);
printf(“\t年齡::%d\n成績:”, maxs.age)
for( i=0; i< 7; i++)
printf(“m”,maxs.score);
printf(“\nTOTAL = %d\n”, maxs. total);
}