【真题1】 给定程序中已建立一个带有头结点的单向链表,链表中的各结点按结点 数据域中的数据从小到大顺序链接。函数fun 的功能是:把形参x 的值放入一个新 结点并插入到链表中,插入后各结点仍保持从小到大顺序排列。 #include < stdio.h >#include < stdlib.h >#define N 8 typedef struct list { int data; struct list *next; } SLIST; void fun( SLIST *h, int x) { SLIST *p, *q, *s; s=(SLIST *)malloc(sizeof(SLIST)); /**********found**********/ s- >data=___1___; q=h; p=h- >next; while(p!=NULL && x >p- >data) { /**********found**********/ q=___2___; p=p- >next; } s- >next=p; /**********found**********/ q- >next=___3___; } SLIST *creatlist(int *a) { SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i< N; i++) { q=(SLIST *)malloc(sizeof(SLIST)); q- >data=a[i]; p- >next=q; p=q; } p- >next=0; return h; } void outlist(SLIST *h) { SLIST *p; p=h- >next; if (p==NULL) printf("\nThe list is NULL!\n"); else { printf("\nHead"); do { printf("- >%d",p- >data); p=p- >next; } while(p!=NULL); printf("- >End\n"); } } main() { SLIST *head; int x; int a[N]={11,12,15,18,19,22,25,29}; head=creatlist(a); printf("\nThe list before inserting:\n"); outlist(head); printf("\nEnter a number : "); scanf("%d",&x); fun(head,x); printf("\nThe list after inserting:\n"); outlist(head); } (2011年2月) 解析: 本题的考核点是链表中的插入操作。 对链表的插入是指将一个结点插入到一个已有的链表中。为了能做到正确插 入,必须解决两个问题:1、怎样找到插入的位置;2、怎样实现插入。 题中第一个空填"x":将要插入的数据放到一个临时开辟的存储单元中。 题中第二个空填"p":此语句将q指针指向比要插入的数据小的数据的结点。 题中第三个空填"s":此语句将q指针指向其下一个结点。 答案:【1】x 【2】p 【3】s 【真题2】 给定程序modi.c 中,函数fun 的功能是:在x 数组中放入n 个采样 值,计算并输出差值 N (Xk-X)^2 N Xk s=Σ──── 其中 x=Σ ── k=1 N k=1 N 例如n=8,输入:193.199、195.673、195.757、196.051、196.092、 196.596、196.579、196.763 时,结果应为:1.135901。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main 函数, #include < conio.h >#include < stdio.h >float fun(float x[],int n) { int j;float xa=0.0,s; for (j=0;j< n;j++) xa+=x[j]/n; s=0; for(j=0;j< n;j++) /************found************/ s+=(x[j]-xa)*(x[j]-xa)/n return s; } main() {float x[100]={193.199,195.673,195.757,196.051,196.092,196.596,196.579, 196.763}; clrscr(); printf("%f\n",fun(x,8)); } (2011年2月) 解析: 本题着重考察考生对C 语言中基本语法的掌握情况。 在C 语言中,语句的结束以分号作为分隔,所以程序中的语句"s+=(x[j]- xa)*(x[j]-xa)/n"应改为"s+=(x[j]-xa)*(x[j]-xa)/n;"或相同作用的语句。 【真题3】 学生的记录由学号和成绩组成,N 名学生的数据已在主函数中放入结构 体数组s 中,请编写函数fun,它的功能是:函数返回指定学号的学生数据,指定的 学号在主函数中输入。若没找到指定学号,在结构体变量中给学号置空串,给成绩 置-1,作为。函数值返回。(用于字符串比较的函数是strcmp)。 注意:部分源程序存在文件prog.c 中 请勿改动主函数main 和其他函数中的任何内容,仅在函数fun 的花括号中填入你编 写的若干语句。 #include < stdio.h >#define N 16 typedef struct { char num[10]; int s; } STREC; STREC fun(STREC *a,char *b) {……} main() { STREC s[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},{"GA01",9 1},{"GA07",72},{"GA08",64},{"GA06",87},{"GA015",85},{"GA013",91} ,{"GA012",64},{"GA014",91},{"GA011",91},{"GA017",64},{"GA018",64 },{"GA016",72}}; STREC h; char m[10]; int i,n;FILE *out; printf("The original data:\n"); for(i=0;i< N;i++) { if((i)%4==0)printf("\n"); printf("%s %3d",s[i].num,s[i].s); } printf("\n\nEnter the number: ");gets(m); h=fun(s,m); printf("The data: "); printf("\n%s %4d\n",h.num,h.s); printf("\n"); out=fopen("out17.dat","w"); h=fun(s,"GA013"); fprintf(out,"%s %4d\n",h.num,h.s); fclose(out); } (2011年2月) 解析: 本题的考核点是查找指定学生数据的算法。 解题思路:通过字符串比较函数将指定的学号与每一个学生的学号进行比较, 若相同,则返回指定的学生数据;若没有一个相同的,则给学号置空串,成绩 置-1 并返回。 本评析仅供参考。 答案: 方法1: {STREC k; int i,flag=0; for(i=0;i< N;i++) if(strcmp(a[i].num,b)==0) {k=a[i]; flag=1;} if(flag==1) return k; else {strcpy(k.num,””); k.s=-1; return k; } } 方法2: {STREC k;int i; for(i=0;i<N;i++) if(strcmp(a[i].num,b)==0) {k=a[i];break;} if(i<N) retrun k; else {strcpy(k..num,””); k.s=-1; return k; } } |