}
void main()
{ int a = 3 , b = 4;
cout << f1( a , b ) << endl; }
答案: 16
6. #include
int age( int n ) { int f;
if( n == 1 ) f = 10 ; else f = age( n-1 ) + 2; return f ; }
void main()
{ cout << \
答案: age:18
7. #include
int f1( int a , int b ) { return a + b ; } int f2( int a ,int b ) { return a – b ; }
int f3( int( *t )( int , int ) , int a , int b ) { return ( *t )( a, b ) ; } void main()
{ int ( *p )( int, int ); p = f1 ;
cout << f3( p, 4, 8 ) << endl; p = f2 ;
cout << f3( p, 8, 4 ) << endl; }
答案: 12 4
8. #include
int sub( int, int ); int a = 1 ; void main()
{ int m = 1, n = 2, f; f = sub( m, n );
cout << a << '\\t' << f << endl;
16
f = sub( m, n ) ;
cout << a << '\\t' << f << endl; }
int sub( int c, int d ) { static int m = 2, n = 5 ;
cout << m << '\\t' << n << '\\t' << endl; a = ++ a ; c = m ++ ; d = n ++; return c + d ; }
答案:
2 5
第7章 2 7 第8章 3 6 3 9
3.3 编程题
1.已知 y?sh(1?shx)sh2x?sh3x , 其中sh为双曲正弦函数,即sh(t)?e?e2t?t。编一程序,
输入x的值,求y的值。
解答:略。
2. 输入m、n和p的值,求s = 1?2???m5?1?2???n55333的值。注意判运算中的溢
1?2???p出。
解答:#include
#include
double f( long k,long num ); void main() { long m,n,p; double s;
cout << \ cin >> m >> n >> p;
s = ( f( 1,m ) + f( 3,n ) )/f( 5,p ); cout << \}
double f( long k,long num ) { long i; double sum = 0;
17
for( i=1; i<=num; i++ ) { sum = sum + pow( i,k ); }
return sum; }
#include
double f( long k,long num ); void main() { long m,n,p; double s,f1,f2,f3; cout << \ cin >> m >> n >> p;
f1=f( 1,m ); f2=f( 3,n ) ; f3=f( 5,p ); if ( f1 && f2 && f3 )
{ s = ( f1 + f2 ) / f3; cout << \cout<<\}
double f( long k,long num ) { long i; double sum = 0;
for( i=1; i<=num && sum 3.输入a,b和c的值,编写一个程序求这三个数的最大值和最小值。要求把求最大值和最小值编写成函数,并使用指针或引用作为形式参数把结果返回函数main。 解答: (1)使用指针参数 #include void fmaxmin( float,float ,float ,float *,float * ); void main() { float a,b,c,max,min; cout << \ cin >> a >> b >> c; fmaxmin( a,b,c,&max,&min ); cout << \ 18 } cout << \ void fmaxmin( float x,float y,float z,float *p1,float *p2 ) { float u,v; if ( x>y ) { u = x; v = y; } else { u = y; v = x; }; if ( z>u ) u = z; if ( z (2)使用引用参数 解答:略。 4.用线性同余法生成随机数序列的公式为: rk = ( multiplier * rk-1 + increment ) % modulus 序列中的每一个数rk,可以由它的前一个数rk-1计算出来。例如,如果有: rk = ( 25173 * rk-1 + 13849 ) % 65536 可以产生 65536个各不相同的整型随机数。设计一个函数作随机数生成器,生成一位或两位数的随机数。 利用这个随机数生成器,编写一个小学生四则运算的练习程序: 第7章 ·可以进行难度选择。一级难度只用一位数,二级难度用两位数; 第8章 ·可以选择运算类型,包括加、减、乘、除等; 第9章 ·给出错误提示; 第10章 ·可以统计成绩。 解答: 函数Rand用于生成指定范围的随机整数。 主函数为了使用户正确输入计算难度和运算符,用了两个while循环。for循环出10道题让用户计算,每答对一道题加10分。最外层的while循环控制游戏的开始,使得用户可以反复练习,直至键入N或n。 #include cout<<\现在开始?( Y 或 N )\\n\ cin>>answer; //游戏开始 //生成指定范围的随机数 19 if (answer=='N'||answer=='n') break; while(1) { cout << \请输入难度( 1或2 ):\ cin >> w; if ( w != 1 && w != 2 ) cout << \输入难度错误,重新输入!\ else break ; } { cout << \请输入运算类型( +,-,*,/ ):\ while(1) cin >> op; if ( op != '+' && op != '-' && op != '*' && op != '/' ) cout << \输入运算符错误,重新输入!\ else break; } //出10道题,每题10分 for( i=1; i<=10; i++ ) { while(1) { if( w == 1 ){ a = Rand(0,10); b = Rand(0,10); } if( w == 2 ){ a = Rand(10,100); b = Rand(10,100); } if ( op == '-' ) if ( op == '/' ) break; } if ( a else cout << a << op << b << '='; cin >> d; switch ( op ) { case '+': r = a + b; break; case '-': r = a - b; break; case '*': r = a * b; break; case '/': r = a / b; break; } { cout << \你算对了,加10分!\ t = t + 10; } if ( r == d ) 20