请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
#define N 8
typedef struct list
{int data;
struct list *next;
} SLIST;
void fun(SLIST *h)
{SLIST *p, *q;
p=h->next;
if (p!=NULL)
{q=p->next;
while(q!=NULL)
{if (p->data==q->data)
{p->next=q->next;
/**********found**********/
free(___1___);
/**********found**********/
q=p->___2___;
}
else
{p=q;
/**********found**********/
q=q->___3___;
}
}
}
}
SLIST *creatlist(int *a)
{SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; 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]={1,2,2,3,4,4,4,5};
head=creatlist(a);
printf("\nThe list before deleting :\n"); outlist(head);
fun(head);
printf("\nThe list after deleting :\n"); outlist(head);
}
解题答案:
/**********found**********/
free(q);
/**********found**********/
q=p->next;
/**********found**********/
q=q->next;
****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是:用选择法对数组中的n个元素按从小到大的顺序进行排序。
请修改程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行和删行,也不得更改程序的结构!
给定源程序:
#include
#define N 20
void fun(int a[], int n)
{int i, j, t, p;
for (j = 0 ;j < n-1 ;j++) {
/************found************/
p = j
for (i = j;i < n; i++)
if(a[i] < a[p])
/************found************/
p = j;
t = a[p] ; a[p] = a[j] ; a[j] = t;
}
}
main()
{
int a[N]={9,6,8,3,-1},i, m = 5;
printf("排序前的数据:");
for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n");
fun(a,m);
printf("排序后的数据:");
for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n");
}
解题答案:
/************found************/
p=j;
/************found************/
p=i;
******************************************
[NextPage] 三、程序题:请编写一个函数fun,它的功能是:求出1到m之间(含m)能被7或11整除的所有整数放在数组a中,通过n返回这些数的个数。例如,若传送给m的值为50,则程序输出:
7 11 14 21 22 28 33 35 42 44 49
注意: 部分源程序存在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入
你编写的若干语句。
给定源程序:
#include
#define M 100
void fun (int m, int *a , int *n)
{
}
main()
{int aa[M], n, k;
void NONO ();
fun (50, aa, &n);
for (k = 0; k < n; k++)
if((k+1) ==0) printf("\n");
else printf("M", aa[k]);
printf("\n");
NONO();
}
void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *fp, *wf ;
int i, n, j, k, aa[M], sum ;
fp = fopen("in.dat","r");
wf = fopen("out.dat","w");
for(i = 0 ; i < 10 ; i++) {
fscanf(fp, "%d,", &j);
fun(j, aa, &n);
sum = 0 ;
for(k = 0 ; k < n ; k++) sum+=aa[k] ;
fprintf(wf, "%d\n", sum);
}
fclose(fp);
fclose(wf);
}
参考答案:
{
int i ;
*n=0 ;
for(i=7 ; i<=m; i++)
if((i % 7 == 0) || (i % 11 == 0)) a[(*n)++]=i ;
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|