為大家收集整理了《2013計算機二級C語言上機題庫及答案解析(2)》供大家參考,希望對大家有所幫助?。?!
填空題
給定程序中,函數(shù)fun的功能是:統(tǒng)計出帶有頭結點的單向鏈表中結點的個數(shù), 存放在形參n所指的存儲單元中。
請在程序的下劃線處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結果。
注意:源程序存放在考生文件夾下的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;
___1___=0;
p=h->next;
while(p)
{ (*n)++;
p=p->___2___;
}
}
main()
{ SLIST *head;
int a[N]={12,87,45,32,91,16,20,48}, num;
head=creatlist(a); outlist(head);
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");
}
}
解題思路:
本題是要求統(tǒng)計出帶有頭結點的單向鏈表中結點的個數(shù)。
第一處:對n所指的存儲單元進行初始化,所以應填:*n。
第二處:指向p的下一個結點,所以應填:next。
第三處:函數(shù)調(diào)用,在主函數(shù)中已經(jīng)給出了head,所以應填:head。
改錯題
給定程序MODI1.C中函數(shù)fun的功能是:求出s所指字符串中最后一次出現(xiàn)的t 所指子字符串的地址,通過函數(shù)值返回,在主函數(shù)中輸出從此地址開始的字符串; 若未找到,則函數(shù)值為NULL。
例如,當字符串中的內(nèi)容為:"abcdabfabcdx",t中的內(nèi)容為:"ab"時,
輸出結果應是:abcdx。 當字符串中的內(nèi)容為:"abcdabfabcdx",t中的內(nèi)容為:"abd"時, 則程序輸出未找到信息:not be found!。
請改正程序中的錯誤,使它能得出正確的結果。
注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結構!
給定源程序:
#include
#include
char * fun (char *s, char *t )
{
char *p , *r, *a;
a = Null;
while ( *s )
{ p = s; r = t;
while ( *r )
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" );
}
解題思路:
第一處:指向空指針錯誤,Null應NULL。
第二處:比較指針位置的值是否相等,所以應改為:if(*r==*p)。
編程題
函數(shù)fun的功能是: 將s所指字符串中除了下標為偶數(shù)、同時ASCII值也為偶數(shù)的字符外,其余的全都刪除;串中剩余字符所形成的一個新串放在t所指的數(shù)組中。 例如,若s所指字符串中的內(nèi)容為:"ABCDEFG123456",其中字符A的ASCII碼
值為奇數(shù),因此應當刪除;其中字符B的ASCII碼值為偶數(shù),但在數(shù)組中的下標為
奇數(shù),因此也應當刪除;而字符2的ASCII碼值為偶數(shù),所在數(shù)組中的下標也為偶數(shù),因此不應當刪除,其它依此類推。最后t所指的數(shù)組中的內(nèi)容應是:"246"。
注意: 部分源程序存在文件PROG1.C中。
請勿改動主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入
你編寫的若干語句。
給定源程序:
#include
#include
void fun(char *s, char t[])
{
}
main()
{
char s[100], t[100];
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
NONO();
}
解題思路:
本題是從一個字符串按要求生成另一個新的字符串。我們使用for循環(huán)語句來解決這個問題。
參考答案:
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 ;
}
填空題
給定程序中,函數(shù)fun的功能是:統(tǒng)計出帶有頭結點的單向鏈表中結點的個數(shù), 存放在形參n所指的存儲單元中。
請在程序的下劃線處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結果。
注意:源程序存放在考生文件夾下的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;
___1___=0;
p=h->next;
while(p)
{ (*n)++;
p=p->___2___;
}
}
main()
{ SLIST *head;
int a[N]={12,87,45,32,91,16,20,48}, num;
head=creatlist(a); outlist(head);
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");
}
}
解題思路:
本題是要求統(tǒng)計出帶有頭結點的單向鏈表中結點的個數(shù)。
第一處:對n所指的存儲單元進行初始化,所以應填:*n。
第二處:指向p的下一個結點,所以應填:next。
第三處:函數(shù)調(diào)用,在主函數(shù)中已經(jīng)給出了head,所以應填:head。
改錯題
給定程序MODI1.C中函數(shù)fun的功能是:求出s所指字符串中最后一次出現(xiàn)的t 所指子字符串的地址,通過函數(shù)值返回,在主函數(shù)中輸出從此地址開始的字符串; 若未找到,則函數(shù)值為NULL。
例如,當字符串中的內(nèi)容為:"abcdabfabcdx",t中的內(nèi)容為:"ab"時,
輸出結果應是:abcdx。 當字符串中的內(nèi)容為:"abcdabfabcdx",t中的內(nèi)容為:"abd"時, 則程序輸出未找到信息:not be found!。
請改正程序中的錯誤,使它能得出正確的結果。
注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結構!
給定源程序:
#include
#include
char * fun (char *s, char *t )
{
char *p , *r, *a;
a = Null;
while ( *s )
{ p = s; r = t;
while ( *r )
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" );
}
解題思路:
第一處:指向空指針錯誤,Null應NULL。
第二處:比較指針位置的值是否相等,所以應改為:if(*r==*p)。
編程題
函數(shù)fun的功能是: 將s所指字符串中除了下標為偶數(shù)、同時ASCII值也為偶數(shù)的字符外,其余的全都刪除;串中剩余字符所形成的一個新串放在t所指的數(shù)組中。 例如,若s所指字符串中的內(nèi)容為:"ABCDEFG123456",其中字符A的ASCII碼
值為奇數(shù),因此應當刪除;其中字符B的ASCII碼值為偶數(shù),但在數(shù)組中的下標為
奇數(shù),因此也應當刪除;而字符2的ASCII碼值為偶數(shù),所在數(shù)組中的下標也為偶數(shù),因此不應當刪除,其它依此類推。最后t所指的數(shù)組中的內(nèi)容應是:"246"。
注意: 部分源程序存在文件PROG1.C中。
請勿改動主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入
你編寫的若干語句。
給定源程序:
#include
#include
void fun(char *s, char t[])
{
}
main()
{
char s[100], t[100];
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
NONO();
}
解題思路:
本題是從一個字符串按要求生成另一個新的字符串。我們使用for循環(huán)語句來解決這個問題。
參考答案:
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 ;
}