程序修改题(51-64)

第五十一题

题目

给定程序modi1.c中函数fun的功能是:从整数10到55之间,选出能被3整除、且有一位上的数是5的那些数,并把这些数放在b所指的数组中,这些数的个数作为函数值返回。规定,函数中a1放个位数,a2放十位数。

#include <stdio.h>
int fun( int  *b )
{  int   k,a1,a2,i=0;
   for(k=10; k<=55; k++) {
/************found************/
      a2=k/1O;
      a1=k-a2*10;
      if((k%3==0 && a2==5)||(k%3==0 && a1==5))
      {  b[i]=k; i++; }
   }
/************found************/
   return  k;
 }
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");
  getchar();
}

解析

a2=k/10;

return  i;

第五十二题

题目

给定程序modi1.c中函数fun的功能是:先将s所指字符串中的字符按逆序存放到t所指字符串中,然后把s所指串中的字符按正序连接到t所指串的后面。

例如:当s中的字符串为:"ABCDE"时,则t中的字符串应为:"EDCBAABCDE"。

#include <stdio.h>
#include <string.h>

void fun (char  *s, char  *t)
{
/************found************/
    int   i;
    i=0;
    sl = strlen(s);
    for (; i<sl; i++)
/************found************/
       t[i] = s[sl-i];
    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);
   getchar();
}

解析

int   i,sl;

t[i] = s[sl-i-1];

第五十三题

题目

给定程序MODI1.C中,函数fun()的功能是统计s所指维数组中0的个数(存在变量zero中)和1的个数(存在变量one中),并输出结果。

#include  <stdio.h>
void  fun( int  *s, int  n )
{
/**********found**********/
   int  i,  one=0, zero ;
   for(i=0; i<n; i++)
/**********found**********/
   switch( s[i] );
   {
/**********found**********/
      case  0  :  zero++;
      case  1  :  one ++;
   }
   printf( "one : %d    zero : %d\n", one,  zero);
}
main()
{  int  a[20]={1,1,1,0,1,0,0,0,1,0,0,1,1,0,0,1,0,1,0,0}, n=20;
   fun( a,  n );
  getchar();
}

解析

int  i,  one=0, zero=0 ;

switch( s[i] )

      case  0  :  zero++;

break;
      case  1  :  one ++;

第五十四题

题目

给定程序MODI1.C中,fun函数的功能是:删除b所指数组中小于10的数据。主函数中输出删除后数组中余下的数据。

#include  <stdio.h>
#include  <stdlib.h>
#define   N  20
int  fun( int *b )
{
/**********found**********/
   int  t[N] ,i, num
   for(i=0; i<N; i++)
     if(b[i]>=10)
/**********found**********/
       t[++num]=b[i];
/**********found**********/
    for(i=0; i<nun; i++)
      b[i]=t[i];
    return( num );
}
main()
{  int  a[N],i,num;
   printf("a数组中的数据 :\n");
   for(i=0;i<N ;i++) {a[i]=rand()%21; printf("%4d",a[i]);}
   printf("\n");
   num=fun(a);
   for(i=0;i<num ;i++) printf("%4d",a[i]);
   printf("\n");
  getchar();
}

解析

int  t[N] ,i, num=0;

t[num++]=b[i];

for(i=0; i<num; i++)

第五十五题

题目

给定程序MODI1.C中,函数fun的功能是:在任意给定的N个正整数中,从左到右依次逐个取三个数作为一组,按值大小找出该组数的中值,用该中值替换与该组数对应的原三个数中的中间位置的数。处理后原数列中首尾2个数不变。处理后数列在主函数中输出。例如,有10个正整数如下:

初始数列为:6 5 7 23 18 5 8 21 45 38

第1组数为:6 5 7 中值为:6 替换后的数列为:6 6 7 23 18 5 8 21 45 38

第2组数为:5 7 23 中值为:7 替换后的数列为:6 6 7 23 18 5 8 21 45 38

第3组数为:7 23 18中值为:18替换后的数列为: 6 6 7 18 18 5 8 21 45 38

第4组数为:28 18 5中值为:18替换后的数列为:6 6 7 18 18 5 8 21 45 38

第5组数为: 18 5 8中值为:8 替换后的数列为:6 6 7 18 18 8 8 21 45 38

第6组数为: 5 8 21中值为:8 替换后的数列为:6 6 7 18 18 8 8 21 45 88

第7组数为:8 21 45中值为:21替换后的数列为:6 6 7 18 18 8 8 21 45 38

第8组数为:21 45 38中值为:38替换后的数列为: 6 6 7 18 18 8 8 21 38 38

最终结果为:6 6 7 18 18 8 8 21 38 38

#include  <stdio.h>
#define   N   10
int findmid(int  a, int  b, int  c)
{  int  t;
   t = (a>b)?(b>c?b:(a>c?c:a)):((a>c)?a:((b>c)?c:b));
/**********found**********/
   return  b;
}
void fun(int  x[])
{  int  i,a,b,c,t[N];
/**********found**********/
   for(i=0;i<N;i++) t[i]=x[i]
   for(i=0;i<N-2;i++)
   {  a=t[i];b=t[i+1];c=t[i+2];
/**********found**********/
      t[i+1]=findmid(a,b,c);
   }
}
main()
{  int  i, x[N]={6,5,7,23,18,5,8,21,45,38};
   for(i=0; i<N; i++)  printf("%d ",x[i]);
   printf("\n");
   fun(x);
   for(i=0; i<N; i++)  printf("%d ",x[i]);
   printf("\n");
  getchar();
}

解析

return  t;

for(i=0;i<N;i++) t[i]=x[i];

t[i+3]=findmid(a,b,c);

第五十六题

题目

#include <math.h>
#include <stdio.h>
double fun(double  eps)
{  double  s,t;     int  n=1;
   s=0.0;
/************found************/
   t=0;
   while( t>eps)
   {  s+=t;
      t=t * n/(2*n+1);
      n++;
   }
/************found************/
   return(s);
}
main()
{  double  x;
   printf("\nPlease enter a precision: "); scanf("%lf",&x);
   printf("\neps=%lf, Pi=%lf\n\n",x,fun(x));
  getchar();
}

解析

t=1.0;

return (2*s);

第五十七题

题目

给定程序modi1.c的功能是:读入一个英文文本行,将其中每个单词的第一个字母改成大写,然后输出此文本行(这里的"单词"是指由空格隔开的字符串)。

例如,若输入: I am a student to take the examination.,则应输出:工 Am A Student To Take The Examination.。

#include <ctype.h>
#include <string.h>
/************found************/
include  <stdio.h>
/************found************/
void 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 );
    getchar();
}

解析

#include  <stdio.h>

void upfst ( char  *p )

第五十八题

题目

给定程序MODI1.C中函数fun的功能是:将形参str所指字符串中的数字字符按从前往后出

现的次序从左往右拼接成一个整数,返回主函数输出。若形参str中没有数字,

则函数fun返回0给主函数。

例如:若str的内容为”C3Xyz9D1",则函数返回391,程序输出391。

#include <stdio.h>
#include <string.h>

int fun(char* str) 
{  
	int i, k;

	i = 0;
/**********************found***********************/
	for (k=0; k<=strlen(str); k++) 
	{
/**********************found***********************/
		if ( (str[k] >= 0) && (str[k] <= 9) ) 
			 i = i * 10 + str[k] - '0';
	}
/**********************found***********************/
	return k;
}

int main()
{ 
	char str[100];
	int l;
	printf("input string:");
	scanf("%s", str);
	l = fun(str);
	printf("num:%d\n", l);
	return 0;
}

解析

for (k=0; k<strlen(str); k++)

if ( (str[k] >= '0') && (str[k] <= '9') )

return i;

第五十九题

题目

#include <stdio.h>

/************found************/
void 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;
       c = a;  a = a + b; b = c;
    }
    return s;
}

main( )
{  int   n = 5;
   printf( "\nThe value of  function is: %lf\n",  fun (  n ) );
   getchar();
}

解析

double fun (  int  n )

s = s + (double)a / b;

第六十题

题目

#include <stdio.h>
#include <math.h>

/************found************/
void fun( int  k )
{  int n; double s,  w, p, q;
   n = 1;
   s = 1.0;
/************found************/
   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
}

main ( )
{
   printf("%f\n", fun (10));
  getchar();
}

解析

double fun( int  k )

while ( n <= k )

 return  s;

第六十一题

题目

给定程序MODI1.C中的函数int strtoval(char pst[],double * p)的功能是,将pst中带符号的数字字符串转换成双精度数值,直到遇到非数字字符为止。函数返回值为0表示转换成功,为1表示转换失败,函数成功转换后的数值存储在指针p所指的存储单元中。

如: +123.65a转换数值后输出+123.650000,函数返回值为0。

-32-3转换数值后输出-32.000000,函数返回值为0。

56.76a转换数值后输出+56.760000,函数返回值为0。

+283.125转换数值后输出+283.125000,函数返回值为0。

对+a8.6转换失败,函数返回值为1,输出出错信息。

#include<stdio.h>
int strtoval(char pst[],double* p)
{
	int sucs;
	int plus;
	int i=0;
	double frac=0.1;
	*p=0.0;
	while(pst[i]==' ') i++;
/**********************found***********************/
	if(pst[i]=='+'&& pst[i]>='0'&&pst[i]<='9')
	{
		plus=1;
		sucs=1;
	}
	else if(pst[i]=='-')
	{
		plus=0;
		sucs=1;
	}
	else 
	{
		sucs=0;
		return sucs;
	}
/**********************found***********************/
	if(pst[i]=='+'&& pst[i]=='-')
		i++;
	if(pst[i]==' ')
		i++;
	if(!(pst[i]>='0'&&pst[i]<='9'))
	{
		sucs=0;
		return sucs;
	}
	while(pst[i]!='\0')
	{
		if(pst[i]>='0'&&pst[i]<='9')
		{
			*p=*p*10+pst[i]-'0';
			i++;
		}
		else if(pst[i]== '.')
			break;
		else 
		{
			if(plus==0)
				*p=-1.0**p;
			return sucs;
		}
	}
/**********************found***********************/
	if(pst[i]!= '.')
	{
		i++;
		while(pst[i]!='\0')
		{
			if(pst[i]>='0'&&pst[i]<='9')
			{
				*p=*p+(pst[i]-'0')*frac;
				frac/=10;
				i++;
			}
			else
			{
				if(plus==0)
					*p=-1.0**p;
				return sucs;
			}
		}
		return sucs;
         }
         return sucs;
}

main()
{
	char str[][100]={
		"    +123.65a",
		"    -32-3",
		"    56.76a",
		"    + 283.125",
		"    +a8.6"		
	};
	double a;
	int i;
	for(i=0;i<5;i++)
		if(strtoval(str[i],&a)==1)
			printf("vol=%f\n",a);
		else 
			printf("error,the string is %s\n",str[i]);
}

解析

if(pst[i]=='+'||(pst[i]>='0'&&pst[i]<='9'))

if(pst[i]=='+'|| pst[i]=='-')

if(pst[i]+= '.')

第六十二题

题目

在给定程序MODI.C中,函数fun的功能是,将形参str中混杂的数字按从前往后出现的次序从左往右拼接成一个整数,用该整数减去str中所有数字之和,结果返回主函数输出。若形参str中没有数字,则函数fun返回0给主函数。

例如:若str的内容为“A3Kym9E1”,则抽取的数字为391,所有数字之和为13,则函数返回378,程序输出378。

#include <stdio.h>
#include <string.h>

int fun(char* str) 
{  
  int i, k, s;

  i = 0;
  s = 0;
/**********************found***********************/
  for (k=0; k<=strlen(str); k++) 
  {
/**********************found***********************/
    if ( (str[k] >= 0) && (str[k] <= 9) ) 
	  {
		i = i * 10 + str[k] - '0';
		s = s + str[k] - '0';
	  }
  }

/**********************found***********************/
  return k;
}

int main()
{ 
  char str[100];
  int l;
  printf("input string:");
  scanf("%s", str);
  l = fun(str);
  printf("num:%d\n", l);
  return 0;
}

解析

for (k=0; k<strlen(str); k++) 

if ( (str[k] >= '0')&&(str[k]<='9'))

return i-s;

第六十三题

题目

在给定程序M0DI1.C中,函数fun的功能是:在长度为N的递增顺序排列的成绩数组score中,用二分法查找成绩。若找到则输出该成绩所在的位置:否则输出“XX成绩没有找到!”的提示。

#include <stdio.h>
#define N 12

int fun(int score[], int x)
{
    int low=0,high=N-1,mid;
    while(low<=high)
    {   mid=(low+high)/2;
        if(x==score[mid])
/**********found**********/
            return (low);
        else if(x>score[mid])
/**********found**********/
            high=mid+1;
        else
/**********found**********/
            low=mid-1;
    }
    return -1;
}

void main()
{
    int score[N] ={55,58,65,70,78,81,83,84,87,93,95,97},x,y;
    printf("输入要查找的学生成绩数据:\n");
    scanf("%d", &x);
    y=fun(score, x);
    if(y==-1)
        printf("%d成绩没有找到!\n", x);
    else
        printf("%d成绩找到,下标为%d的位置上\n",x,y);
}

解析

return (low+high)/2;

low=mid+1;

high=mid-1;

第六十四题

题目

在给定程序MODI1.C中,函数fun的功能是将N x N的二维数组,按下面样例所示规律进行赋值,例如:当N=4时,所得二维数组的内容应为:

1 1 1 1

1 2 2 1

1 2 2 1

1 1 1 1

当N=5时,所得二维数组的内容应为:

1 1 1 1 1

1 2 2 2 1

1 2 3 2 1

1 2 2 2 1

1 1 1 1 1

#include <stdio.h>
#define N 8
void fun(int a[N][N])
{
	int i,j,k=1,t;
/**********************found***********************/
	t=1;
	/**********************found***********************/
	while(t<N/2)
	{
			for(i=t;i<N-t;i++)  
			for(j=t;j<N-t;j++)
	/**********************found***********************/
				a[i][j]=k++;
		k++;
		t++;	
	}
}
main()
{
	int i,j;
	int a[N][N]={0};
  	fun(a);
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
			printf("%5d",a[i][j]);
		printf("\n");
	}
 }


解析

t=0;

while(t<(N+1)/2)

a[i][j]=k;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值