您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页实验4

实验4

来源:爱go旅游网
练习四 Exercise four

目的:1. 掌握函数定义的方法,函数的调用,实参形参,函数返回值的概念; 2. 掌握函数的嵌套调用,递归调用;

3. 掌握变量的作用范围,体会模块化程序设计思路。

Objectives: 1. To understand the methods for defining function, invoking function,

and the concept of real parameter, formatted parameter and return statement;

2. To master the nesting invoking and the recursion invoking;

3. To master the scope of the variable, and to experience the concept of modular programming.

1.自定义函数,求数的平方

To design a customization function that enables to calculate the square of the given value.

2. 用函数嵌套调用形式编写程序,计算1!+2!+3!+……+n!,要求n的值要由键盘

输入

To calculate 1!+2!+3!+……+n! by nesting invoking, and the value of n should be acquired from keyboard.

3.

To calculate mn by recursion invoking, and the values of m and n should be acquired from keyboard. 4.

用递归方法计算m,要求m和n的值都由键盘输入

n

仔细阅读程序, 注意变量的作用域, 存储类别, 若输入 1, 2, 3, 4, 5;分析程序的运行结

果。

To read the codes to analyze the scope of the variable and the storage style; if 1, 2, 3, 4, 5 are

input, please predict the result of the program.

void f1(x, y)

{auto int i; 

static int j; /*调用完, j值保留*/ printf(″i=%5d, j=%5d\\n″, i, j);  i=x+y;  j+=x+y; 

printf(″i=%5d, j=%5d\\n″, i, j);  printf(″x=%5d, y=%5d\\n″, x, y);  }

static int k, l, m; /*在本程序文件内使用*/ void f2(a, b, c) int a, b, c;  { k=a+b+c; 

l=a*b*c; 

m=k+l; 

printf(″a=%5d, b=%5d, c=%5d\\n″, a, b, c);  printf(″k=%5d, l=%5d, m=%5d\\n″, k, l, m); }

main( )

{int d, e, f, g, h; 

int i, j, x, y, a, b, c; 

printf(″please input d, e, f, g, h: ″); 

scanf(″%d, %d, %d, %d, %d″, &d, &e, &f, &g, &h);  printf(″the first invoking f1( )\\n″);  f1(d, e);

printf(″i=%5d, j=%5d\\n″, i, j);  printf(″x=%5d, y=%5d\\n″, x, y);  printf(″d=%5d, e=%5d\\n″, d, e);  printf(″the second invoking f1( )\\n″);  f1(d, e); 

printf(″i=%5d, j=%5d\\n″, i, j);  printf(″to invoke function f2( )\\n″);  f2(f, g, h); 

printf(″k=%5d, l=%5d, m=%5d\\n″, k, l, m);  printf(″a=%5d, b=%5d, c=%5d\\n″, a, b, c);  printf(″f=%5d, g=%5d, h=%5d\\n″, f, g, h);  }

5.程序体验:通用面积计算程序。  (1) 正方形 (2) 长方形 (3) 圆 

(4) 三角形 (5) 梯形 (6) 平行四边形

To experience the codes: the program for calculating general areas which contain square, rectangle, circle, triangle, trapezium, and parallelogram

main( ) {char ch; void zs( ); void cs( );  void ys( );  void ss( );  void ds( );  void ps( );  void tc( );  while (1)

{

printf(″ The codes for calculating general areas \\n″); 

printf(″Z refers to square; C refers to rectangle; Y refers to circle \\n″); 

printf(″S refers to triangle; D refers to trapezium; P refers to parallelogram \\n″);  printf(″Q for quit \\n″); 

printf(″please select the operation(Z/C/Y/S/D/P/Q): ″);  ch=getchar( );  switch (ch)

{ case ′z′: 

case ′Z′: zs( ); break;  case ′c′: 

case ′C′: cs( ); break;  case ′y′: 

case ′Y′: ys( ); break;  case ′s′: 

case ′S′: ss( ); break;  case ′d′: 

case ′D′: ds( ); break;  case ′p′: 

case ′P′: ps( ); break;  case ′q′: 

case ′Q′: tc( ); break; 

default: printf(″Wrong choice!″); } } } 

void zs( ) {float a;  float s; 

printf(″please input the side length of the square: ″); scanf(″%f″, &a);  s=a*a; 

printf(″the area of the square =%7.3f\\n″, s);  }

void cs( ) {float a, b;  float s; 

printf(″please input the length and the width of rectangle: ″);  scanf(″%f, %f″, &a, &b);  s=a*b; 

printf(″the area of the rectangle =%7.3f\\n″, s);  }

#define PI 3.14159 void ys( )

{float r; 

float s; 

printf(″please input the radius of the circle ″);  scanf(″%f″, &r);  s=PI*r*r; 

printf(″the area of the circle =%7.3f\\n″, s);  }

void ss( ) {float a, b, c;  float l;  int flag; 

float s; 

printf(″please input the each side of the triangle: ″);  scanf(″%f, %f, %f″, &a, &b, &c);  flag=(a+b>c)&&(b+c>a)&&(c+a>b); if (flag)

{l=0.5*(a+b+c); 

s=sqrt(l*(l-a)*(l-b)*(l-c)); 

printf(″the area of the triangle =%7.3f\\n″, s);  }

else printf(″the sides are not able to form a triangle!\\n″);  } 

void ds( ) {float a, b, h;  float s; 

printf(“please input the top edge, bottom edge, and the height of the trapezium respectively: “); scanf(″%f, %f, %f″, &a, &b, &h);  s= (a+b)*h*0.5; 

printf(″the area of the trapezium =%7.3f\\n″, s);  }

void ps( ) {float a , h; 

float s; 

printf(″please input the bottom edge and the height of the parallelogram: ″);  scanf(″%f, %f″, &a, &h); 

s= a*h; 

printf(″the area of the parallelogram =%7.3f\\n″, s);  }

void tc( )

{printf(″To quit the program!″);  exit(0);  }

参考答案:The reference answers

1.

#include

long square (long x)

{ long xsquare; xsquare=x*x;

return xsquare;  }  void main() 

{ long innum, result; 

printf("Input an integer: ");  scanf("%ld", &innum);  result=square (innum); 

printf("\nThe square number of %ld is %ld", innum, result); }

2.int power (int n) {int f;

if(n= =1) f=1;

else f=power(n-1)*n; return(f);}

int sum (int n) {int i, s,y; s=0;

for (i=1;i{y=power(i); s+=y;} return(s); }

main()

{int sum1, n;

scanf(“%d”, &n); sum1=sum(n);

printf(“%d\\n”, sum1);}

3.

long mn(int m, int n)

{long j; 

if (n>0) j=m*mn(m, n-1); else j=1;  return(j); } main()

{ int m,n;

long f;

scanf(“%d,%d”, &m,&n); f=mn(m,n); printf(“%ld”, f); }

4.运行结果: 

The first invoking f1( )

i= 34(random value), j= 0  i= 3, j= 3  x= 1, y= 2

i= 34(random value), j= 1985 (random value)  x= -19(random value), y= 2340(random value) d= 1, e= 2 the second invoking f1( )

i= 34(random value), j= 3  i= 3, j= 6  x= 1, y= 2

i= 34(random value), j= 1985 (random value)  to invoke function f2( ) 

a= 3, b= 4, c= 5 k= 12, l = 60, m= 72 k= 12, l = 60, m= 72

a= 13(random value), b= 124(random value), c= 2525(random value) f= 3, g= 4, h= 5

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务