设为首页 加入收藏

TOP

计算机二级C语言上机真题(17)【附详解】
2012-11-05 12:44:16 】 浏览:554
Tags:计算机 二级 语言 上机 真题 详解
【真题1】 给定程序的功能是将在字符串s 中出现、而未在字符串t 中出现的字符
形成一个新的字符串放在u 中,u 中字符按原字符串中字符顺序排列,不去掉重复字
符。 例如:当s="112345",t="2467"时,u 中的字符串为"1135"。
#include < stdio.h >
#include < string.h >
void fun (char *s,char *t, char *u)
{ int i, j, sl, tl;
sl = strlen(s); tl = strlen(t);
for (i=0; i< sl; i++)
{ for (j=0; j< tl; j++)
/************found************/
if (s[i] == t[j]) ___1___ ;
if (j >=tl)
/************found************/
*u++ = ___2___;
}
/************found************/
___3___ = '\0';
}
main()
{ char s[100], t[100], u[100];
printf("\nPlease enter string s:"); scanf("%s", s);
printf("\nPlease enter string t:"); scanf("%s", t);
fun(s, t, u);
printf("the result is: %s\n", u);
}
(2011年2月)
解析: 本题考核点为字符串的查找,赋值操作。
题中第一空填"break;":break 为跳出循环的语句。" if (s[i] == t[j])
break;"语句是指当s[i] == t[j],即t[j]中的字符已经在字符指针s[j]所指向
的字符数组中存在时,循环就结束。
题中第二空填"s[i]":"*u++ = s[i];"是将s[i]赋给*u,然后u 加1。
题中第三空填"*u":"*u='\0';",字符串的最后一个字符应该是'\0'的。
答案:【1】break 【2】s[i] 【3】*u
【真题2】 给定程序modi.c 中函数 fun 的功能是:将s 所指字符串中出现的、t1
所指子串全部替换成t2 所指子字符串,所形成的新串放在w 所指的数组中。在此
处,要求t1 和t2 所指字符串的长度相同。 例如,当s 所指字符串中的内容为:
"abcdabfab",t1 所指子串中的内容为:"ab",t2 所指子串中的内容为:"99"时,
结果,在w 所指的数组中的内容应:"99cd99f99"。 请改正程序中的错误,使它能
得出正确的结果。 注意:不要改动main 函数,
#include < conio.h >
#include < stdio.h >
#include < string.h >
int fun(char *s,char *t1,char *t2,char *w)
{
int i; char *p,*r,*a;
strcpy(w,s);
while(*w)
{p=w;r=t1;
/************found************/
while(r)
if(*r==*p) {r++;p++;}
else break;
if (*r=='\0')
{ a=w;r=t2;
/************found************/
while(*r){*a=*r;a++;r++}
w+=strlen(t2);
}
else w++;
}
}
main()
{
char s[100],t1[100],t2[100],w[100];
clrscr();
printf("\nPlease enter string S:");scanf("%s",s);
printf("\nPlease enter substring t1:");scanf("%s",t1);
printf("\nPlease enter substring t2:");scanf("%s",t2);
if (strlen(t1)==strlen(t2)) {
fun(s,t1,t2,w);
printf("\nThe result is:%s\n",w);
}
else printf("Error:strlen(t1)!=strlen(t2)\n");
}
(2011年2月)
解析: 本题着重考察C 语言指针的用法及常用算法。
在源程序中,r 为指向指针的指针变量,它存放的是指针变量的地址,题意中要
求对这个地址所指向的内容进行判断,所以须在地址前加上*,源程序"while
( r )"应改为"while(*r)"或相同作用的语句。
C 语言中,分号是语句的组成部分,各语句间以分号作为分隔。源程序"while
( *r ){ *a = *r; a++; r++ }"应改为"while(*r){*a=*r;a++;r++;}"或
相同作用的语句。
【真题3】 规定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:
除了字符串前后连续的*号之外,将串中其它*号全部删除。形参h 已指向字符串中
的第一个字母,形参p 已指向字符串中最后一个字母。在编写函数时,不得使用C
语言提供的字符串函数。 例如,字符串中的内容为:
****A*BC*DEF*G******,删除后,字符串中的内容应当是:
****ABCDEFG*******。在编写函数时,不得使用C 语言提供的字符串函数。
注意:部分源程序存在文件prog.c 中。 请勿改动主函数main 和其他函数中的任何
内容,仅在函数fun 的花括号中填入你编写的若干语句。
#include < stdio.h >
#include < conio.h >
void fun(char *a,char *h,char *p)
{……}
main()
{ char s[81],*t,*f;
printf("Enter a string:\n");gets(s);
t=f=s;
while(*t) t++;
t--;
while (*t=='*') t--;
while (*f=='*') f++;
fun(s,f,t);
printf("The string after deleted:\n");puts(s);
}
(2011年2月)
解析: 本题的考核点是删除字符串中字符的算法。
解题思路:先将原串中从首字符开始的连续*放到一字符串数组中,再将从第一
个非*字符开始到串尾连续*之前的非*字符追加其后,接着将串尾的连续*后追
加,最后将新串中的字符放到原串中覆盖原串。
本评析仅供参考。
void fun(char *a,char *h,char *p)
{char b[81],*x;
int i=0,j=0;
x=a;
while (x< h) {b[j]=*x;x++;j++;} /*将原串中从首字符开始的连续*放到
一字符串数组b中*/
while (h< =p)
if (*h!='*') {b[j]=*h;j++;h++;} /*将从第一个非*字符开始到串尾连续*
之前的非*字符追加在数组b中*/
else h++;
p++;
while (*p) {b[j]=*p;j++;p++;} /*将串尾的连续*后追加在数组b中*/
b[j]='\0';j=0;
while (b[j])
{ a[i]=b[j];i++;j++;} /*将字符串b中的所有字符复制到a字符串中覆盖
原串*/
a[i]='\0';
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇计算机二级C语言上机真题(18)【.. 下一篇计算机二级C语言上机真题(16)【..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目