【真题1】 给定程序中已建立一个带有头结点的单向链表,在main 函数中将多次 调用fun 函数,每调用一次fun 函数,输出链表尾部结点中的数据,并释放该结 点,使链表缩短。 #include < stdio.h >#include < stdlib.h >#define N 8 typedef struct list { int data; struct list *next; } SLIST; void fun( SLIST *p) { SLIST *t, *s; t=p- >next; s=p; while(t- >next != NULL) { s=t; /**********found**********/ t=t- >___1___; } /**********found**********/ printf(" %d ",___2___); s- >next=NULL; /**********found**********/ free(___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 a[N]={11,12,15,18,19,22,25,29}; head=creatlist(a); printf("\nOutput from head:\n"); outlist(head); printf("\nOutput from tail: \n"); while (head- >next != NULL){ fun(head); printf("\n\n"); printf("\nOutput from head again :\n"); outlist(head); } } (2011年2月) 解析: 本题的考核点是链表中的基本操作。 答案:1、next 2、t- >data 3、t 【真题2】 文件MODI.C 中程序的功能是:读入一个整数m(4≤m≤10), 例如4,程序将自动在a[0]至a[3]四个数组元素中分别放入 1 4 9 16,且按逆序输出此4 个元素:16 9 4 1 。 请改正程序中的错误,使它能得出正确的结果。 注意: #include < conio.h >#include < stdio.h >#define M 10 main() { int a[M]= {0 }, i=0, j, m; clrscr(); printf( "\nPlease enter an integer number between 4 and 10: " ); scanf( "%d", &m ); /*************found**************/ for ( j=0; j< m; j++ ) a[j] = (i+1)*(i+1); printf( "\nThe output :\n" ); /*************found**************/ for( i = m-1; i >= 0; i-- ) printf( "%4f", *( a + i ) ); getch(); } (2011年2月) 解析: (1)错误:for(j=0;j< m;j++) a[j]=(i+1)*(i+1); 正确: for(j=0;j< m;j++) a[j]=(j+1)*(j+1); (2)错误:printf(“%4f”,*(a+i)); 正确:printf(“%4d”,a[i]); 【真题3】 已知学生的记录由学号和学习成绩构成,N 名学生的数据已存入a 结构 体数组中。请编写函数 fun,函数的功能是:找出成绩最高的学生记录,通过形参返 回主函数。 已给出函数的首部,请完成该函数。 注意:部分源程序存在文件prog.c 中。 请勿改动主函数main 和其它函数中的任何内容,仅在函数fun 的花括号中填入你编 写的若干语句。 #include "stdio.h" #include "string.h" #include "conio.h" #define N 10 typedef struct ss { char num[10]; int s; } STU; fun( STU a[], STU *s ) {……} main ( ) { STU a[N]={ {"A01",81},{"A02",89},{"A03",66},{"A04",87},{"A05",77}, {"A06",90},{"A07",79},{"A08",61},{"A09",80},{"A10",71} }, m ; int i; clrscr(); printf("***** The original data *****\n"); for ( i=0; i< N; i++ )printf("No = %s Mark = %d\n", a[i].num,a[i].s); fun ( a, &m ); printf ("***** THE RESULT *****\n"); printf ("The top : %s , %d\n",m.num, m.s); } (2011年2月) 解析: 本题的考核点是C语言中的循环语句和指针的使用。 解题思路:建立一个循环来寻找成绩最高的学生记录,在给出的参考函数中定 义了一个指针,用于存放学生记录中的最高成绩,最终返回主函数。 /*此题中有一结构组数,要找的是数组元素中整型成员的值最小的元素。要注 意本题中的赋值方式(分两部分,对于字符型成员只能用strcpy()函数,而不 能用等号"="进行赋(即不能用s[0].num=m[0].num),这与字符串的赋值 相同(切记)*/ fun( STU a[], STU *s ) { int i; STU *m; /*定义一个指针变量*m 来保存学生的最高成绩*/ m=a; /*初始化指针变量*/ /*使用for 循环语句依次将结构体数组中的所有元素的s 成员与第一个的结构 体变量的s 成员进行比较*/ for(i=1;i< N;i++) if (a[i].s >m[0].s) m=&a[i]; /*将最大值的下标赋给m*/ s[0].s=m[0].s; /*将分数最高的学生的成绩m[0].s 赋给结构的结构体变量 s[0].s*/ strcpy(s[0].num,m[0].num); /*将分数最高的学生的学号m[0].num 赋给 结构的结构体变量s[0].num*/ } |