江苏省全国计算机等级考试二级C语言2009上机模拟5
一、填空题
请补充main函数,该函数的功能是:从键盘输入一个长整数,如果这个数是负数,则取它的绝对值,并显示出来。例如,输入:-666,结果为:666。
仅在横线上填写所需的若干表达式或语句,请勿改动函数中的其他任何内容。
注意:部分源程序给出如下。
# include
# include
main()
{
  long int a;
  printf("Enter the data:\n");
  scanf(___1___);
  printf("The origial data is %ld\n",a);
  if(a<0)
    ___2___;
  printf("\n");
  printf(___3___);
}
二、改错题
下列给定程序中函数fun的功能是:求出在字符串中最后一次出现的子字符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为NULL。
例如,当字符串中的内容为abcdabfabcdx,t中的内容为ab时,输出结果应是:abcdx。当字符串中的内容为abcdabfabcdx,t中的内容为abd时,则程序输出未找到信息"not found!"。
请修改程序中的错误,使程序能得出正确的结果。
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。
# include
# include
# include
char *fun(char *str,char *t)
{
  char *p,*r,*s;
  /********error********/
  s=NuLL;
  while(*str)
  {
    p=str;
    r=t;
    while(*r)
      /*******error*********/
      if(r==p)
      {
        r++;
        p++;
      }
      else
      {
        break;
      }
    if(*r=='\0')
      s=str;
    str++;
  }
  return s;
}
main()
{
  char str[100],t[100],*p;
  printf("\nplease enter string s:");
  scanf("%s",str);
  printf("\nplease enter substring t:");
  scanf("%s",t);
  p=fun(str,t);
  if(p)
    printf("\nthe result is:%s\n",p);
  else
    printf("\nnot found!\n");
}
三、编程题
编写函数int fun(int mm,int b[MAX]),该函数的功能是求出小于或等于mm的所有素数,并放在b数组中,该函数返回所求出的素数的个数。
请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
注意:部分源程序给出如下。
# include
# include
# define MAX 100
int fun(int mm,int b[MAX])

 
}
main()
{
  int m,i,sum;
  int b[MAX] ;
  FILE *out;
  printf("input a data:\n");
  scanf(" %d",&m);
  sum=fun(m,b);
  for(i=0 ;i  {
    if(i==0 && i!=0)
      printf("\n");
    printf("]",b[i]);
  }
  sum=fun(28,b);
  out=fopen("outfile.dat","w");
  for(i=0 ;i    fprintf(out,"%d\n",b[i]);
  fclose(out);
}