二级上级模拟题5套.docx
《二级上级模拟题5套.docx》由会员分享,可在线阅读,更多相关《二级上级模拟题5套.docx(20页珍藏版)》请在冰豆网上搜索。
![二级上级模拟题5套.docx](https://file1.bdocx.com/fileroot1/2023-6/8/8e6d052a-12f1-4e61-8e96-c0c3b972c711/8e6d052a-12f1-4e61-8e96-c0c3b972c7111.gif)
二级上级模拟题5套
讲解题目
改错题:
21.下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。
函数fun的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。
请改正函数fun中的错误,使它能得出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include
#include
#include
typedefstructaa
{
intdata;
structaa*next;
}NODE;
intfun(NODE*h)
{
intmax=-1;
NODE*p;
/********found********/
p=h;
while(p)
{
/********found********/
if(p->datamax=p->data;
/********found********/
p=h->next;
}
returnmax;
}
voidoutresult(ints,FILE*pf)
{
fprintf(pf,"\nThemaxinlink:
%d\n",s);
}
NODE*creatlink(intn,intm)
{
NODE*h,*p,*s,*q;
inti,x;
h=p=(NODE*)malloc(sizeof(NODE));
h->data=9999;
for(i=1;i<=n;i++)
{
s=(NODE*)malloc(sizeof(NODE));
s->data=rand()%m;
s->next=p->next;
p->next=s;
p=p->next;
}
p->next=NULL;
returnh;
}
outlink(NODE*h,FILE*pf)
{
NODE*p;
p=h->next;
fprintf(pf,"\nTHELIST:
\n\nHEAD");
while(p)
{
fprintf(pf,"->%d",p->data);
p=p->next;
}
fprintf(pf,"\n");
}
intmain()
{
NODE*head;intm;
head=creatlink(12,100);
outlink(head,stdout);
m=fun(head);
printf("\nTHERESULT:
\n");
outresult(m,stdout);
return0;
}
第一套
填空题
给定程序中,涵数fun的功能是:
把形参s所指字符串中下标为奇数的字符右移到下一个奇数位置,最右边被移出字符串的字符串的字符绕回放到第一个奇数位置,下标为偶数的字符不动(注:
字符串的长度大于等于2)
例如,形参s所指的字符串为:
abcdefgh,执行结果为:
ahcbedgf.
注意:
部分源程序给出如下。
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
试题程序:
#include
voidfun(char*s)
{
inti,n,k;
charc;
n=0;
for(i=0;s[i]!
='\0';i++)
n++;
if(n%2==0)
k=n-___1___;
else
k=n-2;
c=___2___;
for(i=k-2;i>=1;i=i-2)
s[i+2]=s[i];
s[1]=___3___;
}
intmain()
{
chars[80]="abcdefgh";
printf("\nTheoriginalstringis:
%s\n",s);
fun(s);
printf("\nTheresultis:
%s\n",s);
return0;
}
改错题:
10.下列给定程序中,函数fun的功能是:
将s所指字符串中出现的t1所指子串全部替换成t2所指子字符串,所形成的新串放在w所指的数组中。
在此处,要求t1和t2所指字符串的长度相同。
例如,当s指字符串中的内容为abcdabfab,t1指子串中的内容为ab,t2所指子串中的内容为99时,结果,在w所指的数组中的内容应为99cd99f99。
请改正程序中的错误,使它能得出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include
#include
#include
/********found********/
intfun(char*s,char*t1,char*t2,char*w)
{
inti;
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;
while(*r)
{
*a=*r;
a++;
r++;}
w+=strlen(t2);
}
else
w++;
}
}
main()
{
chars[100],t1[100],t2[100],w[100];
printf("\nPleaseenterstrings:
");
scanf("%s",s);
printf("\nPleaseentersubstringt1:
");
scanf("%s",t1);
printf("\nPleaseentersubstringt2:
");
scanf("%s",t2);
if(strlen(t1)==strlen(t2)){
fun(s,t1,t2,w);
printf("\nTheresultis:
%s\n",w);
}
else
printf("Error:
strlen(t1)!
=strlen(t2)\n");
}
编程题:
100.请编写函数fun,其功能是:
计算并输出
S=1+(1+20.5)+(1+20.5+30.5)+...+(1+20.5+30.5+...+n0.5)
例如,若主函数从键盘给n输入20后,则输出为s=534.188884。
注意:
n的值要求大于1但不大于100。
部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
doublefun(intn)
{
}
main()
{
intn;
doubles;
printf("\n\nInputn:
");
scanf("%d",&n);
s=fun(n);
printf("\n\ns=%f\n\n",s);
}
第二套
填空题
请补充fun函数,该函数的功能是:
依次取出字符串中所有小写字母,形成新的字符串,并取代原字符串.
注意:
部分源程序给出如下。
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
试题程序:
#include
#include
voidfun(char*s)
{
inti=0;
char*p=s;
while(___1___)
{
if(*p>='a'&&*p<='z')
{
s[i]=*p;
___2___;
}
p++;
}
s[i]=___3___;
}
intmain()
{
charstr[80];
printf("\nEnterastring:
");
gets(str);
printf("\n\nThestringis:
%s\n",str);
fun(str);
printf("\n\nThestringofchangingis:
%s\n",str);
return0;
}
改错题:
92.下列给定程序中,fun函数的功能是:
求s=aa…aa-…-aaa-aa-a(此处aa…aa表示n个a,a和n的值在1至9之间)。
例如a=3,n=6,则以上表达式为:
s=333333-33333-3333-333-33-3
其值是296298。
a和n是fun函数的形参,表达式的值作为函数值传回main函数。
请改正程序中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include
#include
longfun(inta,intn)
{
intj;
/********found********/
longs=0,t=1;
/********found********/
for(j=0;j<=n;j++)
t=t*10+a;
s=t;
for(j=1;j{
/********found********/
t=t%10;
s=s-t;
}
return(s);
}
main()
{
inta,n;
clrscr();
printf("\nPleaseenteraandn:
");
scanf("%d%d",&a,&n);
printf("Thevalueoffunctionis%ld\n",fun(a,n));
}
编程题:
67.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:
函数返回指定学号的学生数据,指定的学号在主函数中输入。
若没找到指定学号,在结构体变量中给学号置空串,给成绩置-1,作为函数值返回(用于字符串比较的函数是strcmp)。
注意:
部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#defineN16
typedefstruct
{
charnum[10];
ints;
}STREC;
STRECfun(STREC*a,char*b)
{
}
main()
{
STRECs[N]={{"GA005",85},{"GA003",76},{"GA002",69},{"GA004",85},
{"GA001",91},{"GA007",72},{"GA008",64},{"GA006",87},
{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},
{"GA011",77},{"GA017",64},{"GA018",64},{"GA016",72}};
STRECh;
charm[10];
inti,n;FILE*out;
printf("Theoriginaldata:
\n");
for(i=0;i{
if(i%4==0)printf("\n");
printf("%s%3d",s[i].num,s[i].s);
}
printf("\n\nEnterthenumber:
");
gets(m);
h=fun(s,m);
printf("Thedata:
");
printf("\n%s%4d\n",h.num,h.s);
printf("\n");
out=fopen("out17.dat","w");
h=fun(s,"GA013");
fprintf(out,"%s%4d\n",h.num,h.s);
fclose(out);
}
第三套
填空题
Str为一个字符序列.请补充FUN函数的功能是:
查找STR中值为X的元素,返回找到值为X的元素个数,并把这些值为X的元素下标依次保存在数组BB中.
例如,在”abcdefahij”中查找‘A’,结果为:
2个‘A’,下标依次为0、6。
注意:
部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
试题程序:
#include
#include
#defineN20
intbb[N];
intfun(char*str,charch)
{
inti=0,n=0;
chart=ch;
char*p=str;
while(*p)
{
if(___1___)
___2___;
p++;
i++;
}
return___3___;
}
intmain()
{
charstr[N];
charch;
inti,n;
printf("*******Inputtheoriginalstring*******\n");
gets(str);
printf("*******TheOriginalstring*******\n");
puts(str);
printf("*******Inputcharacter*******\n");
scanf("%c",&ch);
n=fun(str,ch);
printf("\nThenumberofcharacteris:
%d\n",n);
printf("*******Thesuffixofcharacter*******\n");
for(i=0;iprintf("%d",bb[i]);
return0;
}
改错题:
63.下列给定程序中,函数fun的功能是:
将字符串p中的所有字符复制到字符串b中,
要求每复制三个字符之后插入一个空格。
例如,在调用fun函数之前给字符串a输入
ABCDEFGHIJK,调用函数之后,字符串b中的内容则为ABCDEFGHIJK。
请改正程序中的错误,使它能得出正确结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
voidfun(char*p,char*b)
{
inti,k=0;
while(*p)
{
/********found********/
i=1;
/********found********/
while(i<3||*p)
{
b[k]=*p;
k++;
p++;
i++;
}
/********found********/
if(*p)
{
b[k]='';
}
}
b[k]='\0';
}
main()
{
chara[80],b[80];
printf("Enterastring:
");
gets(a);
printf("Theoriginalstring:
");
puts(a);
fun(a,b);
printf("\nThestringafterinsertspace:
");
puts(b);
printf("\n\n");
}
编程题:
31.请编写函数fun,其功能是:
将s所指字符串中除了下标为偶数、同时ASCII值也为偶数的字符外,其余的全都删除;串中剩余字符所形成的一个新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFG123456,其中字符A的ASCII码值为奇数,因此应当删除;其中字符B的ASCII码值为偶数,但在数组中的下标为奇数,因此也应当删除;而字符2的ASCII码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其他依此类推。
最后t所指的数组中的内容应是246。
注意:
部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#include
voidfun(char*s,chart[])
{
}
main()
{
chars[100],t[100];
clrscr();
printf("\nPleaseenterstringS:
");
scanf("%s",s);
fun(s,t);
printf("\nTheresultis:
%s\n",t);
}
第四套
填空题
给定程序的功能是分别统计字符串中大写字母和小写字母的个数。
例如,给字符串SS输入:
AaaaBBb123CCccccd,则输出结果应为:
upper=5,lower=9。
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
试题程序:
#include
voidfun(char*s,int*a,int*b)
{
while(*s)
{
if(*s>='A'&&*s<='Z')
___1___;
if(*s>='a'&&*s<='z')
___2___;
s++;
}
}
intmain()
{
chars[100];
intupper=0,lower=0;
printf("\nPleaseastring:
");
gets(s);
fun(s,&upper,&lower);
printf("\nupper=%dlower=%d\n",___3___);
return0;
}
改错题:
48.下列给定程序中,函数fun的功能是:
从N个字符串中找出最长的那个串,并将其地址作为函数值返回。
各字符串在主函数中输入,并放入一个字符串数组中。
请改正程序中的错误,使它能得出正确结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include
#include
#defineN5
#defineM81
/********found********/
fun(char(*sq)[N])
{
inti;
char*sp;
sp=sq[0];
for(i=0;iif(strlen(sp)sp=sq[i];
/********found********/
returnsq;
}
main()
{
charstr[N][M],*longest;
inti;
printf("Enter%dlines:
\n",N);
for(i=0;igets(str[i]);
printf("\nTheNstring:
\n",N);
for(i=0;iputs(str[i]);
longest=fun(str);
printf("\nThelongeststring:
\n");
puts(longest);
}
编程题:
93.请编写函数fun,它的功能是计算下列级数和,和值由函数值返回。
例如,当n=10,x=0.3时,函数值为1.349859。
注意:
部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#include
doublefun(doublex,intn)
{
}
main()
{
clrscr();
printf("%f\n",fun(0.3,10));
}