2015年计算机二级C语言上机操作题及答案(40)
发布时间:2011/7/14 11:29:18 来源:城市学习网 编辑:ziteng
第40套
填空题
请补充main函数,该函数的功能是:从键盘出入一个字符串并保存在字符str1中,把字符串str1中下标为偶数的字符保存在字符串str2中并输出。
例如,当str1=”abcdefg”时,则str2=”aceg”。
注意:部分源程序给出如下
仅在横线上填入所编写的若干表达式或语句,请勿改动函数中的任何内容。
试题程序:
#include <stdio.h>
#include <conio.h>
#define LEN 80
main()
{
char str1[LEN], str2[LEN];
char *p1 = str1, *p2 = str2;
int i = 0, j = 0;
printf("Enter the string:\n");
scanf(___1___);
printf("****** the origial string ********\n");
while (*(p1+j))
{
printf("___2___", *(p1+j));
j++;
}
for (i=0; i<j; i+=2)
*p2++ = *(str1+i);
*p2 = '\0';
printf("\nThe new string is:%s\n", ___3___);
}
第1处填空:”%s”,str1
第2处填空:%c
第3处填空:str2[NextPage] 改错题
下列给定程序中,函数fun的功能是:将n个无序整数从小到大排序。
请改正程序中的错误,使其能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题 程序:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
fun(int n, int *a)
{
int i, j, p, t;
for (j=0; j<n-1; j++)
{
p = j;
/********found********/
for (i=j+1; i<n-1; i++)
if (a[p] > a[i])
/********found********/
t = i;
if (p != j)
{
t = a[j];
a[j] = a[p];
a[p] = t;
}
}
}
putarr(int n, int *z)
{
int i;
for (i=1; i<=n; i++, z++)
{
printf("%4d", *z);
if (!(i%10))
printf("\n");
}
printf("\n");
}
main()
{
int aa[20] = {9, 3, 0, 4, 1, 2, 5, 6, 8, 10, 7}, n = 11;
printf("\n\nBefore sorting %d numbers:\n", n);
putarr(n, aa);
fun(n, aa);
printf("\nAfter sorting %d numbers:\n", n);
putarr(n, aa);
}
第1处:for (i=j+1;ii<n-1;i++)应改为for(i=j+1;i<n;i++)
第2处:t=I;应改为p=i;[NextPage] 编程题
请编写函数FUN,该函数的功能是:移动字符串中的内容,移动的规则如下:把第1到第M个字符,平移到字符串的最后,把第M+1到最后的字符移到字符串的前部。
例如,字符串原有的内容为ABCDEFGHIJK,M的值为3,移动后,字符串中的内容应该是DEFGHIJKABC。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio.h>
#include <string.h>
#define N 80
void fun(char *w, int m)
{
}
main()
{
char a[N]= "ABCDEFGHIJK";
int m;
FILE *out;
printf("The original string:\n");
puts(a);
printf("\n\nEnter m: ");
scanf("%d",&m);
fun(a,m);
printf("\nThe string after moving:\n");
puts(a);
printf("\n\n");
out=fopen ("out.dat", "w");
fun(a, strlen(a)-m);
fun(a, 3);
fprintf(out, "%s", a);
fclose (out );
}
答案是:
void fun(char *w,int m)
{
char b[N];
int I,j=0;
for(i=0;i<m;i++)
{
b[j]=w[i];
j++;
}
for(i=0;i<strlen(w)-m;i++)
w[i]=w[i+m];
for(j=0;j<m;j++)
{
w[i]=b[j];
i++;
}
w[i]=’\0’;
}