2016計算機二級c上機考試題庫
- 計算機二級
- 關注:2.93W次
一、程序填空題
請補充函數proc(char*str),該函數的功能是把字符串中的內容逆置。
例如,字符串中原有的字符串為abcdef9,則調用該函數後,串中的內容變為gfedcba。
注意:部分源程序給出如下。
請勿改動main()函數和其他函數中的任何內容,僅在函數proc()的橫線上填入所編寫的.若干表達式或語句。
試題程序:
#include
#include t
#include
#include
#define M 81
void proc(char*str)
【2】 ;
【3】 ;
}
}
void main()
{
char str[M];
system("CLS"):
printf("Enter a string:");
gets(str);
printf("The original string is:");
puts(str);
proc(str);
printf("n");
printf("The string after modified:");
puts(str);
}
二、程序改錯題
下列給定程序中,函數proc()的功能是:將字符串str 中所有字符複製到字符串b中,要求每複製3個字符之後插入一個空格。例如,在調用proc()函數之前給字符串str 輸入abcdefghijk,調用函數之後,字符串b中的內容則為abe def ghijk。
請修改程序中的錯誤,使它能得出正確的結果。
注意:不要改動main()函數,不得增行或刪行,也不得更改程序的結構。
試題程序:
#include
void proc(char*str,char*b)
{
int i,k=0:
while(*str)
//****found****
{
i=1;
//****found****
while(i<3||*str)
{
b[k]=*str;
k++;str++;i++;
}
if(*str)
//****found****
{b[k]=’’;}
void main()
{
char str[80],b[80];
printf("Enter a string:");gets(str);
printf("The original string:");
puts(str);
proc(str,b);
printf("nThe string after insert
space:");puts(b);printf("nn");
}
三、程序設計題
請編寫函數proc(),該函數的功能是:移動一維數組中的內容,若數組中有n個整數,要求把下標從P到n-1(p≤n-1)的數組元素平移到數組的前面。
例如,一維數組中的原始內容為1,2,3,4,5,6,7,8,9,10,11,12,13,14,p的值為4。移動後,一維數組中的內容應為5,6,7,8,9,10,11,12,13,14,1,2,3,4。
注意:部分源程序給出如下。
請勿改動main()函數和其他函數中的任何內容,僅在函數proc()的花括號中填入所編寫的若干語句。
試題程序:
#include
#define M 80
void proc(int * w,int P,int n)
{
}
void main()
{
int arr[M]={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int i,p,n=14;
printf("The original data:n");
for(i=0:i
printf("%3d",arr[i]);
printf("nnEnter P:");
scanf("%d",&p);
proc(arr,p,n);
printf("nThe data after moving:n");
for(i=0:i
printf("%3d",arr[i]);
printf("nn");
}
【參考答案】
一、程序填空題
【1】i
【解析】要將字符串中的內容逆置.可以通過將字符串中的第一個字符和最後一個字符互換,第二個和倒數第二個互換,直到字符串str最中間的字符為止,因此,【1】處填“i
二、程序改錯題
(1)錯誤:i=1:
正確:i=0;
(2)錯誤:while(i<3||*str)
正確:while(i<3&&*str)
(3)錯誤:b[k]=’’;
正確:b[k++]=’’;
【解析】由函數proc()可知,變量i為計算每次字符個數是否到3的計數器變量,其初始值為0,因此,i=1;應改為i =0;。當計數器i小於3,而且字符串str沒有結束時,將str 中的字符賦值給字符串b,因此,“while(i<3||*str)”應改為“while(i<3&&*str)”。每次計數器變量為3而字符串str沒有結束時,為字符串b賦值為空格,而不是空字符,因此,“b[k]=’’;”應改為“b[k++]=’’;”。
三、程序設計題
void proc(int*w,int p,int n)
{
int i,j,t;
for(i=P;i<=n-1;i++)
{ t=w[n-1];//t放最後一個元素
for(j=n-2;j>=0;j--)
w[j+1]=w[j]; //每循環一次,把所有的元
素往後侈
w[0]=t; //再把最後一個放到第一個空間中
}
}
【解析】題目中要求把下標從p到n-1的數組元素平移到數組的前面,可以通過每一次循環將最後一個元素放在第一個位置上,使其成為第一個元素,其餘元素後移一個位置。通過n-1-p次循環實現將從p到n-1的數組元素平移到數組的前面。
- 文章版權屬於文章作者所有,轉載請註明 https://xuezhezhai.com/zh-mo/jsj/erji/66g6v3.html