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)); }