I am a teacher!

导航

计算机等级考试二级C语言程序设计专项训练题——程序修改题(二)

11、给定程序MODI1.C中规定输入的字符串全部为字母,fun函数的功能是:统计a所指字符串中每个字母在字符串中出现的次数(统计时不区分大小写),并将出现次数最高的字母输出(如果有多个相同,输出一个即可)。

例如对于字符串:dadbcdbabdb,对应的输出应为:b或d。

请改正函数fun中指定部位的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

#include<stdio.h>
#include  <stdio.h>
#include  <string.h>
void fun(char  a[])
{  int  b[26], i, n,max;
   for (i=0; i<26; i++)
/**********found**********/
      a[i] = 0;
   n= strlen(a);
   for (i=0; i<n; i++)
      if (a[i] >='a' && a[i]<='z')
/**********found**********/
         b[a[i] - 'A']++;
      else  if (a[i] >='A' && a[i]<='Z')
         b[a[i] -'A']++;
   max = 0;
   for (i=1; i<26; i++)
/**********found**********/
     if (b[max] > b[i])
       max=i;
   printf("出现次数最多的字符是 :  %c\n", max + 'a');
}
int main( )
{  
   char  a[200];
   printf("请输入一个待统计的字符串 :   ");   scanf("%s", a);
   fun(a);
   return 0;
}

12、在主函数中从键盘输入若干个数放入数组中,用0结束输入并放在最后一个元素中。下列给定程序中函数fun的功能是:计算数组元素中所有值为正数的平均值(不包括0)。

例如:数组中元素中的值依次为:39,-47,21,2,-8,15,0,则程序的运行结果为19.250000。

请改正程序中的错误,使它能得出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#include <stdio.h>
double fun ( int x[])
{
/************found************/
  int sum = 0.0;
  int c=0, i=0;
  while (x[i] != 0)
  { if (x[i] > 0) {
       sum += x[i]; c++; }
    i++;
  }
/************found************/
  sum \= c;
  return sum;
}
int 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 ));
   return 0;
}

13、下列给定程序中,函数fun的功能是:找出一个大于给定整数m且紧随m的素数,并作为函数值返回。

请改正程序中的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!

#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)
           break;
/*************found**************/
        if (k<i)
         return(i);
     }
}
int main()
{  
   int n;
   printf("\nPlease enter n: ");
   scanf("%d",&n);
   printf ("%d\n",fun(n));
   return 0;
}

14、下列给定程序中函数fun的功能是:将长整型数中各位上为偶数的数依次取出,构成一个新数放在t中。高位仍在高位,低位仍在低位。

例如,当s中的数为87653142时,t中的数:8642。

请改正程序中的错误,使它能得出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#include <stdio.h>
void fun (long  s, long *t)
{ int   d;
  long  sl=1;
    *t = 0;
    while ( s > 0)
    {  d = s%10;
/************found************/
       if (d%2=0)
       {  *t=d* sl+ *t;
          sl *= 10;
       }
/************found************/
       s \= 10;
    }
}
int main()
{  long s,t;
   printf("\nPlease enter s:"); 
   scanf("%ld", &s);
   fun(s, &t);
   printf("The result is: %ld\n", t);
   return 0;
}

15、下列给定程序的功能是:读入一个整数k(2≤k≤10000),输出它的所有质因子(即所有为素数的因子)。例如,若输入整数2310,则应输出:2,3,5,7,11。

请改正程序中的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!

#include  <stdio.h>
/*************found**************/
IsPrime(int n);
{ 
  int i,m;
  m=1;
  for(i=2;i<n;i++)
 /*************found**************/
    if!(n%i)
    {
        m=0;
        break;
    }
  return(m);
}
int main()
{ 
  int j,k;
  printf("\nPlease enter an interger number between 2 and 10000: ");
  scanf("%d",&k);
  printf("\nThe prime factor(s) of %d is(are): ",k);
  for(j=2;j<k;j++)
     if((!(k%j)) && (IsPrime(j)))
        printf("%4d,",j);
  printf("\n");
  return 0;
}

16、下列给定程序中,函数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)  {r++; p++}
       else  break;
/*************found**************/
       if(r=='\0')
         n++;
       s++;
 }
 return n;
}
int 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);
    return 0;
}

17、下列给定程序中函数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>
/************found************/
#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);
}
int 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);
   return 0;
}

18、下列给定程序中函数fun的功能是:从整数10到55之间,查找能被3整除且有一位上的数值是5的数,把这些数放在b所指的数组中,这些数的个数作为函数值返回。规定函数中al放个位数,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;
 }
int 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"); return 0; }

19、下列给定程序中,函数fun的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码值升序排序后输出。例如,若输入"edcba",则应输出"abcde"。

请改正程序中的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!

#include  <string.h>
#include  <stdio.h>
void fun(char  t[])
{
 char c;
 int i,j;
 /*************found**************/
 for(i=strlen(t);i;i--)
     for(j=0;j<i;j++)
        /*************found**************/
      if(t[j]<t[j+1])
          {
           c= t[j];
           t[j]=t[j+1];
           t[j+1]=c;
          }
}
int main()
{
 char  s[81];
 printf("\nPlease  enter a character string :");
 gets(s);
 printf("\nBefore sorting : %s\n",s);
 fun(s);
 printf("\nAfter sorting decendingly: %s\n",s);
 return 0;
}

20、下列给定程序中函数fun的功能是:删除指针p所指字符串中的所有空白字符(包括制表符、回车符及换行符)。

输入字符串时用'#'结束输入。

请改正程序中的错误,使它能输出正确的结果。

注意:不得增行或删行,也不得更改程序的结构!

#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++)
      if(!isspace(*(p+i))) c[t++]=p[i];
/************found************/
   c[t]="\0";
   strcpy(p,c);
}

int 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);
   return 0;
}

 

posted on 2022-11-16 05:32  aTeacher  阅读(314)  评论(0编辑  收藏  举报