知ing

最新计算机二级C语言上机试题汇编100套

NCRE研究组 编 / 高等教育出版社

゛Zirro、Y 上传

查看本书

11套: 

给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:246810,逆置后,从头至尾结点数据域依次为: 108642。 

请在程序的下划线处填入正确的内容并把下划线删除使程序得出正确的结果。 

注意:源程序存放在考生文件夹下的BLANK1.C中。 

不得增行或删行,也不得更改程序的结构! 

给定源程序: 

#include <stdio.h> 

#include <stdlib.h> 

#define N 5 

typedef struct node { 

int data; 

struct node *next; 

} NODE; 

void fun(NODE *h) 

{ NODE *p, *q, *r; 

/ **********found**********/ 

p = h->__1__; 

/ **********found**********/ 

if (p==__2__) return; 

q = p->next; 

p->next = NULL; 

while (q) 

{ r = q->next; q->next = p; 

/ **********found**********/ 

p = q; q = __3__; 

h->next = p; 

NODE *creatlist(int a[]) 

{ NODE *h,*p,*q; int i; 

h = (NODE *)malloc(sizeof(NODE)); 

h->next = NULL; 

for(i=0; i<N; i++) 

{ q=(NODE *)malloc(sizeof(NODE)); 

q->data=a[i]; 

q->next = NULL; 

if (h->next == NULL) h->next = p = q; 

else { p->next = q; p = q; } 

return h; 

void outlist(NODE *h) 

{ NODE *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"); 

main() 

{ NODE *head; 

int a[N]={2,4,6,8,10}; 

head=creatlist(a); 

printf("\nThe original list:\n"); 

outlist(head); 

fun(head); 

printf("\nThe list after inverting :\n"); 

outlist(head); 

解题思路: 

本题是考察使用链表方法,对链表的结点数据进行降序排列。 

第一处:使用结构指针p,来控制链表的结束,p必须指向h结构指针的next指针,来定位的初始位置。所以应填写:h->next。 

第二处:判断p指针是否结束,所以应填写:0。 

第三处:q指向原qnext指针,所以应填:r。 

*************************************************** 

给定程序MODI1.C中函数fun的功能是计算s所指字符串中含有t所指字符串的数目并作为函数值返回。 

请改正函数fun中指定部位的错误使它能得出正确的结果。 

注意不要改动main函数不得增行或删行也不得更改程序的结构

给定源程序: 

#include <stdio.h> 

#include <string.h> 

#define N 80 

int fun(char *s, char *t) 

{ int n; 

char *p , *r; 

n=0; 

while ( *s ) 

{ p=s; 

/ *********found**********/ 

r=p; 

while(*r) 

if(*r==*p) { r++; p++; } 

else break; 

/ *********found**********/ 

if(*r= 0) 

n++; 

s++; 

 

return n; 

main() 

{ char a[N],b[N]; int m; 

printf("\nPlease enter string a : "); gets(a); 

printf("\nPlease enter substring b : "); gets( b ); 

m=fun(a, b); 

printf("\nThe result is : m = %d\n",m); 

解题思路: 

第一处程序中子串是由变量t来实现的,再根据下面while循环体中语句可知,所以应改为:r=t;。 

第二处是判断相等的条件,所以应改为:if(*r==0)。 

*************************************************** 

请编写函数fun, 函数的功能是将放在字符串数组中的M个字符串(每串的长度不超过N), 按顺序合并组成一个新的字符串。函数fun中给出的语句仅供参考。 

例如字符串数组中的M个字符串为AAAA 

BBBBBBB 

CC 

则合并后的字符串的内容应是: AAAABBBBBBBCC。 

提示:strcat(a,b)的功能是将字符串b复制到字符串a的串尾上,成为一个新串。 

注意:部分源程序在文件PROG1.C中。 

请勿改动主函数main和其它函数中的任何内容仅在函数fun的花括号中填入你编写的若干语句。 

给定源程序: 

#include <stdio.h> 

#define M 3 

#define N 20 

void fun(char a[M][N], char *b) 

/ * 以下代码仅供参考 */ 

int i; *b=0; 

main() 

{ char w[M][N]={"AAAA","BBBBBBB","CC"}, a[100]; 

int i ; 

printf("The string:\n"); 

for(i=0; i<M; i++)puts(w[i]); 

printf("\n"); 

fun(w,a); 

printf("The A string:\n"); 

printf("%s",a);printf("\n\n"); 

NONO(); 

 

解题思路: 

本题是考察字符串的操作。 

使用for循环以及C语言函数strcat依次连接起来。 

参考答案: 

#include <stdio.h> 

#define M 3 

#define N 20 

void fun(char a[M][N], char *b) 

/ * 以下代码仅供参考 */ 

int i; *b=0; 

for(i = 0 ; i < M ; i++) 

strcat(b, a[i]) ; 

※※※※※※※※※※※※※※※※※※※※※※※※※ 


查看更多