0
点赞
收藏
分享

微信扫一扫

二级C语言操作例题(三十二)

钵仔糕的波波仔 2022-03-20 阅读 24

一、程序填空题

在此程序中,函数fun的功能是计算下式

直到

并将计算结果作为函数值返回。

例如,若形参e的值为1e-3,函数的返回值为2.985678。

#include  <stdio.h>

double fun(double  e)

{ int  i;    double  s, x;

/**********found**********/

  s=0;  i=__1__;

  x=1.0;

  while(x>e){

/**********found**********/

    __2__;

/**********found**********/

    x=(2.0*i-1)/((__3__)*(2.0*i));

    s=s+x;

  }

  return  s;

}

void main()

{ double  e=1e-3;

  printf("\nThe result is: %f\n",fun(e));

}

答案:(1) 0 (2) i++ (3) 2.0*i

二、程序修改题

在此程序中,函数fun的功能是:将s所指字符串的正序和反序进行连接,形成新串放在t所指的数组中。

例如,当s所指字符串为:”ABCD”时,t所指字符串中的内容应为:”ABCDDCBA”。

#include <stdio.h>

#include <string.h>

/************found************/

void fun (char  s, char  t)

{

  int   i, d;

  d = strlen(s);

  for (i = 0; i<d; i++)  t[i] = s[i];

  for (i = 0; i<d; i++)  t[d+i] = s[d-1-i];

/************found************/

  t[2*d-1] = '\0';

}

void main()

{

  char   s[100], t[100];

  printf("\nPlease enter string S:"); scanf("%s", s);

  fun(s, t);

  printf("\nThe result is: %s\n", t);

}

答案:(1) void fun (char  *s, char  *t) (2) t[2*d] = 0;

三、程序设计题

在此程序中,编写函数fun,其功能是:将s所指字符串中除了下标为奇数同时ASCII值也为奇数的字符之外,其余的所有字符全部删除,串中剩余字符所形成的一个新串放在t所指的数组中。

例如,若s所指字符串的内容为:”ABCDEFG12345”,其中字符A的ASCII码值为奇数,但所在元素的下标为偶数,因此需要删除;而字符1的ASCII码值为奇数,所在数组中的下标也为奇数,因此不应当删除,其它依此类推。最后t所指数组中的内容应为:”135”。

#include <stdio.h>

#include <string.h>

void fun(char *s, char t[])

{

}

void main()

{

  char   s[100], t[100];void NONO ();

  printf("\nPlease enter string S:"); scanf("%s", s);

  fun(s, t);

  printf("\nThe result is: %s\n", t);

  NONO();

}

void NONO ()

{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

  char s[100], t[100] ;

  FILE *rf, *wf ;

  int i ;

  rf = fopen("in.dat","r") ;

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

  for(i = 0 ; i < 10 ; i++) {

    fscanf(rf, "%s", s) ;

    fun(s, t) ;

    fprintf(wf, "%s\n", t) ;

  }

  fclose(rf) ;

  fclose(wf) ;

}

答案:

int i,j=0,n;

n=strlen(s);

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

if(i%2!=0&&s[i]%2!=0)

{

t[j]=s[i];

j++;

}

t[j]='\0';

举报

相关推荐

0 条评论