设为首页 加入收藏

TOP

计算机二级C语言上机真题(27)【附详解】
2012-11-05 12:44:55 】 浏览:515
Tags:计算机 二级 语言 上机 真题 详解
【真题1】 给定程序的功能是将在字符串s 中出现、而未在字符串t 中出现的字
符,构成一个新的字符串放在u 中,u 中字符按原字符串中字符顺序的逆序排列,不
去掉重复字符。
例如,当s="112345",t="2467"时,u 中的字符是:"5311"。
#include < stdio.h >#include < string.h >void fun (char *s, char *t, char
*u)
{ int i, j, sl, tl, ul;
char r, *up=u;
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___ ;
/************found************/
if(j ___2___ tl)
*u++ = s[i];
}
*u = '\0';
ul = strlen(up);
for (i=0; i< ul/2; i++) {
/************found************/
r = ___3___ ;
up[i] = up[ul-1-i];
up[ul-1-i] = r;
}
}
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
语句结合使用。
题中第二个空填" >=":"if (j >= tl) *u++ = s[i];"是和上面循环语句
"for (j=0; j< tl; j++)
if (s[i] == t[j]) break;"结合起来的。如果循环一直进行下去,则最后j和t1
是相等的。
题中第三个空填"up[i]":两个数的交换通常用一个中间变量。
答案:【1】break 【2】 >= 【3】up[i]
【真题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】 N 名学生的成绩已在主函数中放入一个带头节点的链表结构中,h 指向链
表的头节点。请编写函数fun,它的功能是:找出学生的最高分,由函数值返回。
注意:部分源程序存在文件prog.c 中。
请勿改动主函数main 和其他函数中的任何内容,仅在函数fun 的花括号中填入你编
写的若干语句。
#include < stdio.h >#include < stdlib.h >#define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun(STREC *h)
{
}
STREC *creat(double *s)
{ STREC *h,*p,*q; int i=0;
h=p=(STREC*)malloc(sizeof(STREC));p- >s=0;
while(i< N)
{q=(STREC*)malloc(sizeof(STREC));
q- >s=s[i];i++;p- >next=q;p=q;
}
p- >next=0;
return h;
}
outlist(STREC *h)
{ STREC *p;
p=h- >next; printf("head");
do
{ printf("- >%2.0f",p- >s);p=p- >next;}
while (p!=0);
printf("\n\n");
}
main()
{ double s[N]={85,76,69,85,91,72,64,87},max;
STREC *h;
h=creat(s); outlist(h);
max=fun(h);
printf("max=%6.1f\n",max);
}
(2011年2月)
解析: 本题的考核点是找最大数算法。
提示思路:通过指针指向链表,从首结点开始逐个找,找出最大值并返回。
double fun(STREC *h)
{STREC *c;
double max;
c=h- >next;
max=c- >s; /*假定首结点为最大值*/
while (c- >next!=0)
{c=c- >next; /*c 后移一个结点*/
if (max< c- >s) max=c- >s; /*找出最大值并将其赋给变量max*/
}
return max; /*返回最大值*/
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇计算机二级C语言上机真题(28)【.. 下一篇计算机二级C语言上机真题(26)【..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目