2015年计算机二级C语言上机操作题及答案(39)
发布时间:2011/7/14 11:28:28 来源:城市学习网 编辑:ziteng
第39套
填空题
请补充main函数,该函数的功能是:从一个字符串中截取前面若干个给定字符数的子字符串。其冲,str1指向原字符串,截取后的字符串存放在str2所指的字符数组中,n中存放预截取的字符个数。
例如,当str1=”abcdefg”,然后输入3,则str2=”abc”.
注意:部分源程序给出如下
仅在横线上填入所编写的若干表达式或语句,请勿改动函数中的任何内容。
试题程序:# include <stdio.h>
# include <conio.h>
# define LEN 80
main()
{
char str1[LEN], str2[LEN];
int n, i;
printf("Enter the string:\n");
gets(str1);
printf("Enter the position of the string deleted:");
scanf(___1___);
for (i=0; i<n; i++)
___2___
str2[i] = '\0';
printf("The new string is:%s\n", ___3___);
}
第1处填空:”%d”,&n
第2处填空:str2[i]=str1[i]
第3处填空:str2 [NextPage] 改错题
下列给定程序中,函数fun的功能是,将s所指字符串的正序和反序进行连接,形成一个新串放在t所指的数组中,例如,当s所指这字符串为ABCD时,则t所指字符串中的内容应为ABCDDCBA。
请改正程序中的错误,使其能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题 程序:
#include <conio.h>
#include <stdio.h>
#include <string.h>
/********found********/
void fun(char s, char t)
{
int i, d;
d = strlen(s);
for (i=0; i<d; i++)
t[i] = s[i];
for (i=0; i<d; i++)
t[d+i] = s[d-1-i];
/********found********/
t[2*d-1] = '\0';
}
main()
{
char s[100], t[100];
printf("\nPlease enter string S:");
scanf("%s", s);
fun(s, t);
printf("\nThe result is : %s\n", t);
}
第1处:void fun(char s,char t)应改为void fun(char *s;char *t)
第2处:t[2*d-1]=’\0’;应改为t[2*d]=’\0’;或t[d+i]=’\0’;或t[2*d]=0;或t[d+i]=0;[NextPage] 编程题
请编写函数FUN,该函数的功能是:移动一维数组中的内容;若数组中有N个整数,要求把下标从0到P(P小于等于N-1)的数组元素平移到数组的最后。
例如,一维数组中的原始内容为:1,2,3,4,5,6,7,8,9,10;P的值为3。移动后,一维数组中的内容应为:5,6,7,8,9,10,1,2,3,4。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <stdio.h>
#define N 80
void fun(int *w, int p, int n)
{
}
main()
{
int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i,p,n=15;
FILE *out;
int test[N] = {1,1,2,3,5,8,13,21,34,55,89,144};
printf("The original data:\n");
for(i=0; i<n; i++)
printf("%3d",a[i]);
printf("\n\nEnter p: ");
scanf("%d",&p);
fun(a,p,n);
printf("\nThe data after moving :\n");
for(i=0; i<n; i++)
printf("%3d",a[i]);
printf("\n\n");
out=fopen("out.dat", "w");
fun(test,6,12);
for(i=0;i<12;i++)
fprintf(out, "%d\n", test[i]);
fclose(out);
}
答案是:
void fun(int *w,int p,int n)
{
int b[N],I,j=0;
for(i=0;i<=p;i++)
b[i]=w[i];
for(i=p+1;i<n;i++)
{
w[j]=w[i];
j++;
}
for(i=0;i<=p;i++)
{
w[j]=b[i];
j++;
}
}