程序填空题(1-10)

第一题

题目

编写一个函数void fun(char c, int d),其功能是:根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,输出字母的大小写与形参c一致,数量由形参d指定。若输出字母中有字母Z或z,则应从A或a开始接续,直到输出指定数量的字母。

例如: c为'Y’,d为4,则程序输出ZABC; c为’z’,d为2,则程序输出ab。

解析

void fun(char c, int d) { 
  int i;
  if ((c >= 'a') && (c <= 'z'))
	  for(i=1;i<=d;i++)
		  printf("%c",(c-'a'+i)%26+'a');  
  if ((c >= 'A') && (c <= 'Z'))
	  for(i=1;i<=d;i++)
		  printf("%c",(c-'A'+i)%26+'A');
}
void fun(char c, int d) { 
  int i;
  char A[26], a[26], *ptr;
/**********found**********/
  for (i=0; i<26; i++) {
	A[i] = 'A' + i;
	a[i] = 'a' + i;
  }
/**********found**********/
  if ((c >= 'a') && (c <= 'z')) 
	  ptr = a;
  else   ptr = A;
/**********found**********/
  for (i=1; i<=d; i++) 
	  printf("%c", ptr[(c-ptr[0]+i) % 26] );
}

分析

观察发现解这道题的关键点有两个

1、输出字母的大小写与形参c一致

2、输出字母中有字母Z或z,则应从A或a开始接续

对于1、来说很简单通过if语句就能实现,对于2、来说就不是很好想了,它们都是通过对26取余的方式,来实现从‘Z’、‘z’到‘A’、‘a’的循环。

第二题

题目

程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是对形参b所指结构体变量中的数据进行修改,最后在主函数中输出修改后的数据。 例如:b所指变量t中的学号、姓名和三门课的成绩依次是:10002、"ZhangQi"、93.00、85.00、87.00,修改后输出t中的数据应为: 10004、"LiJie"、93.00、85.00、87.00。

#include  <stdio.h>
#include  <string.h>
struct student {
  long  sno;
  char  name[10];
  float  score[3];
};
void fun( struct student  *b)
{
/**********found**********/
  (1) = 10004;
/**********found**********/
  strcpy((2), "LiJie");
}
main()
{ struct student  t={10002,"ZhangQi", 93, 85, 87};
  int  i;
  printf("\n\nThe original data :\n");
  printf("\nNo: %ld  Name: %s\nScores:  ",t.sno, t.name);
  for (i=0; i<3; i++)  printf("%6.2f ", t.score[i]);
  printf("\n");
/**********found**********/
  fun((3));
  printf("\nThe data after modified :\n");
  printf("\nNo: %ld  Name: %s\nScores:  ",t.sno, t.name);
  for (i=0; i<3; i++)  printf("%6.2f ", t.score[i]);
  printf("\n");
}

解析

(1)b->sno(2)b->name(3)&t

分析

通读题目我们知道在(1)(2)分别要将常量“10004”、“lijie”赋值给形参b中的成员“sno”、“name”,所以答案为(1)b->sno(2)b->name,这里要注意这个形参b是结构体指针变量,其实质上还是一个指针,所以在引出其结构体成员时要用到“->”。假如形参是结构变量,在引出其结构体成员时要用到".”,像这样"b.sno"

对于(3),我们要注意void fun( struct student  *b)中,形参b所指的是变量的地址,所以我们这里要填的是"fun(&t)",即取结构体变量t的地址;而非是结构体变量t本身,即fun(t)

第三题

题目

下列给定程序中,函数fun的功能是:根据形参n计算并返回阶乘n!。为避免每次阶乘都要从1开始乘起,程序中使用了静态结构体变量old保存上次计算的阶乘。每次计算时,要比较新的参数n与上次计算的阶数n,以便决定计算方法。

struct Ord
{
	int n;
	long ordor;
};
long fun(int n)
{
	static struct Ord old={0,1};
	int i;
	if(n==old.n)
/**********************found***********************/
	return ( (1) );
	if(n>old.n)
		for(i=old.n+1;i<=n;i++)
			old.ordor*=i;
	else
		for(i=old.n;i>n;i--)
			old.ordor/=i;

/**********************found***********************/
	old.n=(2);
/**********************found***********************/
	return ( (3) );
}

解析

(1) old.ordor (2) n (3) old.ordor

分析

由 old.ordor/=i; 可知,累乘后的值都赋值给 old.ordor ,其值也正是我们想要的返回值,故(1)(2)填"old.order"

由两个if语句,可知"n"和"old.n"关系决定着程序的流程,如果n>old.n,就在原来"old.order"的基础上接着累乘,反之,就累除.

第四题

题目

程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功

能是将形参a所指结构体变量s中的数据进行修改,并把a中地址作为函数值返回主函数,在主函数中输出修改后的数据。

例如: a所指变量s中的学号、姓名、和三门课的成绩依次是: 10001、 ”ZhangSan”、95、

80、88, 修改后输出t所指变量的数据应为: 10002、 "LiSi”、96、81、89。

请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。

#include  <stdio.h>
#include  <string.h>
struct student {
  long  sno;
  char  name[10];
  float  score[3];
};
/**********found**********/
__1__ fun(struct student  *a)
{ int  i;
  a->sno = 10002;
  strcpy(a->name, "LiSi");
/**********found**********/
  for (i=0; i<3; i++) __2__ += 1;
/**********found**********/
  return __3__ ;
}
main()
{ struct student  s={10001,"ZhangSan", 95, 80, 88},  *t;
  int  i;
  printf("\n\nThe original data :\n");
  printf("\nNo: %ld  Name: %s\nScores:  ",s.sno, s.name);
  for (i=0; i<3; i++)  printf("%6.2f ", s.score[i]);
  printf("\n");
  t = fun(&s);
  printf("\nThe data after modified :\n");
  printf("\nNo: %ld  Name: %s\nScores:  ",t->sno, t->name);
  for (i=0; i<3; i++)  printf("%6.2f ", t->score[i]);
  printf("\n");
  getchar();
}

解析

(1) struct student * (2) a->score[i] (3) a

分析

第五题

题目

给定程序中,函数fun的功能是:计算下式前n项的和作为函数值返回。

2e5feb129bc54385ae1599aa16363a39.jpeg

例如,当形参n的值为10时,函数返回:9.612558。

请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。

double fun(int  n)
{ int  i;    double  s, t;
/**********found**********/
  s=__1__;
/**********found**********/
  for(i=1; i<=__2__; i++)
  { t=2.0*i;
/**********found**********/
    s=s+(2.0*i-1)*(2.0*i+1)/__3__;
  }
  return  s;
}

解析

0.0 n (t*t)

分析

第六题

题目

给定程序中,函数fun的功能是:将形参n中,各位上为偶数的数取出,并按原来从高位到低位相反的顺序组成一个新的数,并作为函数值返回。

例如,输入一个整数:27638496,函数返回值为:64862。

#include  <stdio.h>
unsigned long fun(unsigned long  n)
{ unsigned long  x=0;    int  t;
  while(n)
  { t=n%10;
/**********found**********/
    if(t%2==__1__)
/**********found**********/
       x=__2__+t;
/**********found**********/
    n=__3__;
  }
  return  x;
}
main()
{ unsigned long  n=-1;
  while(n>99999999||n<0)
  { printf("Please input(0<n<100000000): ");  scanf("%ld",&n);  }
  printf("\nThe result is: %ld\n",fun(n));
  getchar();
}

解析

0 x*10 n/10

第七题

题目

给定程序中,函数fun的功能是:将形参s所指字符串中所有ASCII码值小于97的字符存入形参t所指字符数组中,形成一个新串,并统计出符合条件的字符个数作为函数值返回。

例如,形参s所指的字符串为:Abc@1x56*,程序执行后t所指字符数组中的字符串应为: A@156*。

#include  <stdio.h>
int fun(char  *s, char  *t)
{ int  n=0;
  while(*s)
  { if(*s < 97) {
/**********found**********/
     *(t+n)= __1__ ;  n++;  }
/**********found**********/
     __2__ ;
  }
  *(t+n)=0;
/**********found**********/
  return  __3__ ;
}
main()
{ char  s[81],t[81];    int  n;
  printf("\nEnter a string:\n");  gets(s);
  n=fun(s,t);
  printf("\nThere are %d letter which ASCII code is less than 97: %s\n",n,t);
  getchar();
}

解析

*s   s++   n

第八题

题目

给定程序中,函数fun的功能是:将a所指4×3矩阵中第k行的元素与第0行元素交换。

例如,有下列矩阵:

1 2 3

4 5 6

7 8 9

10 11 12

若k为2,程序执行结果为:

7 8 9

4 5 6

1 2 3

10 11 12

#include  <stdio.h>
#define   N   3
#define   M   4
/**********found**********/
void fun(int (*a)[N], int __1__)
{ int i,temp ;
/**********found**********/
  for(i = 0 ; i < __2__ ; i++)
  { temp=a[0][i] ;
/**********found**********/
    a[0][i] = __3__ ;
    a[k][i] = temp ;
  }
}
main()
{ int x[M][N]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12} },i,j;
  printf("The array before moving:\n\n");
  for(i=0; i<M; i++)
  {  for(j=0; j<N; j++) printf("%3d",x[i][j]);
     printf("\n\n");
  }
  fun(x,2);
  printf("The array after moving:\n\n");
  for(i=0; i<M; i++)
  {  for(j=0; j<N; j++) printf("%3d",x[i][j]);
     printf("\n\n");
  }
  getchar();
}

解析

k N a[k][i]

第九题

题目

给定程序中,函数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);
}
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);
  getchar();
}

解析

double f1 f2

第十题

题目

下列给定程序中,函数fun根据所给n名学生的成绩,计算出所有学生的平均成绩,

把高于平均成绩的学生成绩求平均值并返回。

例如:若有成绩为: 50, 60, 70, 80, 90, 100, 55, 65, 75, 85, 95, 99,

则运行结果应为: 91. 5。

#include  <stdio.h>
#pragma warning (disable:4996)
double  fun(double x[], int n)
{	int i, k=0;
    double avg=0.0, sum=0.0;
	for (i=0; i<n; i++) 
		avg += x[i];
/**********************found***********************/
	avg /= ____(1)____;
	for (i=0; i<n; i++) 
		if (x[i] > avg)
		{
/**********************found***********************/
			____(2)____ += x[i];
			k++;
		}
/**********************found***********************/
	return  ____(3)____;
}
main( )
{  double score[12] ={50,60,70,80,90,100,55,65,75,85,95,99};
   double aa;
   aa= fun(score,12);
   printf("%f\n",aa);
}

解析

n sum sum/k

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值