程序修改题(21-30)

第二十一题

题目

给定程序modi1.c中,函数fun的功能是:计算n!

例如,给n输入5,则输出120.000000。

#include <stdio.h>

double fun ( int n )
{ double result = 1.0 ;
/************found************/
  if n = = 0
    return 1.0 ;
  while( n >1 && n < 170 )
/************found************/
    result *= n--
  return result ;
}

main ( )
{ int n ;
  printf("Input N:") ;
  scanf("%d", &n) ;
  printf("\n\n%d! =%lf\n\n", n, fun(n)) ;
  getchar();
}

解析

if (n = = 0)

result *= n--;

第二十二题

题目

给定程序modi1.c是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数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;
  while(p)
  {  if(p->data%2==0)
     sum +=p->data;
/***********found**********/
     p=h->next;
  }
  return  sum;
}
NODE  *creatlink(int  n)
{  NODE  *h, *p, *s;
   int  i;
   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);
   getchar();
}

解析

p=h->next;

p=p->next;

第二十三题

题目

给定程序modi.c中函数fun的功能是:在字符串的最前端加入n个*号,形成新串,并且覆盖原串。

注意:字符串的长度最长允许为79。

#include <stdio.h>
#include <string.h>
void  fun (  char  s[], int  n )
{
   char  a[80] , *p;
   int  i;
/**********found***********/
   s=p;
   for(i=0; i<n; i++)  a[i]='*';
   do
   {  a[i]=*p;
      i++;
   }
/**********found***********/
   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 insert : \"%s\" \n" ,s);
   getchar();
}

解析

p=s;

while(*p++);

第二十四题

题目

给定程序modi1.c中函数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];
/**********found**********/
        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 );
  getchar();
}

解析

s[j++]=s[i];

s[j]='\0';

第二十五题

题目

给定程序modi1.c中函数fun的功能是:对N名学生的学习成绩,按从高到低的顺序找出前m(m≤10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。

#include <stdio.h>
#include <stdlib.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)
   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);
         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);
  getchar();
}

解析

t=(STU *)calloc(sizeof(STU),m);

*(t+k)=b[j];

第二十六题

题目

给定程序modi1.c是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数fun的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。

#include <stdio.h>
#include <stdlib.h>
typedef  struct  aa
{  int  data;
   struct  aa  *next;
} NODE;
int fun (  NODE  *h )
{ int  max=-1;
  NODE  *p;
/***********found**********/
  p=h ;
  while(p)
  {  if(p->data>max )
              max=p->data;
/***********found**********/
     p=h->next ;
  }
  return  max;
}
outresult(int  s, FILE *pf)
{  fprintf(pf,"\nThe max in link  :  %d\n",s);}

NODE  *creatlink(int  n, int  m)
{  NODE  *h, *p, *s;
   int  i;
   h=p=(NODE *)malloc(sizeof(NODE));h->data=9999;
   for(i=1; i<=n; i++)
   {  s=(NODE *)malloc(sizeof(NODE));
      s->data=rand()%m;  s->next=p->next;
      p->next=s;         p=p->next;
   }
   p->next=NULL;
   return  h;
}
outlink(NODE  *h, FILE  *pf)
{  NODE  *p;
   p=h->next;
   fprintf(pf,"\nTHE  LIST :\n\n  HEAD ");
   while(p)
   {  fprintf(pf,"->%d ",p->data); p=p->next; }
      fprintf(pf,"\n");
   }
main()
{  NODE  *head;  int  m;
   head=creatlink(12, 100);
   outlink(head , stdout);
   m=fun(head);
   printf("\nTHE  RESULT  :\n"); outresult(m, stdout);
   getchar();
}

解析

p=h->next;

p=p->next ;

第二十七题

题目

给定程序modi1.c中函数fun的功能是:判断一个整数是否是素数,若是返回1,否则返回0。在main()函数中,若fun返回1输出YES,若fun返回0输出NO!。

#include <stdio.h>

int  fun ( int m )
{  int k = 2;
   while ( k <= m && (m%k))
/************found************/
     k++
/************found************/
   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" );
  getchar();
}

解析

k++;

if (m == k )

第二十八题

题目

给定程序MODI1. C中,函数fun的功能是:分别统计出形参str所指的字符串中的大写字母

和小写字母的个数,并传递回主函数输出。

例如:若str所指的内容为”BAY23Kill",其中大写字母数为4,小写字母数为3,

则应输出: c0=4, c1=3。

#include <stdio.h>
#include <string.h>
#pragma warning (disable:4996)
void fun(char* str, int *c0, int *c1) {  
	int k;
/**********found**********/
	c0 = c1 = 0;
/**********found**********/
	for (k=1; k<strlen(str); k++) {
/**********found**********/
		if ( (str[k] >= 'A') && (str[k] <= 'Z') ) *c0++;
		if ( (str[k] >= 'a') && (str[k] <= 'z') ) (*c1)++;
	}
}

main()
{ char str[100]; int c0,c1;
  printf("input string:");
  scanf("%s", str);
  fun(str, &c0, &c1);
  printf("c0=%d,c1=%d\n", c0, c1);
}

解析

*c0 = *c1 = 0;

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

if ( (str[k] >= 'A') && (str[k] <= 'Z') ) (*c0)++;

第二十九题

题目

给定程序modi1.c中,函数fun的功能是:求三个数的最小公倍数。

例如,给变量x1、x2、x3分别输入15 11 2,则输出结果应当是:330。

#include <stdio.h>
/************found************/
fun(int  x, y, z )
{  int  j,t ,n ,m;
   j = 1 ;
   t=j%x;
   m=j%y ;
   n=j%z;
   while(t!=0||m!=0||n!=0)
   {  j = j+1;
      t=j%x;
      m=j%y;
      n=j%z;
   }
/************found************/
   return i;
}
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);
  getchar();
}

解析

int fun(int  x,int y,int z )

return j;

第三十题

题目

给定程序modi1.c中,函数fun的功能是:统计字符串中各元音字母(即:A、E、I、O、U)的个数。注意:字母不分大、小写。

例如:若输入:THIs is a boot,则输出应该是:1、0、2、2、0。

#include <stdio.h>
void fun ( char   *s,  int   num[5] )
{  int  k, i=5;
   for ( k = 0; k<i; k++ )
/**********found**********/
     num[i]=0;
   for (; *s; s++)
   { i = -1;
/**********found**********/
    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");
   getchar();
}

解析

num[k]=0;

switch ( *s )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值