当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(45)
发布时间:2011/7/14 11:37:05 来源:城市学习网 编辑:ziteng
  第45套
  第一题:填空题
  请补充fun函数,该函数的功能是:先将在字符串s中的字符按逆序存放到t串中,然后把s中的字符按正序连接到t串的后面。
  例如:s中的字符串为ABCDE时,则t中的字符串应为EDCBAABCDE。
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  #include
  #include
  #include
  void fun(char  *s, char  *t)
  {
  int  s1, i;
  s1 = strlen(s);
  for (i=0; i  t[i] = s[___1___];
  for (i=0; i  t[s1+i] = s[i];
  t[___2___] = '\0';
  }
  main()
  {
  char  s[100], t[100];
  printf("\nPlease enter string s:");
  scanf("%s", s);
  fun(s, t);
  printf("The result is: %s\n", t);
  }
  参考答案:1:s1-i-1或s1-1-i
  2:2*s1或s1*2 [NextPage]   第二题:改错题
  下列给定程序中,函数fun的功能是:将s所指字符串中最后一次出现的t1所指子串替换成t2所指子串,所形成的新串放在w所指的数据中。在此处,要求t1和t2所指字符串的长度相同。
  例如,当s所指字符串中的内容为abcdabfabc,t1所指子串的内容为ab,t2所指子串中的内容为99时,结果,在w所指的数组中的内容为abcdabf99c。
  请改正程序中的错误,使它能得出正确的结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  #include
  #include
  #include
  /********found********/
  void fun(char* s, t1, t2, w)
  {
  char  *p, *r, *a;
  strcpy(w, s);
  /********found********/
  while (w)
  {
  p = w;
  r = t1;
  while (*r)
  if (*r == *p)
  {
  r++;
  p++;
  }
  else
  {
  break;
  }
  if (*r == '\0')
  a = w;
  w++;
  }
  r = t2;
  while (*r)
  {
  *a = *r;
  a++;
  r++;
  }
  }
  main()
  {
  char  s[100], t1[100], t2[100], w[100];
  printf("\nPlease enter string S:");
  scanf("%s", s);
  printf("\nPlease enter substring t1:");
  scanf("%s", t1);
  printf("\nPlease enter substring t2:");
  scanf("%s", t2);
  if (strlen(t1) == strlen(t2))
  {
  fun(s, t1, t2, w);
  printf("\nThe result is :  %s\n", w);
  }
  else
  {
  printf("\nError : strlen(t1) != strlen(t2)\n");
  }
  }
  参考答案:1:void fun(char *s,t1,t2,w)应改为
  void fun(char *s,char *t1,char *t2,char *w)
  2:while(w)应改为while(*w)
 [NextPage]   第三题:编程题
  假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:只删除字符串前导和尾部的*号,串中字母之间的*号都不删除。形参n给出了字符串的长度,形参h给出了字符串中前导*号的个数,形参e给出了字符串中最后*号的个数。在编写函数时,不得使用C语言提供的字符串函数。
  例如,若字符串的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是A*BC*DEF*G。
  请勿改动主函数main和其他函数中的任何内容,仅在函数的花括号中填入所编写的若干语句。
  参考答案:
  void fun(char *a,int n,int h,int e)
  { int i=0;
  char *p;
  for (p=a+h;p{ (a+i)=p;
  i++;
  }
  *(a+i)=’\0’;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved