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

一、程序填空题

在此程序中,函数fun的功能是:根据形参i的值返回某个函数的值。当调用正确时,程序输出:x1=5.000000,x2=3.000000,x1*x1+x1*x2=40.000000。

#include  <stdio.h>

double f1(double  x)

{  return x*x;  }

double f2(double  x, double  y)

{  return  x*y;  }

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

__1__ fun(int  i, double  x, double  y)

{ if (i==1)

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

    return __2__(x);

  else

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

    return  __3__(x, y);

}

void main()

{ double  x1=5, x2=3, r;

  r = fun(1, x1, x2);

  r += fun(2, x1, x2);

  printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n\n",x1, x2, r);

}

答案:(1) double (2) f1 (3) f2

二、程序修改题

在此程序中,函数fun的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。

#include <conio.h>

#include <stdio.h>

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

double fun(char *s,char *t)

{

  int s1=0,t1=0;

  char *ss,*tt;

  ss=s;

  tt=t;

  while(*ss)

  {

s1++;

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

(*ss)++;

  }

    while(*tt)

  {

t1++;

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

(*tt)++;

  }

  if(t1>s1)

return t;

  else

return s;

}

void main()

{

  char a[80],b[80];

  printf("\nEnter a string : ");

  gets(a);

  printf("\nEnter a string again: ");

  gets(b);

  printf("\nThe longer is :\n\n%s\n", fun(a,b));

}

答案:(1) char *fun( char *s,char *t) (2) ss++; (3) tt++;

三、程序设计题

在此程序中,编写函数fun,其功能是:移动字符串中的内容,移动的规则是把第1~m个字符,平移到字符串的最后,把m+1到最后的字符移到字符串的前部。

例如,字符串中原有的内容为”ABCDEFGHIJK”,m的值为3,移动后,字符串中的内容应该是”DEFGHIJKABC”。

#include <stdio.h>

#include <string.h>

#define   N    80

void fun (char  *w,int  m)

{

  

}

void main()

{

  FILE *wf;

  char  a[N]= "ABCDEFGHIJK",b[N]= "ABCDEFGHIJK";

  int  m;

  printf("The origina string :\n");

  puts(a);

  printf("\n\nEnter  m: ");

  scanf("%d",&m);

  fun(a,m);

  printf("\nThe string after moving :\n");

  puts(a);

  printf("\n\n");

/******************************/

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

  fun(b,3);

  fprintf(wf,"%s",b);

  fclose(wf);

/*****************************/

}

答案:

int i,j;

char t;

for(i=1;i<=m;i++)             /*进行m次的循环左移*/

{t=w[0];

for(j=1;w[j]!='\0';j++)        /*从第2个字符开始以后的每个字符都依次前移一个字符*/

w[j-1]=w[j];

  w[j-1]=t;                  /*将第1个字符放到最后一个字符中*/

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值