首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 二级考试 >

计算机二级C语言上机题考前预测(附答案)(1)(3)

2013-03-26 
计算机二级C语言上机题考前预测(附答案)(1)

  编程题

  编写一个函数,从传入的num个字符串中找出最长的一个字符串,传回该串地址(用****作为结束输入的标志)。

  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中秘编写的若干语句。

  #include

  #include

  #include

  char *fun(char (*a)[81],int num)

  {int i;

  char *max;

  max=a[0];

  for(i=0;i<=num;i++)

  if(strlen(max)

  max=a[i];

  return max;

  }

  main()

  {

  char ss[10][81],*max;

  int n,i=0;

  FILE *out;

  printf("输入若干个字符串:");

  gets(ss[i]);

  puts(ss[i]);

  while(!strcmp(ss[i],"****")==0)

  {

  i++;

  gets(ss[i]);

  puts(ss[i]);

  }

  n=i;

  max=fun(ss,n);

  printf("\nmax=%s\n",max);

  out=fopen ("out.dat", "w");

  strcpy(ss[0], "Oh,");

  strcpy(ss[1], "you");

  strcpy(ss[2], "want");

  strcpy(ss[3], "some");

  strcpy(ss[4], "too?!?");

  fprintf(out, "%s", fun(ss, 5));

  fclose (out );

  }

  参考答案:

  char *fun(char (*a)[81],int num)

  {int i;

  char *max;

  max=a[0];

  for(i=0;i<=num;i++)

  if(strlen(max)

  max=a[i];

  return max;

  }

更多关注:

2013上半年重庆计算机软件专业技术资格考试

2013年广西计算机应用能力考试1月份考试

2012年下半年西藏软件水平考试成绩查询入口

更多精彩请关注读书人网计算机频道!


热点排行