一、填空题:给定程序中,函数fun的功能是:统计出带有头结点的单向链表中结点的个数, 存放在形参n所指的存储单元中。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
#define N 8
typedef struct list
{int data;
struct list *next;
} SLIST;
SLIST *creatlist(int *a);
void outlist(SLIST *);
void fun(SLIST *h, int *n)
{SLIST *p;
/**********found**********/
___1___=0;
p=h->next;
while(p)
{ (*n)++;
/**********found**********/
p=p->___2___;
}
}
main()
{SLIST *head;
int a[N]={12,87,45,32,91,16,20,48}, num;
head=creatlist(a); outlist(head);
/**********found**********/
fun(___3___, &num);
printf("\nnumber=%d\n",num);
}
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("The list is NULL!\n"); else {printf("\nHead "); do {printf("->%d",p->data); p=p->next;} while(p!=NULL); printf("->End\n"); } } 解题答案: /**********第一空**********/ *n=0; /**********第二空**********/ p=p->next; /**********第三空**********/ fun(head, &num); ****************************************** 例如,当字符串中的内容为:"abcdabfabcdx",t中的内容为:"ab"时,输出结果应是:abcdx。 当字符串中的内容为:"abcdabfabcdx",t中的内容为:"abd"时,则程序输出未找到信息:not be found!。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #include #include char * fun (char *s, char *t) { char *p , *r, *a; /************found************/ a = Null; while (*s) {p = s; r = t; while (*r) /************found************/ if (r == p) {r++; p++;} else break; if (*r == '\0') a = s; s++; } return a ; } main() { char s[100], t[100], *p; printf("\nPlease enter string S :"); scanf("%s", s); printf("\nPlease enter substring t :"); scanf("%s", t); p = fun(s, t); if (p) printf("\nThe result is : %s\n", p); else printf("\nNot found !\n"); } 解题答案: /************found************/ a=NULL; /************found************/ if(*r==*p) ****************************************** 例如,若s所指字符串中的内容为:"ABCDEFG123456",其中字符A的ASCII码 值为奇数,因此应当删除;其中字符B的ASCII码值为偶数,但在数组中的下标为奇数,因此也应当删除;而字符2的ASCII码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其它依此类推。最后t所指的数组中的内容应是:"246"。 注意: 部分源程序存在文件PROG1.C中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入 你编写的若干语句。 给定源程序: #include #include void fun(char *s, char t[]) { } main() { char s[100], t[100]; void NONO (); printf("\nPlease enter string S:"); scanf("%s", s); fun(s, t); printf("\nThe result is: %s\n", t); NONO(); } void NONO () {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ char s[100], t[100] ; FILE *rf, *wf ; int i ; rf = fopen("in.dat","r"); wf = fopen("out.dat","w"); for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%s", s); fun(s, t); fprintf(wf, "%s\n", t); } fclose(rf); fclose(wf); 参考答案: void fun(char *s, char t[]) { int i, j = 0 ; for(i = 0 ; i < strlen(s) ; i += 2) if(s[i] % 2 == 0) t[j++] = s[i] ; t[j] = 0 ; }
二、改错题:给定程序MODI1.C中函数fun的功能是:求出s所指字符串中最后一次出现的t 所指子字符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串; 若未找到,则函数值为NULL。
三、程序题:函数fun的功能是: 将s所指字符串中除了下标为偶数、同时ASCII值也为偶数的字符外,其余的全都删除;串中剩余字符所形成的一个新串放在t所指的数组中。