本文主要是介绍[C语言面试笔试]2023精选改错题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
改 错 题
1、给定程序中fun函数的功能是:根据整型形参m的值,计算如下公式的值:
例如,若m中的值为5,则应输出0.536389。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
double fun ( int m )
{ double y = 1.0 ;
int i ;
/**************found**************/
for(i = 2 ; i < m ; i++) 改为:for(i = 2 ; i <= m ; i++)
/**************found**************/
y -= 1 /(i * i) ; 改为:y -= 1.0 /(i * i) ;
return( y ) ;
}
main( )
{ int n = 5 ;
printf( "\nThe result is %lf\n", fun ( n ) ) ;
}啊啊
2、给定程序中fun函数的功能是:将s所指字符串的正序和反序进行连接,形成一个新串放在t所指的数组中。
例如,当s所指字符串为“ABCD”时,则t所指字符串的内容应为“ABCDDCBA”
#include <stdio.h>
#include <string.h>
/************found************/
void fun (char s, char t) 改为:void fun (char * s, char * t)
{ int i, d;
d = strlen(s);
for (i = 0; i<d; i++) t[i] = s[i];
for (i = 0; i<d; i++) t[d+i] = s[d-1-i];
/************found************/
t[2*d-1] = '\0'; 改为:t[2*d] = '\0';
}
main()
{ char s[100], t[100];
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t); }
3、给定程序中fun函数的功能是:将s所指字符串中位于奇数位置的字符或ASCII码为偶数的字符放入t所指数组中(规定第一个字符放在第0位中)。
例如:字符串中的数据为:AABBCCDDEEFF,则应输出ABBCDDEFF。
#include <stdio.h>
#include <string.h>
#define N 80
void fun(char *s, char t[])
{ int i, j=0;
for(i=0; i<strlen(s); i++)
/***********found**********/
if(i%2 && s[i]%2==0) 改为:if(i%2= =0 || s[i]%2= =0)
t[j++]=s[i];
/***********found**********/
t[i]='\0'; 改为:t[j]='\0';
}
main()
{ char s[N], t[N];
printf("\nPlease enter string s : "); gets(s);
fun(s, t);
printf("\nThe result is : %s\n",t);
}
4、给定程序中fun函数的功能是:计算n!。例如,给n输入5,则输出120.000000。
#include <stdio.h>
double fun ( int n )
{ double result = 1.0 ;
/************found************/
if n = = 0 改为:if (n == 0)
return 1.0 ;
while( n >1 && n < 170 )
/************found************/
result *= n-- 改为:result *= n--;
return result ;
}
main ( )
{ int n ;
printf("Input N:") ;
scanf("%d", &n) ;
printf("\n\n%d! =%lf\n\n", n, fun(n)) ;
}
5、给定程序中fun函数的功能是:先从键盘上输入一个3行3列的矩阵的各个元素的值,然后输出主对角线元素之和。
#include <stdio.h>
int fun()
{ int a[3][3],sum;
int i,j;
/*********found**********/
______; 改为:sum=0
for (i=0;i<3;i++)
{ for (j=0;j<3;j++)
/*********found**********/
scanf("%d",a[i][j]); 改为:scanf("%d",&a[i][j]);
}
for (i=0;i<3;i++)
sum=sum+a[i][i];
printf("Sum=%d\n",sum);
}
main()
{ fun(); }
6、给定程序中fun函数的功能是:求 ,(此处aa…aa表示n个a,a和n的值在1至9之间)。例如,a=3,n=6,则以上表达式为:
s=333333-33333-3333-333-33-3,其值是:296298。a和n是fun函数的形参。
#include <stdio.h>
long fun (int a, int n)
{ int j ;
/**************found**************/
long s = 0, t = 1 ; 改为:long s = 0, t = 0 ;
/**************found**************/
for ( j = 0 ; j <=n ; j++) 改为:for ( j = 0 ; j <n ; j++)
t = t * 10 + a ;
s = t ;
for ( j = 1 ; j < n ; j++) {
/**************found**************/
t = t % 10 ; 改为:t = t / 10 ;
s = s - t ; }
return(s) ;
}
main( )
{ int a, n ;
printf( "\nPlease enter a and n:") ;
scanf( "%d%d", &a, &n ) ;
printf( "The value of function is: %ld\n", fun ( a, n ) );
}
7、给定程序中fun函数的功能是:求k!(k<13),所求阶乘的值作为函数值返回。例如:若k=10,则应输出3628800。
#include <stdio.h>
long fun ( int k)
{
/************found************/
if k > 0 改为:if(k > 0)
return (k*fun(k-1));
/************found************/
else if ( k=0 ) 改为:else if ( k==0 )
return 1L;
}
main()
{ int k = 10 ;
printf("%d!=%ld\n", k, fun ( k )) ; }
8、给定程序中fun函数的功能是:由形参给定n个实数,输出平均值,并统计在平均值以上(含平均值)的实数个数。
例如,n=8时,输入:193.99,195.673,195.757,196.051,196.092,196.596,196.579,196.763,所得平均值为:195.838745,在平均值以上的实数个数应为:5。
#include <stdio.h>
int fun(float x[], int n)
{
/************found************/
int j, c=0, float xa=0.0; 改为:int j, c=0; float xa=0.0;
for (j=0; j<n; j++ )
xa += x[j]/n;
printf("ave =%f\n",xa);
for (j=0; j<n; j++ )
/************found************/
if (x[j] => xa) 改为:if (x[j] >= xa)
c++;
return c;
}
main ( )
{ float x[100] = { 193.199, 195.673, 195.757, 196.051,
196.092, 196.596, 196.579, 196.763 };
printf("%d\n", fun (x, 8)); }
9、给定程序中fun函数的功能是:将tt所指字符串中的小写字母都改为对应的大写字母,其它字符不变。例如:若输入“Ab,cD”,则输出“AB,CD”
#include <stdio.h>
#include <string.h>
char* fun( char tt[] )
{ int i;
for( i = 0; tt[i]; i++ )
/**********found***********/
if(( 'a' <= tt[i] )||( tt[i] <= 'z' ) ) 改为:if((tt[i] >= 'a')&&( tt[i] <= 'z' ) )
/**********found***********/
tt[i] += 32; 改为:tt[i] - = 32;
return( tt );
}
main( )
{ char tt[81];
printf( "\nPlease enter a string: " );
gets( tt );
printf( "\nThe result string is:\n%s", fun( tt ) ); }
10、给定程序中fun函数的功能是:在p所指字符串中找出ASCII码值最大的字符,将其放在第一个位置上;并将该字符前的原字符向后顺序移动。例如,调用fun函数这前给字符串输入:ABCDeFGH,调用后字符串中的内容为:eABCDFGH。
#include <stdio.h>
/**********found**********/
fun( char *p ) 改为:void fun( char *p )
{ char max,*q; int i=0;
max=p[i];
while( p[i]!=0 )
{ if( max<p[i] )
{ max=p[i];
/**********found**********/
q=p+I 改为:q=p+i;
}
i++; }
/**********found**********/
wihle( q>p ) 改为:while(q>p )
{ *q=*(q-1);
q--; }
p[0]=max;
}
main()
{ char str[80];
printf("Enter a string: "); gets(str);
printf("\nThe original string: "); puts(str);
fun(str);
printf("\nThe string after moving: "); puts(str); printf("\n\n"); }
11、给定程序MODI1.CJ是建立一个带头结点的单向链表,并用随机函数为各结点赋值。给定程序中fun函数的功能是将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并作为函数值返回。
#include <stdio.h>
#include <stdlib.h>
typedef struct aa
{ int data; struct aa *next; }NODE;
int fun(NODE *h)
{ int sum = 0 ;
NODE *p;
/***********found**********/
p=h; 改为:p=h->next;
/***********found**********/
while(p->next) 改为:while(p)
{ if(p->data%2==0) sum +=p->data;
/***********found**********/
p=h->next; 改为:p=p->next;
}
return sum;
}
NODE *creatlink(int n)
{ NODE *h, *p, *s, *q;
int i, x;
h=p=(NODE *)malloc(sizeof(NODE));
for(i=1; i<=n; i++)
{ s=(NODE *)malloc(sizeof(NODE));
s->data=rand()%16; s->next=p->next;
p->next=s; p=p->next; }
p->next=NULL;
return h;
}
outlink(NODE *h, FILE *pf)
{ NODE *p;
p = h->next;
fprintf(pf ,"\n\nTHE LIST :\n\n HEAD " );
while(p)
{ fprintf(pf ,"->%d ",p->data ); p=p->next; }
fprintf (pf,"\n");
}
outresult(int s, FILE *pf)
{ fprintf(pf,"\nThe sum of even numbers : %d\n",s);}
main( )
{ NODE *head; int even;
head=creatlink(12);
head->data=9000;
outlink(head , stdout);
even=fun(head);
printf("\nThe result :\n"); outresult(even, stdout);
}
12、给定程序中fun函数的功能是:求三个数的最小公倍数。例如,给主函数中的变量x1,x2,x3分别输入15 11 2,则输出结果应当是:330。
#include <stdio.h>
/************found************/
fun(int x, y, z ) 改为:fun(int x, int y, int z )
{ int j,t ,n ,m;
/************found************/
j = 1 ; 改为:j = x;
t=j%x; m=j%y ; n=j%z;
/************found************/
while(t!=0&&m!=0&&n!=0) 改为:while(t!=0||m!=0||n!=0)
{ j = j+1; t=j%x; m=j%y; n=j%z; }
/************found************/
return i; 改为:return j;
}
main( )
{ int x1,x2,x3,j ;
printf("Input x1 x2 x3: "); scanf("%d%d%d",&x1,&x2,&x3);
printf("x1=%d, x2=%d, x3=%d \n",x1,x2,x3);
j=fun(x1,x2,x3);
printf("The minimal common multiple is : %d\n",j); }
13、给定程序中fun函数的功能是:先将在字符串s中的字符按正序存放到t串中,然后把s中的字符按逆序连接到t串的后面。例如:当s听字符串为“ABCDE”时,则t中的字符串应为“ABCDEEDCBA”。
#include <stdio.h>
#include <string.h>
void fun (char *s, char *t)
{ int i, sl;
sl = strlen(s);
/************found************/
for( i=0; i<=s1; i ++) 改为:for( i=0; i< sl; i ++)
t[i] = s[i];
for (i=0; i<sl; i++)
t[sl+i] = s[sl-i-1];
/************found************/
t[sl] = '\0'; 改为:t[2*sl] = '\0';
}
main()
{ char s[100], t[100];
printf("\nPlease enter string s:"); scanf("%s", s);
fun(s, t);
printf("The result is: %s\n", t);
}
14、给定程序中fun函数的功能是:从s所指字符串中删除所有小写字母c。
#include <stdio.h>
void fun( char *s )
{ int i,j;
for(i=j=0; s[i]!='\0'; i++)
if(s[i]!='c')
/************found************/
s[j]=s[i]; 改为:s[j++]=s[i];
/************found************/
s[i]='\0'; 改为:s[j]='\0';
}
main()
{ char s[80];
printf("Enter a string: "); gets(s);
printf("The original string: "); puts(s);
fun(s);
printf("The string after deleted : "); puts(s);printf("\n\n");
}
15、给定程序中fun函数的功能是:从整数1到55之间,选出能被3整除、且有一位上的数是5的那些数,并把这些数放在b所指的数组中,这些数的个数作为函数值返回。规定,函数中a1放个位数,a2放十位数。
#include <stdio.h>
/************found************/
fun( int *b ) 改为:int fun( int *b )
{ int k,a1,a2,i=0;
/************found************/
for(k=10; k<=55; k++) { 改为:for(k=10; k<55; k++) {
/************found************/
a2=k/1O; 改为:a2=k/10;
a1=k-a2*10;
if((k%3==0 && a2==5)||(k%3==0 && a1==5))
{ b[i]=k; i++; }
}
/************found************/
return k; 改为:return i;
}
main( )
{ int a[100],k,m;
m=fun( a );
printf("The result is :\n");
for(k=0; k<m; k++) printf("%4d",a[k]); printf("\n");
}
16、给定程序中fun函数的功能是:求S的值。
例如,当k为10时,函数值应为:1.533852。
#include <stdio.h>
#include <math.h>
/************found************/
void fun( int k ) 改为:float fun( int k )
{ int n; float s, w, p, q;
n = 1;
s = 1.0;
while ( n <= k )
{ w = 2.0 * n;
p = w - 1.0;
q = w + 1.0;
s = s * w *w/p/q;
n++;
}
/************found************/
return s 改为:return s;
}
main ( )
{ printf("%f\n", fun (10)); }
17、给定程序中fun函数的功能是:将十进制正整数m转换成k(2<=k<=9)进制数,并按高位到低位顺序输出。
例如,若输入8和2,则应输出1000(即十进制数8转换成二进制表示是1000)。
#include <conio.h>
#include <stdio.h>
/**********found**********/
fun( int m, int k ) 改为:void fun( int m, int k )
{ int aa[20], i;
for( i = 0; m; i++ )
{
/**********found**********/
aa[i] = m/k; 改为:aa[i] = m% k;
m /= k;
}
for( ; i; i-- )
/**********found**********/
printf( "%d", aa[ i ] ); 改为:printf( "%d", aa[ i-1] );
}
main( )
{ int b, n;
printf( "\nPlease enter a number and a base:\n" ); scanf( "%d %d", &n, &b );
fun( n, b ); printf("\n"); }
18、给定程序中fun函数的功能是:根据整型形参n,计算如下公式的值。
例如:若n=10,则应输出0.617977.
#include <stdio.h>
/************found************/
int fun ( int n ) 改为:float fun ( int n )
{ float A=1; int i;
/************found************/
for (i=2; i<n; i++) 改为:for (i=2; i<=n; i++)
A = 1.0/(1+A);
return A ; }
main( )
{ int n ;
printf("\nPlease enter n: ") ;
scanf("%d", &n ) ; printf("A%d=%f\n", n, fun(n) ) ; }
19、给定程序中fun函数的功能是:输出M行M列整数方阵,然后求两条对角线上元素之和,返回此和数。
#include <stdio.h>
#define M 5
/************found************/
int fun(int n, int xx[][]) 改为:int fun(int n, int xx[M][M])
{ int i, j, sum=0;
printf( "\nThe %d x %d matrix:\n", M, M );
for( i = 0; i < M; i++ )
{ for( j = 0; j < M; j++ )
/************found************/
printf( "%f ", xx[i][j] ); 改为:printf( "%4d ", xx[i][j] );
printf("\n"); }
for( i = 0 ; i < n ; i++ )
sum += xx[i][i]+xx[i][ n-i-1 ];
return( sum );
}
main( )
{ int aa[M][M]={{1,2,3,4,5},{4,3,2,1,0}, {6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7}};
printf ( "\nThe sum of all elements on 2 diagnals is %d.",fun( M, aa )); }
20、给定程序中fun函数的功能是:求出s所指字符串中最后一次出现的t所指子字符串的地址,通过函数值返回,在主函数中从此地址开始的字符串;若未打到,则函数值为NULL。
例如:当字符串中的内容为:“abcdabfabcdx”,t中的内容为“ab”时,输出结果为:abcdx。当字符串中的内容为:“abcdabfabcdx”,t中的内容为“abd”时,则程序输出未打到住处:not be found!
#include <stdio.h>
#include <string.h>
char * fun (char *s, char *t )
{ char *p , *r, *a;
/************found************/
a = Null; 改为:a = NULL;
while ( *s )
{ p = s; r = t;
while ( *r )
/************found************/
if ( r == p ) 改为:if ( r* ==* p )
{ r++; p++; }
else break;
if ( *r == '\0' ) a = s;
s++; }
return a ;
}
main()
{ char s[100], t[100], *p;
printf("\nPlease enter string S :"); scanf("%s", s );
printf("\nPlease enter substring t :"); scanf("%s", t );
p = fun( s, t );
if ( p ) printf("\nThe result is : %s\n", p);
else printf("\nNot found !\n" );
}
21、给定程序中fun函数的功能是:统计一个无符号整数中各位数字值为零的个数,通过形参传回主函数,并把该整数中各位上最大的数字值作为函数值返回。例如,若输入无符号整数30800,则数字值为零的个数为3,各位上数字值最大的是8。
#include <stdio.h>
int fun(unsigned n, int *zero)
{ int count=0,max=0,t;
do
{ t=n%10;
/**************found**************/
if(t=0) 改为:if(t==0)
count++;
if(max<t) max=t;
n=n/10;
}while(n);
/**************found**************/
zero=count; 改为:* zero=count;
return max;
}
main( )
{ unsigned n; int zero,max;
printf("\nInput n(unsigned): "); scanf("%d",&n);
max = fun( n,&zero );
printf("\nThe result: max=%d zero=%d\n",max,zero);
}
22、给定程序中fun函数的功能是:求整数x的y次方的低3位值。例如,整数5的6次方为15625,此值的低3位值为625。
#include <stdio.h>
long fun(int x, int y, long *p )
{ int i;
long t=1;
/**************found**************/
for(i=1; i<y; i++) 改为:for(i=1; i<=y; i++)
t=t*x;
*p=t;
/**************found**************/
t=t/1000; 改为:t=t%1000;
return t;
}
main( )
{ long t,r; int x,y;
printf("\nInput x and y: "); scanf("%ld%ld",&x,&y);
t=fun(x,y,&r);
printf("\n\nx=%d, y=%d, r=%ld, last=%ld\n\n",x, y,r,t );
}
23、给定程序中fun函数的功能是:给一维数组a输入任意4个整数,并按下列的规律输出。例如输入1,2,3,4,程序运行后输出以下方阵。
4 1 2 3
3 4 1 2
2 3 4 1
1 2 3 4
#include <stdio.h>
#define M 4
/**************found**************/
void fun(int a) 改为:void fun(int a[M])
{ int i,j,k,m;
printf("Enter 4 number : ");
for(i=0; i<M; i++) scanf("%d",&a[i]);
printf("\n\nThe result :\n\n");
for(i=M;i>0;i--)
{ k=a[M-1];
for(j=M-1;j>0;j--)
/**************found**************/
aa[j]=a[j-1]; 改为:a[j]=a[j-1];
a[0]=k;
for(m=0; m<M; m++) printf("%d ",a[m]);
printf("\n");
}
}
main()
{ int a[M];
fun(a); printf("\n\n"); }
24、在主函数中从键盘输入若干个数放入数组中,用0结束输入并放在最后一个元素中。给定程序中fun函数的功能是:计算数组元素中值为正数的平均值(不包括0)。例如,数组中元素中值依次为:39,-47,21,2,-8,15,0,则程序运行结果为19.250000。
#include <stdio.h>
double fun ( int x[])
{
/************found************/
int sum = 0.0; 改为:double sum = 0.0;
int c=0, i=0;
while (x[i] != 0)
{ if (x[i] > 0) { sum += x[i]; c++; }
i++; }
/************found************/
sum \= c; 改为:sum /= c;
return sum;
}
main( )
{ int x[1000]; int i=0;
printf( "\nPlease enter some data (end with 0): " );
do { scanf("%d", &x[i]); } while (x[i++] != 0);
printf("%f\n", fun ( x ));
}
25、给定程序中fun函数的功能是:根据整形形参m,计算如下公式的值:
例如,若m中的值为5,则应输出1.463611。
#include <stdio.h>
double fun ( int m )
{ double y = 1.0 ;
int i;
/**************found**************/
for(i = 2 ; i < m ; i++) 改为:for(i = 2 ; i <= m ; i++)
/**************found**************/
y += 1 / (i * i) ; 改为:y += 1.0 / (i * i) ;
return( y ) ;
}
main( )
{ int n = 5 ;
printf( "\nThe result is %lf\n", fun ( n ) ) ; }
26、给定程序中fun函数的功能是:将p所指字符串中每个单词的最后一个字母改成大写。(这里的“单词”是指由空格隔开的字符串)。例如,若输入“I am a student to take the examination.”,则应输出“I aM A studenT tO takE thE examinatioN.”。
#include <ctype.h>
#include <stdio.h>
void fun( char *p )
{ int k = 0;
for( ; *p; p++ )
if( k )
{
/**********found***********/
if( p == ' ' ) 改为:if(* p == ' ' )
{ k = 0;
/**********found***********/
* (p-1) = toupper( *( p - 1 ) ) 改为:* (p-1) = toupper( *( p - 1 ) );
}
}
else k = 1;
}
main( )
{ char chrstr[64];
int d ;
printf( "\nPlease enter an English sentence within 63 letters: ");
gets(chrstr); d=strlen(chrstr) ;
chrstr[d] = ' ' ; chrstr[d+1] = 0 ;
printf("\n\nBefore changing:\n %s", chrstr);
fun(chrstr);
printf("\nAfter changing:\n %s", chrstr);
}
27、给定程序中fun函数的功能是:将n个无序整数从小到大排序。
#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++ ) 改为:for ( i=j+1; i<n ; i++ )
if ( a[p]>a[i] )
/************found************/
t=i; 改为:p=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 ); }
28、给定程序中fun函数的功能是:将s所指字符串中的字母转换为按字母序列的后续字母(但Z转换为A,z转换为a),其它字符不变。
#include <stdio.h>
#include <ctype.h>
void fun (char *s)
{
/**********found***********/
while(*s!='@') 改为:while(*s!='\0')
{ if(*s>='A' & *s<='Z' || *s>='a' && *s<='z')
{ if(*s=='Z') *s='A';
else if(*s=='z') *s='a';
else *s += 1;
}
/**********found***********/
(*s)++; 改为:s++;
}
}
main( )
{ char s[80];
printf("\n Enter a string with length < 80. :\n\n "); gets(s);
printf("\n The string : \n\n "); puts(s);
fun ( s );
printf ("\n\n The Cords :\n\n "); puts(s);
}
29、给定程序中fun函数的功能是:在字符串的最前端加入n个*号,形成新串,并且覆盖原串。(注意:字符串的长度最长允许为79)。
#include <stdio.h>
#include <string.h>
/**********found***********/
void fun ( char s[], int n ) 改为:void fun ( char *s, int n )
{ char a[80] , *p;
int i;
/**********found***********/
s=p; 改为:p=s;
for(i=0; i<n; i++) a[i]='*';
do
{ a[i]=*p;
i++; }
/**********found***********/
while(*p); 改为:while(*p++);
a[i]=0;
strcpy(s,a);
}
main( )
{ int n; char s[80];
printf("\nEnter a string : "); gets(s);
printf("\nThe string \"%s\"\n",s);
printf("\nEnter n ( number of * ) : "); scanf("%d",&n);
fun(s,n);
printf("\nThe string after inster : \"%s\" \n" ,s);
}
30、给定程序中fun函数的功能是:实现两个整数的交换。例如给a和b分别输入60和65,输出结果为a=65 b=60。
#include <stdio.h>
/**********found**********/
void fun ( int a, b ) 改为:void fun ( int * a, int * b )
{ int t;
/**********found**********/
t = b; b = a ; a = t; 改为:t = *b; *b = *a ; *a = t;
}
main ( )
{ int a, b;
printf ( "Enter a , b : "); scanf ( "%d%d", &a, &b );
fun ( &a , &b ) ;
printf (" a = %d b = %d\n ", a, b );
}
31、给定程序中fun函数的功能是:首先把b所指字符串中的字符按逆序存放,然后将a所指字符串中的字符和b所指字符串中的字符,按排列的顺序交叉合并到c所指数组中,过长的剩余字符接在c所指数组的尾部。例如,当a所指字符串中的内容为“abcdefg”,b所指字符串中的内容为“1234”时,c所指数组中的内容应该为“a4b3c2d1efg”;而当a所指字符串中的内容为“1234”,b所指字符串中的内容为时“abcdefg”,c所指数组中的内容应该为“1g2f3e4dcba”。
#include <stdio.h>
#include <string.h>
void fun( char *a, char *b, char *c )
{ int i , j; char ch;
i = 0; j = strlen(b)-1;
/************found************/
while ( i > j ) 改为:while ( i < j )
{ ch = b[i]; b[i] = b[j]; b[j] = ch;
i++; j--; }
while ( *a || *b ) {
if ( *a ) { *c = *a; c++; a++; }
if ( *b ) { *c = *b; c++; b++; }
}
/************found************/
c = 0; 改为:*c = ‘\0'
}
main( )
{ char s1[100],s2[100],t[200];
printf("\nEnter s1 string : ");scanf("%s",s1);
printf("\nEnter s2 string : ");scanf("%s",s2);
fun( s1, s2, t );
printf("\nThe result is : %s\n", t );
}
32、给定程序中fun函数的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码升序排序后输出。例如:若输入edcba,则应输出abcde。
#include <stdio.h>
void fun( char t[] )
{ char c;
int i, j;
/**********found***********/
for( i = strlen( t ); i; i-- ) 改为:for( i = strlen( t ); i>0; i-- )
for( j = 0; j < i; j++ )
/**********found***********/
if( t[j] < t[ j + 1 ] ) 改为:if( t[j] > t[ j + 1 ] )
{ c = t[j]; t[j] = t[ j + 1 ]; t[j + 1 ] = c; }
}
main( )
{ char s[81];
printf( "\nPlease enter a character string: " );
gets( s );
printf( "\n\nBefore sorting:\n \"%s\"", s );
fun( s );
printf( "\nAfter sorting decendingly:\n \"%s\"", s );
}
33、给定程序中fun函数的功能是:将一个八进制数字字符组成的字符串转换为其面值相等的十进制整数。规定输入的字符串最多只能包含5位八进制数字字符。例如,若输入77777,则输出将是32767。
#include <stdio.h>
int fun( char *p )
{ int n;
/**********found**********/
n= *P-'o'; 改为:n= *p-'0';
p++;
while( *p!=0 ) {
/**********found**********/
n=n*7+*P-'o'; 改为:n=n*8+*p-'0';
p++; }
return n;
}
main()
{ char s[6]; int i; int n;
printf("Enter a string (Ocatal digits): "); gets(s);
if(strlen(s)>5){ printf("Error: String too longer !\n\n");exit(0); }
for(i=0; s[i]; i++)
if(s[i]<'0'||s[i]>'7')
{ printf("Error: %c not is ocatal digits!\n\n",s[i]);exit(0); }
printf("The original string: "); puts(s);
n=fun(s);
printf("\n%s is convered to integer number: %d\n\n",s,n);
}
34、给定程序中fun函数的功能是:求出以下分数序列的前n项之和。
和的值通过函数值返回main函数。
#include <stdio.h>
/**************found**************/
fun (int n ) 改为:double fun (int n )
{ int a = 2, b = 1, c, k ;
double s=0.0 ;
for ( k = 1; k <= n; k++ )
{ s = s + 1.0 * a / b ;
/**************found**************/
c = a; a += b; b += c; 改为:c = a; a += b; b = c;
}
return(s) ;
}
main( )
{ int n = 5 ;
printf( "\nThe value of function is: %lf\n", fun ( n ) ) ; }
35、给定程序中fun函数的功能是:从s所指字符串中,找出与t所指字符串相同的子串的个数作为函数值返回。例如,当s所指字符串中的内容为“abcdabfab”,t所指字符串的内容为“ab”,则函数返回整数3.
#include <stdio.h>
#include <string.h>
int fun (char *s, char *t)
{ int n; char *p , *r;
n = 0;
while ( *s )
{ p = s; r = t;
while ( *r )
/************found************/
if ( r == p ) { 改为:if ( *r == *p ) {
/************found************/
r++; p++ 改为:r++; p++;
}
else break;
/************found************/
if ( r == '\0' ) 改为:if ( *r == '\0' )
n++;
s++;
}
return n;
}
main()
{ char s[100], t[100]; int m;
printf("\nPlease enter string S:"); scanf("%s", s);
printf("\nPlease enter substring t:"); scanf("%s", t);
m = fun( s, t);
printf("\nThe result is: m = %d\n", m); }
36、给定程序中fun函数的功能是:先将s所指字符串中的字符按逆序存放到t所指字符串中,然后把s所指串中的字符按正序连接到t所指串的后面。例如,当s所指的字符串为“ABCDE”时,则t所指的字符串应为“EDCBAABCDE”
#include <stdio.h>
#include <string.h>
void fun (char *s, char *t)
{
/************found************/
int i; 改为:int i, s1;
sl = strlen(s);
for (i=0; i<sl; i++)
/************found************/
t[i] = s[sl-i]; 改为:t[i] = s[sl-i-1];
for (i=0; i<sl; i++)
t[sl+i] = s[i];
t[2*sl] = '\0';
}
main( )
{ char s[100], t[100];
printf("\nPlease enter string s:"); scanf("%s", s);
fun(s, t);
printf("The result is: %s\n", t); }
37、给定程序中fun函数的功能是:依次取出字符串中所有数字字符,形成新的字符串,并取代原字符串。
#include <stdio.h>
void fun(char *s)
{ int i,j;
for(i=0,j=0; s[i]!='\0'; i++)
if(s[i]>='0' && s[i]<='9')
/**********found**********/
s[j]=s[i]; 改为:s[j++]=s[i];
/**********found**********/
s[j]="\0"; 改为:s[j]= ‘\0’;
}
main( )
{ char item[80];
printf("\nEnter a string : ");gets(item);
printf("\n\nThe string is : \"%s\"\n",item);
fun(item);
printf("\n\nThe string of changing is : \"%s\"\n",item ); }
38、给定程序中fun函数的功能是:统计字符串中各元音字母(即A,E,I,O,U)的个数。注意,字母不分大小写。例如输入:THIs is a boot,则输出应该是:1、0、2、2、0。
#include <stdio.h>
/**********found**********/
fun ( char *s, int num[5] ) 改为:void fun(char *s, int num[5] )
{ int k, i=5;
for ( k = 0; k<i; k++ )
/**********found**********/
num[i]=0; 改为:num[k]=0;
for (; *s; s++)
{ i = -1;
/**********found**********/
switch ( s ) 改为:switch ( *s )
{ case 'a': case 'A': {i=0; break;}
case 'e': case 'E': {i=1; break;}
case 'i': case 'I': {i=2; break;}
case 'o': case 'O': {i=3; break;}
case 'u': case 'U': {i=4; break;} }
if (i >= 0) num[i]++; }
}
main( )
{ char s1[81]; int num1[5], i;
printf( "\nPlease enter a string: " ); gets( s1 );
fun ( s1, num1 );
for ( i=0; i < 5; i++ ) printf ("%d ",num1[i]); printf ("\n"); }
39、给定程序中fun函数的功能是:计算正整数num的各位上的数字之积。例如,若输入252,则输出应该是20。若输入202,则输出应该是0。
#include <stdio.h>
long fun (long num)
{
/************found************/
long k; 改为:long k=1;
do
{ k*=num%10 ;
/************found************/
num\=10 ; 改为:num/=10 ;
} while(num) ;
return (k) ;
}
main( )
{ long n ;
printf("\Please enter a number:") ; scanf("%ld",&n) ;
printf("\n%ld\n",fun(n)) ; }
40、假定整数数列中的数不重复,并存放在数组中。给定程序中fun函数的功能是:删除数列中值为x的元素。n中存放的是数列中元素的个数。
#include <stdio.h>
#define N 20
fun(int *a,int n,int x)
{ int p=0,i;
a[n]=x;
while( x!=a[p] )
p=p+1;
/**********found**********/
if(P==n) return -1; 改为:if(p==n) return -1;
else
{ for(i=p;i<n;i++)
/**********found**********/
a[i+1]=a[i]; 改为:a[i]=a [i+1];
return n-1; }
}
main( )
{ int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;
n=10;
printf("The original data :\n");
for(i=0;i<n;i++) printf("%5d",w[i]);
printf("\nInput x (to delete): "); scanf("%d",&x);
printf("Delete : %d\n",x);
n=fun(w,n,x);
if ( n==-1 ) printf("***Not be found!***\n\n");
else
{ printf("The data after deleted:\n");
for(i=0;i<n;i++) printf("%5d",w[i]);printf("\n\n"); }
}
41、给定程序中fun函数的功能是:计算并输出下列级数的前N项之和 直到 大于q为止,q的值通过形参传入。
例如,若q的值为50.0,则函数值为49.394948。
#include <stdio.h>
double fun( double q )
{ int n; double s,t;
n = 2;
s = 2.0;
while (s<=q)
{ t=s;
/************found************/
s=s+(n+1)/n; 改为:s=s+(double)(n+1)/n;
n++; }
printf("n=%d\n",n);
/************found************/
return s; 改为:return t;
}
main ( )
{ printf("%f\n", fun(50)); }
42、给定程序中fun函数的功能是:根据输入的三个边长(整型值),判断能束构成三角形,构成的是等边三解形还是等腰三角形。若能构成等边三角形函数返回3,若能构成等腰三角形函数返回2,若能构成一般三角形函数返回1,若不能构成三角形函数返回0。
#include <stdio.h>
#include <math.h>
/**************found**************/
void fun(int a,int b,int c) 改为:int fun(int a,int b,int c)
{ if(a+b>c && b+c>a && a+c>b) {
if(a==b && b==c)
/**************found**************/
return 1; 改为:return 3;
else if(a==b||b==c||a==c)
return 2;
/**************found**************/
else retrun 1 改为:else return 1;
}
else return 0;
}
main()
{ int a,b,c,shape;
printf("\nInput a,b,c: "); scanf("%d%d%d",&a,&b,&c);
printf("\na=%d, b=%d, c=%d\n",a,b,c);
shape =fun(a,b,c);
printf("\n\nThe shape : %d\n",shape); }
43、给定程序中fun函数的功能是:计算n的5次方的值(规定n的值大于2小于8),通过形参指针传回主函数,并计算该值的个位、十位、百位上数字之和作为函数值返回。例如,7的5次方是16807,其低3位数的和值是15。
#include <stdio.h>
#include <math.h>
int fun( int n ,int *value )
{ int d,s,i;
/**************found**************/
d=0; s=0; 改为:d=1; s=0;
for(i=1; i<=5; i++) d=d*n;
*value=d;
for(i=1; i<=3; i++)
{ s=s+d%10;
/**************found**************/
d=d\10; 改为:d=d/10;
}
return s;
}
main( )
{ int n, sum, v;
do
{ printf("\nEnter n( 2<n<8): ");scanf("%d",&n); }
while(n<=2||n>=8);
sum=fun( n,&v );
printf("\n\nThe result:\n value=%d sum=%d\n\n",v,sum); }
44、给定程序中fun函数的功能是:判断一个整数是否是素数,若是返回1,否则返回0。在main( )函数中,若fun返回1输出YES,若返回0则输出NO!。
#include <stdio.h>
int fun ( int m )
{ int k = 2;
while ( k <= m && (m%k))
/************found************/
k++ 改为:k++;
/************found************/
if (m = k ) 改为:if (m == k )
return 1;
else return 0;
}
main( )
{ int n;
printf( "\nPlease enter n: " ); scanf( "%d", &n );
if ( fun ( n ) ) printf( "YES\n" );
else printf( "NO!\n" ); }
45、给定程序中fun函数的功能是:判断ch中的字符是否与str所指串中的某个字符相同;若相同,什么也不做,若不同,则将其插在串的最后。
#include <stdio.h>
#include <string.h>
/**********found**********/
void fun(char str, char ch ) 改为:void fun(char * str, char ch )
{ while ( *str && *str != ch ) str++;
/**********found**********/
if ( *str == ch ) 改为:if ( *str == ‘\0’ )
{ str [ 0 ] = ch;
/**********found**********/
str[1] = '0'; 改为:str[1] = '\0';
}
}
main( )
{ char s[81], c ;
printf( "\nPlease enter a string:\n" ); gets ( s );
printf ("\n Please enter the character to search : " );
c = getchar(); fun(s, c) ;
printf( "\nThe result is %s\n", s); }
46、给定程序中fun函数的功能是:按顺序给s所指数组中的元素赋予从2开始的偶数,然后再按顺序对每五个元素求一个平均值,并针这些值依次存放在w所指的数组中。若s所指数组中元素的个数不是5的倍数,多余部分忽略不计。例如,s所指数组在14个元素,则只对前10个元素进行处理,不对最后的4个元素求平均值。
#include <stdio.h>
#define SIZE 20
fun(double *s, double *w)
{ int k,i; double sum;
for(k=2,i=0;i<SIZE;i++)
{ s[i]=k; k+=2; }
/**********found**********/
sun=0.0; 改为:sum=0.0;
for(k=0,i=0;i<SIZE;i++)
{ sum+=s[i];
/**********found**********/
if(i+1%5==0) 改为:if((i+1)%5==0)
{ w[k]=sum/5; sum=0; k++; }
}
return k;
}
main( )
{ double a[SIZE],b[SIZE/5];
int i, k;
k = fun(a,b);
printf("The original data:\n");
for(i=0; i<SIZE; i++)
{ if(i%5==0) printf("\n");
printf("%4.0f", a[i]); }
printf("\n\nThe result :\n");
for(i=0; i<k; i++) printf("%6.2f ",b[i]);
printf("\n\n"); }
47、给定程序中fun函数的功能是:按以下递归公式求函数值。
例如,当给n输入5时,函数值为18;当给n输入3时,函数值为14。
#include <stdio.h>
/************found************/
fun ( n ) 改为:int fun ( int n )
{ int c;
/************found************/
if(n=1) 改为:if(n==1)
c = 10 ;
else c= fun(n-1)+2;
return(c);
}
main( )
{ int n;
printf("Enter n : "); scanf("%d",&n);
printf("The result : %d\n\n", fun(n)); }
48、给定程序中fun函数的功能是:用二分法求方程 的一个根,并要求绝对误差不超过0.001。例如,若给m输入-100,给n输入90,则函数求得一个根值为2.000。
#include <stdio.h>
#include <math.h>
double funx(double x)
{ return(2*x*x*x-4*x*x+3*x-6); }
double fun( double m, double n)
{
/************found************/
int r; 改为:double r;
r=(m+n)/2;
/************found************/
while(fabs(n-m)<0.001) 改为:while(fabs(n-m)>0.001)
{ if(funx(r)*funx(n)<0) m=r;
else n=r;
r=(m+n)/2; }
return r;
}
main( )
{ double m,n, root;
printf("Enter m n : \n"); scanf("%lf%lf",&m,&n);
root=fun( m,n );
printf("root = %6.3f\n",root); }
49、给定程序中fun函数的功能:用选择法对数组中的n个元素按从小到大的顺序进行排序。
#include <stdio.h>
#define N 20
void fun(int a[], int n)
{ int i, j, t, p;
for (j = 0 ;j < n-1 ;j++) {
/************found************/
p = j 改为:p = j;
for (i = j;i < n; i++)
if(a[i] < a[p])
/************found************/
p = j; 改为:p = i;
t = a[p] ; a[p] = a[j] ; a[j] = t; }
}
main( )
{ int a[N]={9,6,8,3,-1},i, m = 5;
printf("排序前的数据:") ;
for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n");
fun(a,m);
printf("排序后的数据:") ;
for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n"); }
50、给定程序中fun函数的功能是:计算函数F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值。其中x和y的值不等,z和y的值不等。例如,当x的值为9、y的值为11、z的值为15时,函数值为-3.50。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/************found************/
#define FU(m,n) (m/n) 改为:#define FU(m,n) (m)/(n)
float fun(float a,float b,float c)
{ float value;
value=FU(a+b,a-b)+FU(c+b,c-b);
/************found************/
Return(Value); 改为:return(value);
}
main( )
{ float x,y,z,sum;
printf("Input x y z: ");
scanf("%f%f%f",&x,&y,&z);
printf("x=%f,y=%f,z=%f\n",x,y,z);
if (x==y||y==z){printf("Data error!\n");exit(0);}
sum=fun(x,y,z);
printf("The result is : %5.2f\n",sum); }
51、给定程序中fun函数的功能是:根据形参m的值,计算如下公式的值:
例如,若输入5,则应输出2.283333。
#include <stdio.h>
double fun( int m )
{ double t = 1.0;
int i;
for( i = 2; i <= m; i++ )
/**********found**********/
t += 1.0/k; 改为:t += 1.0/i;
/**********found**********/
________ 改为:return t;
}
main( )
{ int m;
printf( "\nPlease enter 1 integer number:" );
scanf( "%d", &m );
printf( "\nThe result is %lf\n", fun( m ) ); }
52、给定程序中fun函数的功能是:用递归算法计算裴波拉契数列中第n项的值,从第1项起,裴波拉契数列为:1、1、2、3、5、8、13、21……
例如,若给n输入7,则裴波拉契数值为13。
#include <stdio.h>
long fun(int g)
{
/**********found**********/
switch(g); 改为:switch(g)
{ case 0: return 0;
/**********found**********/
case 1 ;case 2 : return 1 ; 改为:case 1 :case 2 : return 1 ;
}
return( fun(g-1)+fun(g-2) );
}
main( )
{ long fib; int n;
printf("Input n: "); scanf("%d",&n); printf("n = %d\n",n);
fib=fun(n);
printf("fib = %d\n\n",fib); }
53、给定程序中fun函数的功能是:用冒泡法对6个字符串按由小到大顺序进行排列。
#include <stdio.h>
#include <string.h>
#define MAXLINE 20
/**************found**************/
fun ( char *pstr[6]) 改为:void fun ( char *pstr[6])
{ int i, j ;
char *p ;
for (i = 0 ; i < 5 ; i++ ) {
/**************found**************/
for (j = i + 1, j < 6, j++) 改为:for (j = i + 1;j < 6;j++)
{
/**************found**************/
if(strcmp(*(pstr + i), (pstr + j)) > 0) 改为:if(strcmp(*(pstr + i), *(pstr + j)) > 0)
{ p = *(pstr + i) ;
/**************found**************/
*(pstr + i) = pstr + j ; 改为:*(pstr + i) = *(pstr + j) ;
*(pstr + j) = p ; }
}
}
}
main( )
{ int i ;
char *pstr[6], str[6][MAXLINE] ;
for(i = 0; i < 6 ; i++) pstr[i] = str[i] ;
printf( "\nEnter 6 string(1 string at each line): \n" ) ;
for(i = 0 ; i < 6 ; i++) scanf("%s", pstr[i]) ;
fun(pstr) ;
printf("The strings after sorting:\n") ;
for(i = 0 ; i < 6 ; i++) printf("%s\n", pstr[i]) ; }
54、给定程序中fun函数的功能是:将m(1<=m<=10)个字符串连接起来,组成一个新串,放入pt所指存储区中。例如,把3个串“abc”,“CD”,“EF”连接起来,结果是“abcCDEF”。
#include <stdio.h>
#include <string.h>
/************found************/
int fun ( char str[][10], int m, char *pt ) 改为:void fun ( char str[][10], int m,char *pt )
{
/************found************/
Int k, q, i ; 改为:int k, q, i ;
for ( k = 0; k < m; k++ )
{ q = strlen ( str [k] );
for (i=0; i<q; i++)
/************found************/
pt[i] = str[k,i] ; 改为:pt[i] = str[k][i] ;
pt += q ; pt[0] = 0 ; }
}
main( )
{ int m, h ;
char s[10][10], p[120] ;
printf( "\nPlease enter m:" ) ;
scanf("%d", &m) ; gets(s[0]) ;
printf( "\nPlease enter %d string:\n", m ) ;
for ( h = 0; h < m; h++ ) gets( s[h]) ;
fun(s, m, p) ;
printf( "\nThe result is : %s\n", p) ; }
55、已知一个数列从第0项开始的前三项分别为0、0、1,以后各项都是其相邻前三项之和。给定程序中fun函数的功能是:计算并输出该数列前n项的平方根之和,n的值通过形参传入。例如,当n=10时,输出结果为23.197745。
#include <stdio.h>
#include <math.h>
/************found************/
fun(int n) 改为:double fun(int n)
{ double sum, s0, s1, s2, s; int k;
sum = 1.0;
if (n <= 2) sum = 0.0;
s0 = 0.0; s1 = 0.0; s2 = 1.0;
for (k = 4; k <= n; k++)
{ s = s0 + s1 + s2;
sum += sqrt(s);
s0 = s1; s1 = s2; s2 = s; }
/************found************/
return sum 改为:return sum;
}
main ( )
{ int n;
printf("Input N=");
scanf("%d", &n);
printf("%f\n", fun(n) ); }
56、给定程序中fun函数的功能是:求两个非零正整数的最大公约数,并作为函数值返回。例如,num1和num2分别输入49和21,则输出最大公约数为7;若给num1和num2分别输入27和81,则输出最大公约数为27。
#include <stdio.h>
int fun(int a,int b)
{ int r,t;
if(a<b) {
/************found************/
t=a; b=a; a=t; 改为:t=a; a=b; a=t;
}
r=a%b;
while(r!=0)
{ a=b; b=r; r=a%b; }
/************found************/
return(a); 改为:return(b);
}
main( )
{ int num1, num2,a;
printf("Input num1 num2: "); scanf("%d%d",&num1,&num2);
printf("num1= %d num2= %d\n\n",num1,num2);
a=fun(num1,num2);
printf("The maximun common divisor is %d\n\n",a); }
57、给定程序中creatlink函数的功能是:创建带头节点的单向链表,并为各节点数值域赋0到m-1的值。
#include <stdio.h>
#include <stdlib.h>
typedef struct aa
{ int data;
struct aa *next;
} NODE;
NODE *Creatlink(int n, int m)
{ NODE *h=NULL, *p, *s;
int i;
/**********found***********/
p=(NODE )malloc(sizeof(NODE)); 改为:p=(NODE *)malloc(sizeof(NODE));
h=p;
/**********found***********/
p->next=NULL; 改为:p=s;
for(i=1; i<=n; i++)
{ s=(NODE *)malloc(sizeof(NODE));
/**********found***********/
s->data=rand()%m; 改为:s->data=rand()%(m-1);
s->next=p->next;
p->next=s;
p=p->next;
}
/**********found***********/
return p; 改为:return h;
}
outlink(NODE *h)
{ NODE *p;
p=h->next;
printf("\n\nTHE LIST :\n\n HEAD ");
while(p)
{ printf("->%d ",p->data);
p=p->next; }
printf("\n");
}
main( )
{ NODE *head;
head=Creatlink(8,22);
outlink(head); }
58、给定程序中fun函数的功能是:计算整数n的阶乘。
#include <stdio.h>
double fun(int n)
{ double result=1.0;
while (n>1 && n<170)
/*********found*********/
result*=--n; 改为:result*=n--;
/*********found*********/
return _____; 改为:return result;
}
main( )
{ int n;
printf("Enter an integer: ");
scanf("%d",&n);
printf("\n\n%d!=%lg\n\n",n,fun(n)); }
59、给定程序中fun函数的功能是:删除p所指字符串中的所有空白字符(包括Tab、回车符及换行符)。输入字符串时用‘#’结束输入。
#include <string.h>
#include <stdio.h>
#include <ctype.h>
fun ( char *p)
{ int i,t; char c[80];
/************found************/
For (i = 0,t = 0; p[i] ; i++) 改为:for (i = 0,t = 0; p[i] ; i++)
if(!isspace(*(p+i))) c[t++]=p[i];
/************found************/
c[t]="\0"; 改为:c[t]=’\0’;
strcpy(p,c);
}
main( )
{ char c,s[80];
int i=0;
printf("Input a string:"); c=getchar();
while(c!='#')
{ s[i]=c;i++;c=getchar(); }
s[i]='\0'; fun(s); puts(s); }
60、数列中,第一项值为3,后一项都比前一项的值增5;给定程序中fun函数的功能是:计算前n项(4<n<50)项的累加和,在累加过程中把那些被4除后余2的当前累加值放入数组中,符合条件的累加值的个数作为函数值返回主函数。例如,当n的值为20时,该数列为3,8,13,18,23,28…93,98。符合此条件的累加值应为42,126,366,570,1010。
#include <stdio.h>
#define N 20
int fun(int n,int *a)
{ int i,j,k,sum;
/**************found**************/
sum=j==0; 改为:sum=j=0;
for(k=3,i=0;i<n;i++,k+=5)
{ sum=sum+k;
/**************found**************/
if(sum%4=2) 改为:if(sum%4==2)
a[j++]=sum; }
return j;
}
main( )
{ int a[N],d,n,i;
printf("\nEnter n (4<n<=50): ");scanf("%d",&n);
d=fun(n,a);
printf("\n\nThe result :\n");
for(i=0; i<d; i++)printf("%6d",a[i]);printf("\n\n"); }
61、给定程序中fun函数的功能是:计算S=f(-n)+f(-n+1)+…+f(0)+f(1)+f(2)+…f(n)的值。例如,当n为5时,函数值应为:10.407143。f(x)函数定义如下:
#include <stdio.h>
#include <math.h>
/************found************/
f( double x) 改为:double f( double x)
{ if (x == 0.0 || x == 2.0) return 0.0;
else if (x < 0.0) return (x -1)/(x-2);
else return (x +1)/(x-2); }
double fun( int n )
{ int i; double s=0.0, y;
for (i= -n; i<=n; i++)
{y=f(1.0*i); s += y;}
/************found************/
return s 改为:return s;
}
main ( )
{ printf("%f\n", fun(5) ); }
62、给定程序中fun函数的功能是:求出数组中最大数和次最大数,并把最大数和a[0]中的数对调,次最大数和a[1]中的数对调。
#include <stdio.h>
#define N 20
/**********found**********/
int fun ( int * a, int n ) 改为:void fun ( int * a, int n )
{ int i, m, t, k ;
for(i=0;i<2;i++) {
/**********found**********/
m=0; 改为:m=i+1;
for(k=i+1;k<n;k++)
/**********found**********/
if(a[k]>a[m]) k=m; 改为:if(a[k]>a[m]) m=k;
t=a[i];a[i]=a[m];a[m]=t; }
}
main( )
{ int x, b[N]={11,5,12,0,3,6,9,7,10,8}, n=10, i;
for ( i=0; i<n; i++ ) printf("%d ", b[i]);
printf("\n");
fun ( b, n );
for ( i=0; i<n; i++ ) printf("%d ", b[i]);
printf("\n"); }
63、给定程序中fun函数的功能是:找出一个大于给定整数m且紧随m的素数,并作为函数值返回。
#include <stdio.h>
int fun(int m)
{ int i, k ;
for (i = m + 1 ; ; i++) {
for (k = 2 ; k < i ; k++)
/**************found**************/
if (i % k != 0) 改为:if (i % k == 0)
break ;
/**************found**************/
if (k < i) 改为:if (k >= i)
return(i); }
}
main( )
{ int n;
clrscr( ); printf(“\n please enter n: “);
scanf(“%d”,&n); printf(“%d\n”,fun(n)); }
64、给定程序中fun函数的功能是:将p所指字符串中的所有字符复制到b中,要求复制三个字符之后插入一个空格。例如,在调用fun函数之前给a输入字符串:ABCDEFGHIJK,调用函数之后,字符数组b中的内容则为:ABC DEF GHI JK。
#include <stdio.h>
void fun(char *p, char *b)
{ int i, k=0;
while(*p)
{
/**********found**********/
i=1; 改为:i=0;
/**********found**********/
while( i<=3 ||*p ) 改为:while( i<3 && *p )
{
/**********found**********/
b[k]=p; 改为:b[k]=*p;
k++; p++; i++;
}
if(*p)
{
/**********found**********/
b[k++]=" "; 改为:b[k++]=’ ‘;
}
}
b[k]='\0';
}
main( )
{ char a[80],b[80];
printf("Enter a string: "); gets(a);
printf("The original string: "); puts(a);
fun(a,b);
printf("\nThe string after insert space: "); puts(b); printf("\n\n"); }
65、给定程序中fun函数的功能是:计算小于形参k的最大的10个能被13或17整除的自然数之和。k的值由主函数传入,若k的值为500,则函数值为4622。
#include <stdio.h>
int fun( int k )
{ int m=0, mc=0, j ;
while ((k >= 2) && (mc < 10))
{
/************found************/
if ((k%13 = 0) || (k%17 = 0)) 改为:if ((k%13 == 0) || (k%17 == 0))
{ m = m+ k; mc++; }
k--;
}
return m;
/************found************/
_____ 改为:}
main ( )
{ printf("%d\n", fun (500)); }
66、给定程序中fun函数的功能是:将长整形数中每一位上为奇数的数依次取出,构成一个新书放在t中。高位仍在高位,低位仍在低位。例如,当s中的数为:87653142时,t中的数为:7531。
#include <stdio.h>
void fun (long s, long *t)
{ int d;
long sl=1;
/************found************/
t = 0; 改为:*t = 0;
while ( s > 0)
{ d = s%10;
/************found************/
if (d%2 == 0) 改为:if (d%2 != 0)
{ *t = d * sl + *t; sl *= 10; }
s /= 10; }
}
main( )
{ long s, t;
clrscr();
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t); }
67、给定程序中fun函数的功能是:交换主函数中两个变量的值。例如:若变量a中的值原为8,b中的值为3。程序运行后a中的值为3,b中的值为8。
#include <stdio.h>
/*********found**********/
int fun(int x,int y) 改为:int fun(int *x,int *y)
{ int t;
/*********found**********/
t=x;x=y;y=t; 改为:t=*x;*x=*y;*y=t;
}
main( )
{ int a,b;
a=8;b=3; fun(&a,&b); printf("%d, %d\n",a,b); }
68、给定程序中fun函数的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本算法是:先对字符串中的头两个元素进行排序。然后把第三个字符插入到前两个字符中,插入后前三个字符依然有序;再把第四个字符插入到前三个字符中,……。待排序的字符串已在主函数中赋予。
#include <stdio.h>
#include <string.h>
#define N 80
void insert(char *aa)
{ int i,j,n; char ch;
/**********found**********/
n=strlen[ aa ]; 改为:n=strlen(aa);
for( i=1; i<n ;i++ ) {
/**********found**********/
c=aa[i]; 改为:ch=aa[i];
j=i-1;
while ((j>=0) && ( ch<aa[j] ))
{ aa[j+1]=aa[j];
j--; }
aa[j+1]=ch; }
}
main( )
{ char a[N]="QWERTYUIOPASDFGHJKLMNBVCXZ";
int i ;
printf ("The original string : %s\n", a);
insert(a) ;
printf("The string after sorting : %s\n\n",a ); }
69、给定程序中fun函数的功能是:为一个偶数寻找两个素数,这两个素数之和等于该偶数,并将这两个素数通过形参指针传回主函数。
#include <stdio.h>
#include <math.h>
void fun(int a,int *b,int *c)
{ int i,j,d,y;
for(i=3;i<=a/2;i=i+2) {
/**************found**************/
Y=1; 改为:y=1;
for(j=2;j<=sqrt((double)i);j++)
if(i%j==0) y=0;
if(y==1) {
/**************found**************/
d==a-i; 改为:d=a-i;
for(j=2;j<=sqrt((double)d);j++)
if(d%j==0) y=0;
if(y==1) { *b=i; *c=d; }
}
}
}
main( )
{ int a,b,c;
do
{ printf("\nInput a: "); scanf("%d",&a); } while(a%2);
fun(a,&b,&c);
printf("\n\n%d = %d + %d\n",a,b,c); }
70、给定程序中fun函数的功能是:比较两个字符串,将长的那个字符串的首地址作为函数值返回。
#include <stdio.h>
/**********found**********/
char fun(char *s, char *t) 改为:char *fun(char *s, char *t)
{ int sl=0,tl=0; char *ss, *tt;
ss=s; tt=t;
while(*ss)
{ sl++;
/**********found**********/
(*ss)++; 改为:ss++;
}
while(*tt)
{ tl++;
/**********found**********/
(*tt)++; 改为:tt++;
}
if(tl>sl) return t;
else return s;
}
main( )
{ char a[80],b[80],*p,*q; int i;
printf("\nEnter a string : "); gets(a);
printf("\nEnter a string again : "); gets(b);
printf("\nThe longer is :\n\n\"%s\"\n",fun(a,b)); }
71、给定程序中fun函数的功能是:读入一个英文文本行,将其中每个单词的第一个字符改为大写,然后输出次文本行。(这里的“单词”是指由空格隔开的字符串)。例如,若输入“I am a student to take the examination.”,则应输出“I Am A Student To Take The Examination.”。
#include <ctype.h>
#include <string.h>
/************found************/
include (stdio.a) 改为:# include “stdio.h”
/************found************/
upfst ( char p ) 改为:upfst ( char *p )
{ int k=0;
for ( ; *p; p++ )
if ( k ) { if ( *p == ' ' ) k = 0; }
else if ( *p != ' ' ) { k = 1; *p = toupper( *p ); }
}
main( )
{ char chrstr[81];
printf( "\nPlease enter an English text line: " ); gets( chrstr );
printf( "\n\nBefore changing:\n %s", chrstr );
upfst( chrstr );
printf( "\nAfter changing:\n %s\n", chrstr ); }
72、给定程序中fun函数的功能是:根据形参m的值(2<=m<=9),在m行m列的二维数组中存放如下所示规律的数据,由main函数输出。例如,
若输入2,则输出 若输入4,则输出
1 2 1 2 3 4
2 4 2 4 6 8
3 6 9 12
4 8 12 16
#include <conio.h>
#define M 10
int a[M][M] = {0} ;
/**************found**************/
fun(int **a, int m) 改为:void fun(int (*a)[M], int m)
{ int j, k ;
for (j = 0 ; j < m ; j++ )
for (k = 0 ; k < m ; k++ )
/**************found**************/
a[j][k] = k * j ; 改为:a[j][k] = (k+1) * (j+1);
}
main ( )
{ int i, j, n ;
printf ( " Enter n : " ) ; scanf ("%d", &n ) ;
fun ( a, n ) ;
for ( i = 0 ; i < n ; i++)
{ for (j = 0 ; j < n ; j++)
printf ( "%4d", a[i][j] ) ;
printf ( "\n" ) ; }
}
73、给定程序中fun函数的功能是:对N名学生的学习成绩,按从高到低的顺序找出前m(m<=10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#define N 10
typedef struct ss
{ char num[10];
int s;
} STU;
STU *fun(STU a[], int m)
{ STU b[N], *t;
int i,j,k;
/**********found**********/
t=(STU *)calloc(sizeof(STU),m) 改为:t=(STU *)malloc(sizeof(STU));
for(i=0; i<N; i++) b[i]=a[i];
for(k=0; k<m; k++)
{ for(i=j=0; i<N; i++)
if(b[i].s > b[j].s) j=i;
/**********found**********/
t(k)=b(j); 改为:t[k]=b[j];
b[j].s=0; }
return t;
}
outresult(STU a[], FILE *pf)
{ int i;
for(i=0; i<N; i++)
fprintf(pf,"No = %s Mark = %d\n", a[i].num,a[i].s);
fprintf(pf,"\n\n"); }
main( )
{ STU a[N]={ {"A01",81},{"A02",89},{"A03",66},{"A04",87},{"A05",77},
{"A06",90},{"A07",79},{"A08",61},{"A09",80},{"A10",71} };
STU *pOrder;
int i, m;
printf("***** The Original data *****\n");
outresult(a, stdout);
printf("\nGive the number of the students who have better score: ");
scanf("%d",&m);
while( m>10 )
{ printf("\nGive the number of the students who have better score: ");
scanf("%d",&m); }
pOrder=fun(a,m);
printf("***** THE RESULT *****\n");
printf("The top :\n");
for(i=0; i<m; i++)
printf(" %s %d\n",pOrder[i].num , pOrder[i].s);
free(pOrder); }
74、给定程序中fun函数的功能是:从低位开始取出长整形变量s中奇数为上的数,依次构成一个新书放在t中。高位仍在高位,地位仍在低位。例如,当s中的数为:7654321时,t中的数为7531。
#include <stdio.h>
/************found************/
void fun (long s, long t) 改为:void fun (long s, long *t)
{ long sl=10;
*t = s % 10;
while ( s > 0)
{ s = s/100;
*t = s%10 * sl + *t;
/************found************/
sl = sl*100; 改为:sl = sl*10;
}
}
main( )
{ long s, t;
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t); }
75、给定程序中fun函数的功能是:从n(形参)个学生的成绩中统计出低于平均分的学生人数,此人数有函数值返回,平均分存放在形参aver所指的存储单元中。例如,若输入8名学生的成绩:80.5 60 72 90.5 98 51.5 88 64,则低于平均分的学生人数为4(平均分为75.5625)。
#include <stdio.h>
#define N 20
int fun ( float *s, int n, float *aver )
{ float ave, t = 0.0 ;
int count = 0, k, i ;
for ( k = 0 ; k < n ; k++ )
/**************found**************/
t = s[k] ; 改为: t+= s[k] ;
ave = t / n ;
for ( i = 0 ; i < n ; i++ )
if ( s[ i ] < ave ) count++ ;
/**************found**************/
*aver = Ave ; 改为:*aver = ave ;
return count ;
}
main ( )
{ float s[30], aver ;
int m, i ;
printf ( "\nPlease enter m: " ) ; scanf ("%d", &m ) ;
printf ( "\nPlease enter %d mark :\n ", m ) ;
for( i = 0 ; i < m ; i++ ) scanf ( "%f", s + i ) ;
printf( "\nThe number of students : %d \n" , fun ( s, m, &aver ) );
printf( "Ave = %f\n", aver ) ; }
76、给定程序中fun函数的功能是:根据整形形参m,计算如下公式的值。
例如,若m=2000,则应输出0.000160。
#include <stdio.h>
/************found************/
fun ( int m ) 改为:double fun ( int m )
{ double y = 0, d ;
int i ;
/************found************/
for( i = 100, i <= m, i += 100 ) 改为:for( i = 100;i <= m; i += 100 )
{ d = (double)i * (double)i ;
y += 1.0 / d ; }
return( y ) ;
}
main( )
{ int n = 2000 ;
printf( "\nThe result is %lf\n", fun ( n ) ) ; }
77、给定程序中fun函数的功能是:读入一个整数k(2<=k<=10000),打印它的所有质因子(即所有为素数的因子)。例如,若输入整数2310,则应输出:2、3、5、7、11。
#include <stdio.h>
/************found************/
IsPrime ( int n ); 改为:IsPrime ( int n )
{ int i, m;
m = 1;
for ( i = 2; i < n; i++ )
/************found************/
if !( n%i ) 改为:if (!( n%i ))
{ m = 0; break ; }
return ( m );
}
main( )
{ int j, k;
printf( "\nPlease enter an integer number between 2 and 10000: " );
scanf( "%d", &k );
printf( "\n\nThe prime factor(s) of %d is( are ):", k );
for( j = 2; j <= k; j++ )
if( ( !( k%j ) )&&( IsPrime( j ) ) ) printf( "\n %4d", j );
printf("\n"); }
78、给定程序中fun函数的功能是:根据以下公式求л值,并作为函数值返回。例如,给指定精度的变量eps输入0.0005时,应当输出Pi=3.140578。
#include <math.h>
#include <stdio.h>
double fun(double eps)
{ double s,t; int n=1;
s=0.0;
/************found************/
t=0; 改为:t=1.0;
/************found************/
while( t>eps) 改为:while( t>=eps)
{ s+=t; t=t * n/(2*n+1); n++; }
/************found************/
return(s); 改为:return(s*2);
}
main( )
{ double x;
printf("\nPlease enter a precision: "); scanf("%lf",&x);
printf("\neps=%lf, Pi=%lf\n\n",x,fun(x)); }
79、给定程序中fun函数的功能是:分别统计字符串中大些字母和小写字母的个数。例如,给字符串s输入:AAaaBBb123CCccccd,则应输出结果:upper – 6, lower -8。
#include <stdio.h>
/**********found**********/
void fun ( char *s, int a, int b ) 改为:void fun ( char *s, int *a, int *b )
{ while ( *s )
{ if ( *s >= 'A' && *s <= 'Z' )
/**********found**********/
*a=a+1 ; 改为:(*a)++;
if ( *s >= 'a' && *s <= 'z' )
/**********found**********/
*b=b+1; 改为:(*b)++;
s++; }
}
main( )
{ char s[100]; int upper = 0, lower = 0 ;
printf( "\nPlease a string : " ); gets ( s );
fun ( s, & upper, &lower );
printf( "\n upper = %d lower = %d\n", upper, lower ); }
80、给定程序中fun函数的功能是:找出100至n(不大于1000)之间三位数字相等的所有整数,把这些整数放在s所指数组中,个数作为函数值返回。
#include <stdio.h>
#define N 100
int fun(int *s, int n)
{ int i,j,k,a,b,c;
j=0;
for(i=100; i<n; i++) {
/**************found**************/
k=n; 改为:k=i;
a=k%10; k/=10;
/**************found**************/
b=k%10; k/=10; 改为:b=k%10;
/**************found**************/
c=k%10 改为:c=k/10;
if( a==b && a==c ) s[j++]=i; }
return j;
}
main( )
{ int a[N], n, num=0, i;
do { printf("\nEnter n( <=1000 ) : "); scanf("%d",&n); } while(n > 1000);
num = fun( a,n );
printf("\n\nThe result :\n");
for(i=0; i<num; i++)printf("%5d",a[i]);
printf("\n\n"); }
81、由N个有序整数组成的数列已放在一维数组中,给定程序中fun函数的功能是:利用折半查找算法查找整数m在数组中的位置。若找到,返回其下标值;反之,返回-1。折半查找的基本算法是:每次查找前先确定数组中待查的范围:low和high(low<high),然后把m与中间位置(mid)中元素的值进行比较。如果m的值大于中间位置元素中的值,则下一次的查找范围放在中间位置之后的元素中;反之,下一次的查找范围罗在中间位置之前的元素中。直到low>high,查找结束。
#include <stdio.h>
#define N 10
/************found************/
void fun(int a[], int m ) 改为:int fun(int a[], int m )
{ int low=0,high=N-1,mid;
while(low<=high)
{ mid=(low+high)/2;
if(m<a[mid]) high=mid-1;
/************found************/
else If(m > a[mid]) 改为:else if(m > a[mid])
low=mid+1;
else return(mid);
}
return(-1);
}
main()
{ int i,a[N]={-3,4,7,9,13,45,67,89,100,180 },k,m;
printf("a数组中的数据如下:");
for(i=0;i<N;i++) printf("%d ", a[i]);
printf("Enter m: "); scanf("%d",&m);
k=fun(a,m);
if(k>=0) printf("m=%d,index=%d\n",m,k);
else printf("Not be found!\n"); }
82、给定程序中fun函数的功能是:从3个红球,5个白球,6个黑球中任意取出8个作为一组,进行输出。在每组中,可以没有黑球,但必须要有红球和白球。组合数作为函数值返回。正确的组合数应该是15。程序中i的值代表红球数,j的值代表白球数,k的值代表黑球数。
#include <stdio.h>
int fun()
{ int i,j,k,sum=0;
printf("\nThe result :\n\n");
/**************found**************/
for(i=0; i<=3; i++) 改为:for(i=1; i<=3; i++)
{ for(j=1; j<=5; j++)
{ k=8-i-j;
/**************found**************/
if(K>=0 && K<=6) 改为:if((k>=1&&k<=6)&&(i!=0&&j!=0)||k==0)
{ sum=sum+1;
printf("red:%4d white:%4d black:%4d\n",i,j,k); }
}
}
return sum;
}
main( )
{ int sum;
sum=fun(); printf("sum =%4d\n\n",sum); }
83、给定程序中fun函数的功能是:根据整型形参m,计算如下公式的值。
例如,若主函数中输入5,则应输出-0.283333。
#include <stdio.h>
double fun( int m )
{ double t = 1.0;
int i;
for( i = 2; i <= m; i++ )
/**********found**********/
t = 1.0-1 /i; 改为:t-=1.0/i;
/**********found**********/
_______; 改为:return t;
}
main( )
{ int m ;
printf( "\nPlease enter 1 integer numbers:\n" );
scanf( "%d", &m);
printf( "\n\nThe result is %lf\n", fun( m ) ); }
84、给定程序中fun函数的功能是:计算s所指字符串中含有t所指字符串的数目,并作为函数值返回。
#include <stdio.h>
#include <string.h>
#define N 80
int fun(char *s, char *t)
{ int n; char *p , *r;
n=0;
while ( *s )
{ p=s;
/*********found**********/
r=p; 改为:r=t;
while(*r)
if(*r==*p) { r++; p++; } else break;
/*********found**********/
if(*r= O) 改为:if(*r== ‘\0’)
n++;
/*********found**********/
______; 改为:s++;
}
return n;
}
main( )
{ char a[N],b[N]; int m;
printf("\nPlease enter string a : "); gets(a);
printf("\nPlease enter substring b : "); gets( b );
m=fun(a, b);
printf("\nThe result is : m = %d\n",m); }
85、给定程序中fun函数的功能是:首先将大写字母转换为对应小写字母,若小写字母为a~u,则将其装换位其后的第5个字母;若小写字母为v~z,使其值减21。转换后的小写字母作为函数值返回。例如,若形参是字母A,则转换为小写字母f,若形参是字母W,则转换为小写字母b。
#include <stdio.h>
#include <ctype.h>
char fun(char c)
{ if( c>='A' && c<='Z')
/**************found**************/
C=C+32; 改为:c=c+32;
if(c>='a' && c<='u')
/**************found**************/
c=c-5; 改为:c=c+5;
else if(c>='v'&&c<='z') c=c-21;
return c;
}
main( )
{ char c1,c2;
printf("\nEnter a letter(A-Z): "); c1=getchar();
if( isupper( c1 ) )
{ c2=fun(c1);
printf("\n\nThe letter \'%c\' change to \'%c\'\n", c1,c2); }
else printf("\nEnter (A-Z)!\n"); }
86、给定程序中fun函数的功能是:将s所指字符串中出现的与t1所指字符串相同的子串全部替换成t2所指字符串,所形成的新串放在w所指的数组中。在此处,要求t1与t2所指字符串的长度相同。例如,当c所指字符串中的内容为“abcdabfab”,t1所指子串中的内容为:“ab”,t2所指子串中的内容为:“99”时,结果在w所指的数组中的内容应为:“99cd99f99”。
#include <stdio.h>
#include <string.h>
/************found************/
int fun (char *s, char *t1, char *t2 , char *w)
/* 改为:void 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 ) 改为:while ( *r )
if ( *r == *p ) { r++; p++; }
else break;
if ( *r == '\0' )
{ a = w; r = t2;
while ( *r ){
/************found************/
*a = *r; a++; r++ 改为:*a = *r; a++; r++;
}
w += strlen(t2) ; }
else w++;
}
}
main( )
{ char s[100], t1[100], t2[100], w[100];
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"); }
87、给定程序中fun函数的功能是:逐个比较p、q所指两个字符串对应位置中的字符,把ASCII值大或相等的字符依次放到c所指数组中,形成一个新的字符串。例如,若主函数中a字符串为:aBCDeFgH,主函数中b字符串为:ABcd,则c中的字符串应为:aBcdeFgH。
#include <stdio.h>
#include <string.h>
void fun(char *p ,char *q, char *c)
{
/************found************/
int k = 1; 改为:int k = 0;
/************found************/
while( *p != *q ) 改为:while( *p ||*q )
{ if( *p<*q ) c[k]=*q;
else c[k]=*p;
if(*p) p++;
if(*q) q++;
k++; }
}
main( )
{ char a[10]="aBCDeFgH", b[10]="ABcd", c[80]={'\0'};
fun(a,b,c);
printf("The string a: "); puts(a);
printf("The string b: "); puts(b);
printf("The result : "); puts(c); }
88、给定程序中fun函数的功能是:把主函数中输入的3个数,最大的放在a中,最小的放在c中。例如,输入的数为:55 12 34,输出结果应当是:a-55.0,b-34.0,c-12.0。
#include <stdio.h>
void fun(float *p,float *q,float *s)
{
/**********found**********/
float *k; 改为:float k;
if( *p<*q ) { k=*p; *p=*q; *q=k; }
/**********found**********/
if( *p>*s ) 改为:if( *p<*s )
{ k=*s; *s=*p; *p=k; }
if( *q<*s ) { k=*q; *q=*s; *s=k; }
}
main( )
{ float a,b,c;
printf("Input a b c: "); scanf("%f%f%f",&a,&b,&c);
printf("a = %4.1f, b = %4.1f, c = %4.1f\n\n",a,b,c);
fun(&a,&b,&c);
printf("a = %4.1f, b = %4.1f, c = %4.1f\n\n",a,b,c); }
89、给定程序中fun函数的功能是:利用夏明的公式求л的近似值,直到最后一项的绝对值小于指定的数(参数num)为止:
例如,程序运行后,输入0.0001,则程序输出3.1414。
#include <math.h>
#include <stdio.h>
float fun ( float num )
{ int s ;
float n, t, pi ;
t = 1 ; pi = 0 ; n = 1 ; s = 1 ;
/**************found**************/
while(t >= num) 改为:while(fabs(t) >= num)
{ pi = pi + t ; n = n + 2 ; s = -s ;
/**************found**************/
t = s % n ; 改为:t = s / n;
}
pi = pi * 4 ; return pi ;
}
main( )
{ float n1, n2 ;
printf("Enter a float number: ") ;
scanf("%f", &n1) ;
n2 = fun(n1) ;
printf("%6.4f\n", n2) ; }
90、给定程序中fun函数的功能是:计算并输出high以内最大的10个素数之和。high的值由主函数传给fun函数。若high的值为100,则函数的值为732。
#include <stdio.h>
#include <math.h>
int fun( int high )
{ int sum = 0, n=0, j, yes;
/************found************/
while ((high >= 2) && (n < 10) 改为:while ((high >= 2) && (n < 10))
{ yes = 1;
for (j=2; j<=high/2; j++ )
if (high % j ==0 ){
/************found************/
yes=0; break 改为:yes=0; break;
}
if (yes) { sum +=high; n++; }
high--; }
return sum ;
}
main ( )
{ printf("%d\n", fun (100)); }
91、给定程序中fun函数的功能是:将字符串中的字符按逆序输出,但不改变字符串中的内容。例如,若字符串为abcd,则应输出dcba。
#include <stdio.h>
/************found************/
fun (char a) 改为:void fun(char *a)
{ if ( *a )
{ fun(a+1) ;
/************found************/
printf("%c" *a) ; 改为:printf("%c", *a) ;
}
}
main( )
{ char s[10]="abcd";
printf("处理前字符串=%s\n处理后字符串=", s);
fun(s); printf("\n") ; }
92、给定程序中fun函数的功能是:求出以下分述序列的前n项之和。和值通过函数值返回到main函数。
例如,若n=5,则应输出8.391667。
#include <stdio.h>
/************found************/
fun ( int n ) 改为:double fun ( int n )
{ int a, b, c, k; double s;
s = 0.0; a = 2; b = 1;
for ( k = 1; k <= n; k++ ) {
/************found************/
s = s + (Double)a / b; 改为:s = s + (double)a / b;
c = a; a = a + b; b = c;
}
return s;
}
main( )
{ int n = 5;
printf( "\nThe value of function is: %lf\n", fun ( n ) ); }
93、给定程序中fun函数的功能是:从低位开始取出长整型变量s中偶数位上的数,依次构成一个新数放在t中。高位仍在高位,低位仍在低位。例如,当s中的数为:7654321时,t中的数为642。
#include <stdio.h>
/************found************/
void fun (long s, long t) 改为:void fun (long s, long *t)
{ long sl=10;
s /= 10; *t = s % 10;
/************found************/
while ( s < 0) 改为:while ( s > 0)
{ s = s/100; *t = s%10*sl + *t; sl = sl * 10; }
}
main()
{ long s, t;
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t);
}
94、给定程序中fun函数的功能是:将长整型数中每一位上为偶数的数依次取出,构成一个新数放在t中。高位仍在高位,低位仍在低位。例如,当s中的数为:87653142时,t中的数为8642。
#include <stdio.h>
void fun (long s, long *t) {
/************found************/
int d; 改为:long d;
long sl=1;
*t = 0;
while ( s > 0)
{ d = s%10;
/************found************/
if (d%2=0) 改为:if (d%2==0)
{ *t=d* sl+ *t; sl *= 10; }
/************found************/
s \= 10; 改为:s /= 10;
}
}
main( )
{ long s, t;
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t); }
95、给定程序中fun函数的功能是:将s所指字符串中最后一次出现的与t1所指字符串相同的子串替换成t2所指字符串,所形成的新串放在w所指的数组中。在此处,要求t1与t2所指字符串的长度相同。例如,当c所指字符串中的内容为“abcdabfabc”,t1所指子串中的内容为:“ab”,t2所指子串中的内容为:“99”时,结果在w所指的数组中的内容应为:“abcdabf99c”。
#include <stdio.h>
#include <string.h>
/************found************/
int fun (char *s, char *t1, char *t2 , char *w)
/* 改为:void fun (char *s, char *t1, char *t2 , char *w) */
{ int i; char *p , *r, *a;
strcpy( w, s );
/************found************/
while ( w ) 改为:while ( *w )
{ p = w; r = t1;
while ( *r )
/************found************/
IF ( *r == *p ) 改为:if ( *r == *p )
{ r++; p++; }
else break;
if ( *r == '\0' ) a = w;
w++; }
r = t2;
while ( *r ){ *a = *r; a++; r++; }
}
main()
{ char s[100], t1[100], t2[100], w[100];
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("\nError : strlen(t1) != strlen(t2)\n"); }
96、给定程序中fun函数的功能是:应用递归算法求形参a的平方根。求平方根的迭代公式如下:
例如,a为2时,平方根为1.414214。
#include <stdio.h>
#include <math.h>
/**********found**********/
double fun(double a, dounle x0) 改为:double fun(double a, double x0)
{ double x1, y;
x1=(x0+ a/x0)/2.0;
/**********found**********/
if( fabs(x1-xo)>0.00001 ) 改为: if( fabs(x1-x0)>=0.00001 )
y=fun(a,x1);
else y=x1;
return y;
}
main( )
{ double x;
printf("Enter x: "); scanf("%lf",&x);
printf("The square root of %lf is %lf\n",x,fun(x,1.0)); }
97、给定程序中fun函数的功能是:统计子字符串substr在字符串str中出现的次数。例如,若字符串为aaas lkaaas,子字符串为as,则应输出2。
#include <stdio.h>
/**********found**********/
fun( char * str, char * substr) 改为:int fun( char * str, char * substr)
{ int i, j, k, num=0;
/**********found**********/
for(i=0,str[i],i++) 改为:for(i=0;str[i];i++)
for(j=i,k=0;substr[k]==str[j];k++,j++)
if(substr[k+1]== ‘\0’) { num++; break; }
return num;
}
main()
{ char str[80],substr[80];
printf(“ Input a string:”); gets(str);
printf(“ Input a substring:”); gets(substr);
printf(“%d\n”, fun(str,substr)); }
98、给定程序中fun函数的功能是:从N个字符串中找出最长的那个串,并将其地址作为函数值返回。各字符串在主函数中输入,并放入一个字符串数组中。
#include <stdio.h>
#include <string.h>
#define N 5
#define M 81
/**********found**********/
fun(char (*sq)[N]) 改为:char * fun(char (*sq)[M])
{ int i;
char *sp;
sp=sq[0];
for(i=0;i<N;i++)
if(strlen(sp)<strlen(sq[i])) sp=sq[i];
/**********found**********/
return sq; 改为:return sp;
}
main( )
{ char str[N][M], * longest; int i;
printf(“Enter %d lines:\n”, N);
for(i=0;i<N;i++) gets(str[i]);
printf(“\n The string :\n”,N);
for(i=0;i<N;i++) puts(str[i]);
longest=fun(str);
printf(“\n The longest string :\n”);
puts(longest); }
99、下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。给定程序中fun函数的功能是将单向链表结点(不包括头结点)数据域中的最大值,并作为函数值返回。
#include <stdio.h>
#include <stdlib.h>
typedef struct aa
{ int data; struct aa *next; }NODE;
/***********found**********/
fun(NODE *h) 改为:int fun(NODE *h)
{ int max = -1 ;
NODE *p;
p=h;
while(p)
{ if(p->data>max) max=p->data;
/***********found**********/
p=h->next; 改为:p=p->next;
}
return max;
}
outresult(int s, FILE *pf)
{ fprintf(pf, “\n The max in link: %s\n”,s); }
NODE *creatlink(int n)
{ NODE *h, *p, *s, *q;
int i, x;
h=p=(NODE *)malloc(sizeof(NODE));
for(i=1; i<=n; i++)
{ s=(NODE *)malloc(sizeof(NODE));
s->data=rand()%16; s->next=p->next;
p->next=s; p=p->next; }
p->next=NULL;
return h;
}
outlink(NODE *h, FILE *pf)
{ NODE *p;
p = h->next;
fprintf(pf ,"\n\nTHE LIST :\n\n HEAD " );
while(p) { fprintf(pf ,"->%d ",p->data ); p=p->next; }
fprintf (pf,"\n");
}
outresult(int s, FILE *pf)
{ fprintf(pf,"\nThe sum of even numbers : %d\n",s); }
main( )
{ NODE *head; int even;
head=creatlink(12); head->data=9000; outlink(head , stdout); even=fun(head);
printf("\nThe result :\n"); outresult(even, stdout); }
100、给定程序中fun函数的功能是:通过某种方式是相两个变量值的交换,规定不允许增加语句和表达式。例如:若变量a中的值原为8,b中的值为3。程序运行后a中的值为3,b中的值为8。
#include <stdio.h>
int fun(int * x,int y)
{ int t;
/*********found**********/
t=x; x=y; 改为:t=*x;*x=y;
/*********found**********/
return(y); 改为:return(t);
}
main( )
{ int a=3,b=8;
clrscr();
printf("%d, %d\n",a,b); b=fun(&a,b); printf("%d, %d\n",a,b);
}
这篇关于[C语言面试笔试]2023精选改错题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!