一、填空题:给定程序中已建立一个带有头结点的单向链表,链表中的各结点按结点数据域中的数据递增有序链接。函数fun的功能是:把形参x的值放入一个新结点并插 入到链表中,插入后各结点数据域的值仍保持递增有序。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
#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 {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); }解题答案: /**********第一空*********/ s->daa=x; /**********第二空**********/ q=p; /**********第三空**********/ q->next=s; ****************************************** 例如,若输入:252,则输出应该是:20。若输入:202,则输出应该是:0。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #include long fun (long num) { /************found************/ long k; do {k*=num%10 ; /************found************/ num\=10 ; } while(num); return (k); } main() {long n ; printf("\nPlease enter a number:"); scanf("%ld",&n); printf("\n%ld\n",fun(n)); } 解题答案: /************found************/ long k=1; /************found************/ num/=10; ****************************************** 例如:若有5门课程的成绩是:90.5, 72, 80, 61.5, 55 则函数的值为:71.80。 注意: 部分源程序存在文件PROG1.C中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入 你编写的若干语句。 给定源程序: #include float fun (float *a , int n) { } main() {float score[30]={90.5, 72, 80, 61.5, 55}, aver; void NONO (); aver = fun(score, 5); printf("\nAverage score is: %5.2f\n", aver); NONO (); } void NONO () {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i, j ; float aver, score[5] ; fp = fopen("in.dat","r"); wf = fopen("out.dat","w"); for(i = 0 ; i < 10 ; i++) { for(j = 0 ; j < 5 ; j++) fscanf(fp,"%f,",&score[j]); aver = fun(score, 5); fprintf(wf, "%5.2f\n", aver); } fclose(fp); fclose(wf); 解题答案: { int i; float ave=0.0; for(i=0; i ave=ave/n; return ave; }
二、改错题:给定程序MODI1.C中函数fun的功能是:计算正整数num的各位上的数字之积。
三、程序题:请编写一个函数fun,它的功能是:计算n门课程的平均分,计算结果作为函数值返回。