《C Primer Plus》第九章编程题

1、设计函数min(x,y),返回两个double数值中较小的数值,同时用一个简单的驱动程序测试该函数。
#include  <stdio.h>
double  min( double  , double );

int  main( void  ){
                  double  num1,num2;
    printf( "Please enter two nums(enter q to quit):\n" );
                  while  (scanf( "%lf %lf"  ,&num1,&num2)==2)
                {
                                printf(  "The smaller num is %.3f"  ,min(num1,num2));
                }
                  return  0;
}
double  min( double  num1  ,  double  num2  ){
                  if ( num1  > num2 )
                                  return  num2  ;
                  else
                                  return  num1  ;
}

2、设计函数chline(ch,i,j),实现指定字符在i列到j列的输出,并用一个简单的驱动程序测试该函数。
#include  <stdio.h>

void  chline( char  , int  , int  );

int  main( void  ){
                  char  ch;
                  int  r;
                  int  l;
                printf(  "Please enter the char and the area you want to display:\n"  );
                  while (scanf( "%c %d %d"  ,&ch,&r,&l)==3)
                                chline(ch,r,l);
                  return  0;
}
void  chline( char  ch , int  r , int  l ){
                  int  index_r;
                  int  index_l;
                  for (index_r =0;index_r< r  ;index_r++){
                                  for (index_l=0;index_l< l  ;index_l++){
                                                printf(  "%c" , ch  );
                                }
                                printf(  "\n" );
                }
}

3、两数值的谐均值可以这样算:首先对两数值的倒数取平均数,然后再取倒数。编写一个带有一两个double参数的函数,计算这两个参数的谐均值。
#include  <stdio.h>
const  int  SUM=2;
double  cal( double  , double );

int  main( void  ){
                  double  num1;
                  double  num2;
                printf(  "Please enter the nums you want to cal(Enter q to quit):\n"  );
                  while (scanf( "%lf %lf"  ,&num1,&num2)==2){
                                printf(  "The special average is %.3lf"  ,cal(num1,num2));
                }
                  return  0;
}
double  cal( double  num1 , double  num2 ){
                  double  temp;
                temp = (1/  num1 +1/ num2  )/SUM;
                  return  1/temp;
}
4、编写一个程序,使其从标准输入读取字符,直到遇到文件结尾。对于每个字符,程序需要检查并报告该字符是否是一个字母,如果是的话,程序还应该报告字母表中的数值位置。
#include  <stdio.h>
#include  <ctype.h>
#include  <string.h>

int  main( void  ){
                  char  c;
                printf(  "Please enter something(enter # to quit):\n"  );
                  while ((c=getchar())!= EOF  ){
                                  if (c>= 'A'  &&c<= 'Z' )
                                 c = tolower(c);
                     if (c>= 'a'  &&c<= 'z' )
                                                printf(  "The position in alpha is %-3d.\n"  ,(c- 'a' )+1);
                                  else
                                                printf(  "Sorry,it isn't a word.\n"  );
                                fflush(  stdin );
                                printf(  "Enter another word to check(enter # to quit):\n"  );
                }
                  return  0;
}
5、在第六章“C控制语句:循环”的程序清单6.20中,函数power()的功能是返回一个double类型数的某个正整数次幂。现在改进该循环,使其能正确地计算负幂。同时,用该函数实现0的任何次幂为0,并且任何数值的0次幂为1。使用循环的方法编写函数并在一个程序中测试它。
程序清单6-20:
#include  <stdio.h>
double  power( double  , int );
int  main( void  ){
                  double  x,xpow;
                  int  exp;

                printf(  "Enter a number and the positive integer power"  );
                printf(  "to which \n the number will be raised.Enter q"  );
                printf(  "to quit.\n" );
                  while (scanf( "%lf %d"  ,&x,&exp)==2){
                                xpow = power(x,exp);
                                printf(  "%.3g to the power %d is %.5g\n"  ,x,exp,xpow);
                                printf(  "Enter next pair of numbers or q to quit.\n"  );
                }
                printf(  "Hope you enjoyed this power trip--bye!\n"  );
                  return  0;
}

double  power( double  n  ,  int  p ){
                  double  pow = 1;
                  int  i;
                  for (i=1;i<= p  ;i++)
                                pow*=  n ;
                  return  pow;
}
改写:
#include  <stdio.h>
#include  <math.h>

double  power( double  ,  int );
int  main( void  ){
                  double  x,xpow;
                  int  exp;

                printf(  "Enter a number and the positive integer power"  );
                printf(  "to which \n the number will be raised.Enter q"  );
                printf(  "to quit.\n"  );
                  while  (scanf( "%lf %d"  ,&x,&exp)==2){
                                xpow = power(x,exp);
                                printf(  "%.3g to the power %d is %.5g\n"  ,x,exp,xpow);
                                printf(  "Enter next pair of numbers or q to quit.\n"  );
                }
                printf(  "Hope you enjoyed this power trip--bye!\n"  );
                  return  0;
}

double  power( double  n  ,  int  p ){
                  double  pow = 1;
                  int  i;
                  int  j;
                  if ( p  >0){
                                  for  (i=1;i<= p  ;i++)
                                                pow*=  n  ;
                                  return  pow;}
                  else  if  ( p <0){
                                  for (j=1;j<=abs( p  );j++)
                                                pow*=  n ;
                                  return  1/pow;
                }  else  if  ( n ==0)
                                  return  0;
                  else  if  ( p ==0)
                                  return  1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值