当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(38)
发布时间:2011/7/14 11:25:48 来源:城市学习网 编辑:ziteng
  第38套
  填空题
  请补充fun函数,该函数的功能是判断一个数的个位数字和百位数字之和是否等于其十位上的数字,是则返回“yes!“,是否返回”no!”.
  注意:部分源程序给出如下
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  试题程序:
  #include <stdio.h>
  #include <conio.h>
  char *fun(int  n)
  {
  int  g, s, b;
  g = n%10;
  s = n/10%10;
  b = ___1___;
  if ((g+b) == s)
  return ___2___;
  else
  return ___3___;
  }
  main()
  {
  int  num = 0;
  printf("******Input data *******\n ");
  scanf("%d", &num);
  printf("\n\n\n");
  printf("****** The result *******\n ");
  printf("\n\n\n%s", fun(num));
  }
  第1处填空:n/100%10
  第2处填空:”yes!”
  第3处填空:”no!” [NextPage]   改错题
  下列给定程序中,函数fun的功能是:通过某种方式实现两个变量值的交换,规定下允许增加语句和表达式。例如变量a中的值原为8,b中的值原为3,程序运行后a中的值为3,b的值为8。
  请改正程序中的错误,使其能得出正确结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  试题 程序:
  #include <conio.h>
  #include <stdio.h>
  int fun(int  *x, int  y)
  {
  int  t;
  /********found********/
  t = x;  x = y;
  /********found********/
  return (y);
  }
  main()
  {
  int a = 3, b = 8;
  printf("%d  %d\n", a, b);
  b = fun(&a, b);
  printf("%d  %d\n", a, b);
  }
  第1处:t=x;x=y;应改为t=*x;*x=y;
  第2处:return(y);应改为return(t);或return t;
 [NextPage]   编程题
  请编写函数FUN,它的功能是:求出SS所指字符串中指定字符的个数,并返回此值。
  例如,若输入字符串123412132,输入字符1,则输出3。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:#include <conio.h>
  #include <stdio.h>
  #include <string.h>
  #define  M 81
  int fun(char *ss, char c)
  {
  }
  main()
  {
  char  a[M],  ch;
  FILE *out;
  printf("\nPlease enter a string:");
  gets(a);
  printf("\nPlease enter a char:");
  ch = getchar();
  printf("\nThe number of the char is: %d\n", fun(a, ch));
  out=fopen ("out.dat", "w");
  strcpy(a, "The number of the char is: ");
  fprintf(out, "%d", fun(a, ' '));
  fclose (out );
  }
  答案是:
  int fun(char *ss,char c)
  {
  int n=0;
  while(*ss)
  {
  if(*ss==c)
  n++;
  ss++;
  }
  return n;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved