当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(24)
发布时间:2011/7/8 11:34:48 来源:城市学习网 编辑:ziteng
  第24套
  填空题
  函数FUN的功能是:从三个形参a,b,c中找出中间的那个数,作为函数值返回。
  例如,当a=3,b=5,c=4时,中数为4。
  注意:部分源程序给出如下
  请勿改动主函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
  试题程序:#include <stdio.h>
  int fun(int  a, int  b, int  c)
  {
  int  t;
  t = (a > b) ? (b>c ? b : (a>c ? c : ___1___)) :
  ((a > c) ? ___2___ :
  ((b > c) ? c : ___3___));
  return t;
  }
  main()
  {
  int  a1 = 3, a2 = 5, a3 = 4, r;
  r = fun(a1, a2, a3);
  printf("\nThe middle number is  :  %d\n", r);
  }
  第1处填空:a
  第2处填空:a
  第3处填空:b [NextPage]  改错题
  下列给定程序中,函数fun的功能是:从s所指字符串中,找出t所指子串的个数作为函数值返回,例如,当s所指字符串中的内容为abcdabfab,t 所指字符串的内容为ab,则函数返回整数3。
  请改正程序中的 错误,使它能得也正确的结果,
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  试题 程序:
  #include <conio.h>
  #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;
  }
  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);
  }
  第1处:if (r==p)应改为if(*r==*p)
  第2处:if(r==’\0’)应改为if(*r==’\0’)
  [NextPage]   编程题
  编写函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用C语言中提供的将字符串转换成整 数的函数。)
  例如 若输入字符串“-1234“,则 函数把它转换为整数值-1234。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:#include <stdio.h>
  #include <string.h>
  long fun ( char *p)
  {
  }
  main()
  {
  char s[6];
  long    n;
  FILE *out;
  char *test[] = {"-1234", "5689", "7102", "-4356"};
  printf("Enter a string:\n");
  gets(s);
  n = fun(s);
  printf("%ld\n",n);
  out=fopen("out.dat", "w");
  for(n=0;n<4;n++)
  fprintf(out, "%ld\n", fun(test[n]));
  fclose(out);
  }
  答案是:
  long fun(char *p)
  {
  long s=0,t;
  int i=0,j,n=strlen(p),k,s1;
  if(p[0]==’-’)
  i++;
  for(j=I;j<=n-1;j++)
  {
  t=p[j]-‘0’;
  s1=10;
  for(k=j;k<n-1;k++)
  t*=s1;
  s+=t;
  }
  if(p[0]==’-’)
  return –s;
  else
  return s;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved