请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。
注意:源程序存放在考生文件夹下BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, long sno)
{FILE *fp;
STU n; int i;
fp = fopen(filename,"rb+");
/**********found**********/
while (!feof(__1__))
{fread(&n, sizeof(STU), 1, fp);
/**********found**********/
if (n.sno__2__sno) break;
}
if (!feof(fp))
{for (i=0; i<3; i++) n.score[i] += 3;
/**********found**********/
fseek(__3__, -(long)sizeof(STU), SEEK_CUR);
fwrite(&n, sizeof(STU), 1, fp);
}
fclose(fp);
}
main()
{STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},{10005,"ZhangSan", 95, 80, 88}}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j {printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name); for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]); printf("\n"); } fun("student.dat", 10003); fp = fopen("student.dat", "rb"); fread(ss, sizeof(STU), N, fp); fclose(fp); printf("\nThe data after modifing :\n"); for (j=0; j {printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name); for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]); printf("\n"); } } 解题答案: /**********第一空**********/ while (!feof(fp)) /**********第二空**********/ if (n.sno==sno) break; /**********第三空*********/ fseek(fp, -(long)sizeof(STU), SEEK_CUR); ****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本算法是:先对字符串中的头两个元素进行排序。然后把第三个字符插入到前两个字符中,插入后前三个字符依然有序;再把第四个字符插入到前三个字符中,……。待排序的字符串已在主函数中赋予。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 给定源程序: #include #include #define N 80 void insert(char *aa) {int i,j,n; char ch; /**********found**********/ n=strlen[aa]; for(i=1; i /**********found**********/ c=aa[i]; j=i-1; while ((j>=0) && (ch {aa[j+1]=aa[j]; j--; } aa[j+1]=ch; } } main() {char a[N]="QWERTYUIOPASDFGHJKLMNBVCXZ"; printf ("The original string : %s\n", a); insert(a); printf("The string after sorting : %s\n\n",a); } 解题答案: /**********found**********/ n=strlen(aa); /**********found**********/ ch=aa[i]; ****************************************** [NextPage] 三、程序题:N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:找出学生的最高分,由函数值返回。 注意: 部分源程序在文件PROG1.C文件中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 给定源程序: #include #include #define N 8 struct slist {double s; struct slist *next; }; typedef struct slist STREC; double fun(STREC *h) { } STREC * creat(double *s) {STREC *h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC));p->s=0; while(i {q=(STREC*)malloc(sizeof(STREC)); q->s=s[i]; i++; p->next=q; p=q; } p->next=0; return h; } outlist(STREC *h) {STREC *p; p=h->next; printf("head"); do {printf("->%2.0f",p->s);p=p->next;} while(p!=0); printf("\n\n"); } main() {double s[N]={85,76,69,85,91,72,64,87}, max;void NONO (); STREC *h; h=creat(s); outlist(h); max=fun(h); printf("max=%6.1f\n",max); NONO(); } void NONO () {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *in, *out ; int i,j ; double s[N],max; STREC *h ; in = fopen("in.dat","r"); out = fopen("out.dat","w"); for(i = 0 ; i < 10 ; i++) { for(j=0 ; j < N; j++) fscanf(in, "%lf,", &s[j]); h=creat(s); max=fun(h); fprintf(out, "%6.1lf\n", max); } fclose(in); fclose(out); } 参考答案: double fun( STREC *h ) { double max=h->s; STREC *p; p=h->next; while(p) { if(p->s>max ) max=p->s; p=p->next; } return max; } STREC * creat( double *s) { STREC *h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC));p->s=0; while(i { q=(STREC*)malloc(sizeof(STREC)); q->s=s[i]; i++; p->next=q; p=q; } p->next=0; return h; } outlist( STREC *h) { STREC *p; p=h->next; printf("head"); do { printf("->%2.0f",p->s);p=p->next;} while(p!=0); printf("\n\n"); }
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|