全國計(jì)算機(jī)等級考試二級C語言練習(xí)題(十一)
1.程序填空題
給定程序中,函數(shù)fun的功能是:將N×N矩陣中元素的值按列右移1個(gè)位置,右邊被移出矩陣的元素繞回左邊。
例如,N=3,有下列矩陣:
1 2 3
4 5 6
7 8 9
計(jì)算結(jié)果為:
3 1 2
6 4 5
9 7 8
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#define N 3
void fun(int (*a)[N])
{
int I,j,t;
for(i=0;i<___1___;i++)
{
t=a[i][___2___];
for(j=N-1;j>0;j--)
a[i][j]=a[i][j-1];
___3___=t;
}
}
main()
{
int I,j,a[][N]={1,2,3,4,5,6,7,8,9};
printf("The original array:\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%2d ",a[i][j]);
printf("\n");
}
fun(a);
printf("\nThe result is:\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%2d ",a[i][j]);
printf("\n");
}
}
【答案】
(1)N (2)N-1 (3)a[i][0]
2.程序修改題
在給定程序中:函數(shù)fun的功能是:把在字符串s中出現(xiàn)的每個(gè)字符,緊隨其后重復(fù)出現(xiàn)一次,形成一個(gè)新串放在t中,t中字符按原字符串中字符順序排列。
例如:當(dāng)s中的字符串為:"ABAABBCCDDEE",則t中的字符串應(yīng)為:"AABBAAAABBBBCCCCDDDDEEEE"。
請改正函數(shù)fun中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
/**********found**********/
void fun(char s,char t)
{
int I,sl;
sl=strlen(s);
for(i=0; i<sl; i++)
{ t[2*i]=s[i]; t[2*i+1]=s[i]; }
/**********found**********/
t[2*sl]='0';
}
main()
{
char s[100],t[100];
system("cls");
printf("\nPlease enter string s:");
scanf("%s",s);
fun(s,t);
printf("The result is: %s\n",t);
}
【答案】
(1)將void fun (char s,char t) 改為:
void fun (char *s,char *t)
(2)將t[2*sl] = '0'; 改為:t[2*sl] = '\0';?
3.程序設(shè)計(jì)題
函數(shù)fun的功能是:從三個(gè)形參x、y、z中找出中間的那個(gè)數(shù),作為函數(shù)值返回。
例如,當(dāng)x=123,y=453,z=334時(shí),中間的數(shù)為334。
注意:部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun()的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
int fun(int a,int b,int c)
{
?
}
main()
{
int x=123,y=453,z=334,mid;
mid=fun(x,y,z);
printf("\nThe middle numberis:%d\n",mid);
}
【答案】
int temp;
temp=(a>b)?(b>c?b:(a>c?c:a)):((a>c)? a:((b>c)?c:b));
return temp;
1.程序填空題
給定程序中,函數(shù)fun的功能是:在形參s所指字符串中的每個(gè)非數(shù)字字符之后插入一個(gè)"#"號(hào)。
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
void fun(char *str)
{
int I,j,n;
for(i=0;str[i]!='\0';i++)
if(str[i]>='a'___1___str[i]<='z')
{
n=0;
while(str[i+1+n]!=___2___)
n++;
for(j=i+n+1;j>I;j--)
str[j+1]=___3___;
str[j+1]='#';
i=i+1;
}
}
main()
{
char str[100]="a1bd45sdtg56f";
printf("\nThe original string is : %s\n",str);
fun(str);
printf("\nThe result is : %s\n",str);
}
【答案】
(1)&& (2)0 (3)str[j]?
2.程序修改題
給定程序中函數(shù)fun的功能是:把在字符串s中出現(xiàn)的每個(gè)字符,緊隨其后重復(fù)出現(xiàn)一次,放在一個(gè)新串t中,t中字符按原字符串中逆排列。
例如:當(dāng)s中的字符串為:“ABCDE”時(shí),則t中的字符串應(yīng)為:“EEDDCCBBAA”。
請改正函數(shù)fun中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
void fun(char *s,char *t)
{
int I,sl;
sl=strlen(s);
/**********found**********/
for(i=1;i<sl;i++)
{
t[2*i]=s[sl-i-1];
t[2*I +1]=s[sl-i-1];
}
/**********found**********/
t[2*sl]='0/';
}
main()
{
char s[100],t[100];
system("cls");
printf("Please enter string s:");
scanf("%s",s);
fun(s,t);
printf("The result is: %s\n",t);
}
【答案】
(1)將for (i=1; i<sl; i++) 改為:
for (i=0; i<sl; i++)
(2)將t[2*sl] = '0/'; 改為:t[2*sl] = '\0';?
3.程序設(shè)計(jì)題
請編寫函數(shù)fun,它的功能是計(jì)算:
F作為函數(shù)值返回。
在C語言中可調(diào)用log(n)函數(shù)求ln(n)。log函數(shù)的引用說明是:double log(double x)。
注意:部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun()的花括號(hào)中填入所編寫的若干語句。
#include <conio.h>
#include <math.h>
#include <stdio.h>
double fun(int n)
{
?
}
main()
{
int I;
FILE *out;
printf("%f\n",fun(10));
out=fopen("outfile.dat","w");
for(i=0;i<10;i++)
fprintf(out,"%f\n",fun(i+15));
fclose(out);
}
【答案】
int i;
double f=0.0,log(double x);
for(i=1;i<=n;i++)
f=f+log(i);
f=sqrt(f);
return f;
1.程序填空題
下列給定的程序中,fun()函數(shù)的功能是:將p所指字符串中每個(gè)單詞的最后一個(gè)字母改成大寫(這里的“單詞”是指有空格隔開的字符串)。
例如,若輸入:I am a student to take the examin- ation,則應(yīng)輸出:I aM A studenT tO takE thE examin- atioN
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
void fun(char *p)
{
int k=0;
for( ; *p; p++)
if(k)
{
if(*p==' ')
{
/**********found**********/
___1___;
/**********found**********/
___2___=toupper(*(p-1));
}
}
else k=1;
}
main()
{
char chrstr[64];
int d;
printf("Please enter an English sentence
within 63 letters: ");
gets(chrstr);
d=strlen(chrstr);
chrstr[d]=' ';
chrstr[d+1]=0;
printf("\nBofore changing:\n %s",chrstr);
/**********found**********/
___3___;
printf("After changing:\n %s",chrstr);
}
【答案】
(1)k=0 (2)*(p-1) (3)fun(chrstr)
2.程序修改題
給定程序中,函數(shù)fun的功能是:將在字符串s中下標(biāo)為偶數(shù)位置上的字符,緊隨其后重復(fù)出現(xiàn)一次,放在一個(gè)新串t中,t中字符按原字符串中字符的順序排列(注意0為偶數(shù))。
例如,當(dāng)s中的字符串為“ABCDE”時(shí),則t中的字符串應(yīng)為“AACCEE”。
請改正程序中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
void fun(char *s,char *t)
{
int I,j,sl;
sl=strlen(s);
/**********found**********/
for(i=0,j=0; i<sl; i++)
{
t[2*j]=s[i];
t[2*j+1]=s[i];
j++;
}
/**********found**********/
t[2*sl]='\0';
}
main()
{
char s[100],t[100];
system("cls");
printf("Please enter string s:");
scanf("%s",s);
fun(s,t);
printf("The result is: %s\n",t);
}
【答案】
(1)將for (i=0,j=0; i<sl; i++) 改為:
for (i=0,j=0; i<sl; i+=2)
(2)將t[2*sl] = '\0'; 改為:t[2*j]='\0';
3.程序設(shè)計(jì)題
請編寫函數(shù)fun(),它的功能是求Fibonacci數(shù)列中小于t的最大的一個(gè)數(shù),結(jié)果由函數(shù)返回。其中Fibonacci數(shù)列F(n)的定義為:
F(0)=0,F(xiàn)(1)=1 F(n)=F(n-1)+F(n-2)
例如:t=1000時(shí),函數(shù)值為987。
注意;部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <conio.h>
#include <math.h>
#include <windows.h>
#include <stdio.h>
int fun(int t)
{
?
}
main()
{
int n;
system("cls");
n=1000;
printf("n=%d, f=%d\n",n,fun(n));
}
【答案】
int fun(int t)
{
int a=1,b=1,c=0;
do
{
c=a+b; a=b; b=c;
}while(c<t);
c=a;
return c;
}
1.程序填空題
給定程序中,函數(shù)的功能是計(jì)算并輸出high以內(nèi)最大的10個(gè)素?cái)?shù)之和。High由主函數(shù)傳給fun函數(shù)。若high的值為100,則函數(shù)的值為732。
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <math.h>
int fun(int high)
{ int sum=0,n=0,j,yes;
/**********found**********/
while((high>=2)&&___1___)
{
yes=1;
for(j=2; j<=high/2; j++ )
if(high%j==0)
{
/**********found**********/
yes=0;___2___;
}
if(yes) { sum+=high; n++; }
/**********found**********/
___3___;
}
return sum ;
}
main()
{
printf("%d\n",fun(100));
}
【答案】
(1)(n<10) (2)break (3)high--
2.程序修改題
給定程序中函數(shù)fun的功能是:將在字符串s中下標(biāo)為奇數(shù)位置上的字符,緊隨其后重復(fù)出現(xiàn)一次,放在一個(gè)新串t中,t中字符按原字符串中字符的順序排列(注意0為偶數(shù))。
例如,當(dāng)s中的字符串為“ABCDEF”時(shí),則t中的字符串應(yīng)為“BBDDFF”。
請改正函數(shù)fun中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
void fun(char *s,char *t)
{
int I,j,sl;
sl=strlen(s);
/**********found**********/
for(i=0,j=0; i<sl; i+=2)
{
t[2*j]=s[i];
t[2*j +1]=s[i];
/**********found**********/
j--;
}
t[2*j]='\0';
}
main()
{
char s[100],t[100];
system("cls");
printf("Please enter string s:");
scanf("%s",s);
fun(s,t);
printf("The result is: %s\n",t);
}
【答案】
(1)將 for(i=0,j=0; i<sl; i+=2) 改為:
for (i=1,j=0; i<sl; i+=2)
(2)將j--; 改為:j++;?
3.程序設(shè)計(jì)題
函數(shù)fun的功能是:
Chapter_7
統(tǒng)計(jì)在字符串a(chǎn)中,以下指定字符出現(xiàn)的次數(shù),并存到b數(shù)組中,其中:字符'+'、'-'、'*'、'/'、'&'出現(xiàn)的次數(shù)分別存放到b[0]、b[1]、b[2]、b[3]、b[4]中,其他字符出現(xiàn)的次數(shù)存放到b[5]中。
例如,當(dāng)a字符串中的字符串為:"+-12+*1/-a/&b"時(shí),調(diào)用該函數(shù)后,b中存放的數(shù)據(jù)應(yīng)為:2、2、1、2、1、5。
注意:部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
#include <string.h>
void fun(char *a,int b[])
{
?
}
main()
{
int I,b[6];
char a[100]="+-12+*1/-a/&b";
fun(a,b);
printf("The result is: ");
for(i=0; i<6; i++) printf("%d ",b[i]);
printf("\n");
}
【答案】
void fun(char *a,int b[])
{
int i;
for(i=0; i<6; i++) b[i] = 0;
for(i=0; i< strlen(a); i++)
switch(a[i])
{
case '+': b[0]++; break;
case '-': b[1]++; break;
case '*': b[2]++; break;
case '/': b[3]++; break;
case '&': b[4]++; break;
default: b[5]++;
}
}
1.程序填空題
給定程序的功能是根據(jù)如下公式求P的值,結(jié)果由函數(shù)值帶回。M與n為兩個(gè)正整數(shù)且要求m>n:
?
例如:m=11,n=4時(shí),運(yùn)行結(jié)果為330.000000。
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
long jc(int m)
{
long s=1;
int I ;
/**********found**********/
for(i=1;i<=m;i++)___1___;
return s;
}
float fun(int m,int n)
{
float p;
p=1.0*jc(m)/jc(n)/jc(m-n);
/**********found**********/
___2___;
}
main()
{
printf("P=%f\n",fun (11,4));
}
【答案】
(1)s=s*i或s*=i
(2)return p;
2.程序修改題
給定程序中函數(shù)fun的功能是:將在字符串s中下標(biāo)為偶數(shù)位置上的字符,緊隨其后重復(fù)出現(xiàn)一次,放在一個(gè)新串t中,t中字符按原字符串中字符出現(xiàn)的逆序排列(注意0為偶數(shù))。
例如,當(dāng)s中的字符串為“ABCDEF”時(shí),則t中的字符串應(yīng)為“EECCAA”。
請改正函數(shù)fun中的錯(cuò)誤,使它能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
void fun(char *s,char *t)
{
int I,j,sl;
sl=strlen(s);
if(sl%2) sl--;
else sl-=2;
/**********found**********/
for(i=sl,j=0; i>=0; i--)
{ t[2*j]=s[i]; t[2*j+1]=s[i]; j++; }
/**********found**********/
t[2*sl]='\0';
}
main()
{
char s[100],t[100];
system("cls");
printf("\nPlease enter string s:");
scanf("%s",s);
fun(s,t);
printf("The result is: %s\n",t);
}
【答案】
(1)將for (i=sl,j=0; i>=0; i--) 改為:
for (i=sl,j=0; i>=0; i-=2)
(2)將t[2*sl] = '\0'; 改為:t[2*j] = '\0';
3.程序設(shè)計(jì)題
在給定程序中,函數(shù)fun的功能是:把a(bǔ)數(shù)組中的n個(gè)數(shù)和b數(shù)組中逆序的n個(gè)數(shù)一一對應(yīng)相乘,結(jié)果存在c數(shù)組中。
例如:當(dāng)a數(shù)組中的值是:1、3、5、7、9,b數(shù)組中的值是:2、3、4、5、6,調(diào)用該函數(shù)后c中存放的數(shù)據(jù)是:6、15、20、21、18
注意:部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
void fun(int a[],int b[],int c[],int n)
{
?
}
main()
{
int I,a[100]={1,3,5,7,9},
b[100]={2,3,4,5,6},c[100];
fun(a,b,c,5);
printf("The result is: ");
for(i=0;i<5;i++)
printf("%d",c[i]);
printf("\n");
}
【答案】
void fun(int a[],int b[],int c[],int n)
{
int i;
for(i=0; i<n; i++)
c[i]=a[i]*b[n-1-i];
}
1.程序填空題
給定程序中,函數(shù)fun的功能是:計(jì)算數(shù)組元素中值為正數(shù)的平均值(不包括0)。在主函數(shù)中從鍵盤輸入若干個(gè)數(shù)放入數(shù)組中,輸入0結(jié)束輸入并放在最后一個(gè)元素中。
例如:數(shù)組中元素中的值依次為:39、-47、21、2、-8、15、0,則程序的運(yùn)行結(jié)果為:19.250000。
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
double fun(int x[])
{
/**********found**********/
___1___;
int c=0,i=0;
while (x[i]!=0)
{
if(x[i]>0) { sum+=x[i]; c++; }
i++;
}
/**********found**********/
___2___;
return sum;
}
main()
{
int x[1000]; int i=0;
printf("Please enter some data(end with 0):");
do
{ scanf("%d",&x[i]); }
while(x[i++]!=0);
printf("%f\n",fun(x));
}
【答案】
(1)double sum = 0.0; (2)sum /= c;?
2.程序修改題
給定程序中函數(shù)fun的功能是:將在字符串s中下標(biāo)為奇數(shù)位置上的字符,緊隨其后重復(fù)出現(xiàn)一次,放在一個(gè)新串t中,t中字符按原字符串中字符出現(xiàn)的逆序排列(注意0為偶數(shù))。
例如:當(dāng)s中的字符串為“ABCDEFG”時(shí),則t中的字符串應(yīng)為“FFDDBB”。
請改正函數(shù)fun中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
void fun(char *s,char *t)
{
int I,j,sl;
sl=strlen(s);
/**********found**********/
if(sl%2) Sl-=2;
else Sl--;
for(i=sl,j=0; i>=0; i-=2)
{ t[2*j]=s[i]; t[2*j+1]=s[i]; j++; }
/**********found**********/
t[2*sl]='0';
}
main()
{
char s[100],t[100];
system("cls");
printf("Please enter string s:");
scanf("%s",s);
fun(s,t);
printf("The result is: %s\n",t);
}
【答案】
(1)將if(sl%2) Sl-=2; 改為:
if(sl%2) sl-=2;
(2)將else Sl--;改為:else sl--;
(2)將t[2*sl] = '0'; 改為:t[2*j] = '\0';
3.程序設(shè)計(jì)題
程序定義了N×N的二維數(shù)組,并在主函數(shù)中自動(dòng)賦值。請編寫函數(shù)fun(),該函數(shù)的功能是:給數(shù)組周邊元素置0。例如:a數(shù)組中的值為
0 1 2 7 9
1 9 7 4 5
0 3 8 3 1
4 5 6 8 2
1 9 1 4 1
則返回主程序后a數(shù)組中的值應(yīng)為
0 0 0 0 0
0 9 7 4 0
0 3 8 3 0
0 5 6 8 0
0 0 0 0 0
注意:部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
#include <stdlib.h>
#define N 5
fun(int b[][N])
{
?
}
main()
{
int a[N][N],I,j;
printf("***** The array *****\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
a[i][j]=rand()%10;
printf("%4d",a[i][j]);
}
printf("\n");
}
fun(a);
printf("*** THE RESULT ***\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++) printf("%4d",a[i][j]);
printf("\n");
}
}
【答案】
fun(int b[][N])
{
int i;
for(i=0;i<N;i++)
{
b[0][i]=0;
b[N-1][i]=0;
b[i][0]=0;
b[i][N-1]=0;
}
}
1.程序填空題
請補(bǔ)充fun函數(shù),該函數(shù)的功能是:依次取出字符串中所有大寫字母,形成新的字符串,并取代原字符串。
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <conio.h>
void fun(char *s)
{
int j=0;
char *p=s;
while(___1___)
{
if(*p>='A' && *p<='Z')
{
s[j]=*p;
___2___;
}
p++;
}
s[j]=___3___;
}
main()
{
char str[100];
printf("\nPlease Input a string :");
gets(str);
printf("\n\nThe original string is : %s\n",str);
fun(str);
printf("\n\nThe string of changing is : %s\n",str);
}
【答案】
(1)*p (2)j++ (3)'\0'
2.程序修改題
在給定程序中,函數(shù)fun()的功能是:求廣義Fibonacci級數(shù)的第n項(xiàng)。1,1,1,3,5,9,17,31,…… 項(xiàng)值通過函數(shù)值返回主函數(shù)。
例如,若n = 15,則應(yīng)輸出:2209。
請改正程序中的語法錯(cuò)誤,使其能計(jì)算出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
long fun(int n)
{
long a=1,b=1,c=1,d=0,k;
/**********found**********/
for(k=4;k<=n;k++)
{ d=a+b+c;
/**********found**********/
a=b; b=c; c=d;
}
return d;
}
main()
{
int n=15;
printf("The value is: %ld\n",fun(n));
}
【答案】
(1)將for (k=0; k<=n; k++) 改為:
for (k=4; k<=n; k++)
(2)將a=b; b=c; 改為:a=b; b=c; c=d;
3.程序設(shè)計(jì)題
請編寫函數(shù)fun(),其功能是:判斷字符串是否為回文?若是,函數(shù)返回1,主函數(shù)中輸出:YES,否則返回0,主函數(shù)中輸出NO?;匚氖侵疙樧x和倒讀都一樣的字符串。
例如,字符串LEVEL是回文,而字符串120021就不是回文。
注意:部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
#define N 80
int fun(char *str)
{
?
}
main()
{
char s[N];
printf("Enter a string: "); gets(s);
printf("\n\n"); puts(s);
if(fun(s)) printf(" YES\n");
else printf("NO\n");
}
【答案】
int fun(char *str)
{
int i,j=strlen(str);
for(i=0; i<j/2; i++)
if(str[i]!=str[j-i-1]) return 0;
return 1;
}
1.程序填空題
在給定程序中,函數(shù)fun()的功能是將帶頭節(jié)點(diǎn)的單向鏈表結(jié)點(diǎn)數(shù)據(jù)域中的數(shù)據(jù)從小到大排序。即若原鏈表結(jié)點(diǎn)數(shù)據(jù)域從頭至尾的數(shù)據(jù)為:2、10、4、0、8、6,排序后鏈表結(jié)點(diǎn)數(shù)據(jù)域從頭至尾的數(shù)據(jù)為:0、2、4、6、8、10。
請?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <stdlib.h>
#define N 6
typedef struct node
{int data;
struct node *next;
}NODE;
void fun(NODE *h)
{
NODE *p,*q;
int t;
/**********found**********/
___1___;
while(p)
{
q=p->next
while(q)
{
/**********found**********/
if(___2___)
{
t=p->data;
p->data=q->data;
q->data=t;
}
q=q->next;
}
p=p->next;
}
}
NODE *creatlist(int a[])
{
NODE *h,*p,*q;
int I;
h=(NODE*)malloc(sizeof(NODE));
h->next=NULL;
for(i=0;i<N;i++)
{
q=(NODE*)malloc(sizeof(NODE));
q->data=a[i];
q->next=NULL;
if(h->next==NULL) h->next=p=q;
else { p->next=q; p= q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p=h->next;
if(p==NULL) printf("The list is NULL!\n");
else
{ printf("\nHead ");
do
{ printf("->%d",p->data);
p=p->next;
}while(p!=NULL);
printf("->End\n");
}
}
main()
{
NODE *head;
int a[N]={0,10,4,2,8,6 };
head=creatlist(a);
printf("The original list:\n");
outlist(head);
fun(head);
printf("The list after sorting :\n");
outlist(head);
}
【答案】
(1)p = h->next
(2)p->data >= q->data
2.程序修改題
在給定程序中,函數(shù)fun()的功能是:將在字符串s中出現(xiàn)、而未在字符串t中出現(xiàn)的字符形成一個(gè)新的字符串放在u中,u中字符按原字符串中字符順序排列,不去掉重復(fù)字符。
例如:當(dāng)s =“AABCDE”,t =“BDFG”字符。U中的字符串為“AACE”。
請改正函數(shù)fun中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
/**********found**********/
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++)
if(s[i]==t[j]) break;
/**********found**********/
if(j>tl)
*u++=s[i];
}
*u='\0';
}
main()
{
char s[100],t[100],u[100];
system("cls");
printf("Please enter string s:");
scanf("%s",s);
printf("Please enter string t:");
scanf("%s",t);
fun(s,t,u);
printf("the result is: %s\n",u);
}
【答案】
(1)將void fun (char *s,char *t,char u)
改為:void fun (char *s,char *t,char *u)
(2)將if (j>tl) 改為:if (j>=tl)
3.程序設(shè)計(jì)題
給定程序的功能是求1/4的圓周長,函數(shù)通過形參得到圓的直徑,函數(shù)返回1/4的圓周長(圓周長公式為:L=πd,在程序中定義的變量名要與公式的變量相同)。
注意:部分源程序已給出。請勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun()的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
double fun(double d)
{
?
}
main()
{
double z;
printf("Input the d of the round : ");
scanf("%lf",&z);
printf(" L=%lf\n ",fun(z));
}
【答案】
return 3.14159*d/4.0;