c语言中void plusone,C Primer Plus 第9章 函数 9.11 编程练习答案

1、设计函数min(x,y)返回两个double数值中较小的数值,同时用一个驱动程序测试该函数。

#include

double min(double ,double );

int main(void)

{

double x,y;

printf("input two doubles:");

scanf("%lf%lf",&x,&y);

printf("the smaller is:%.2lf\n",min(x,y));

return(0);

}

double min(double a,double b)

{

r=(a

}

2、 设计函数chline(ch,i,j),实现指定字符在i列到j列的输出,同时用一个驱动程序测试该函数。

#include

void chline(char ch,int i,int j);

int main (void)

{

char ch;

int x,y;

printf("Input a char: ");

scanf("%c",&ch);

printf("Input two inter: ");

scanf("%d%d",&x,&y);

chline(ch,x,y);

return 0;

}

void chline(char ch,int i,int j)

{

int k;

for(k=1;k

printf(" ");

for(;k<=j;k++)

printf("%c",ch);

return 0;

}

3、编写一个函数。函数的3个参数是一个字符和两个整数。字符参数是需要输出的字符。第一个字符说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。编写一个调用该函数的程序。

#include

void chline(char ch,int i,int j);

int main (void)

{

char ch;

int x,y;

printf("Input a char : ");

scanf("%c",&ch);

printf("Input the row and column : ");

scanf("%d%d",&x,&y);

chline(ch,x,y);

return 0;

}

void chline(char ch,int i,int j)

{

int r,c;

for(r=1;r<=i;r++)

{

for(c=1;c<=j;c++)

printf("%c",ch);

printf("\n");

}

return 0;

}

4、两个数的谐均值可以这样计算:首先对两个数值的倒数取平均值,最后再取拿到。编写一个带有两个double参数的函数,计算这两个参数的谐均值。

#include

double calculate(double,double);;

int main(void)

{

double a,b;

printf("Input two doubles: ");

scanf("%lf%lf",&a,&b);

printf("1/((1/x+1/y)/2)=%0.3lf\n",calculate(a,b));

return 0;

}

double calculate(double x,double y)

{

return 1/((1/x+1/y)/2);

}

5、编写并测试函数larger_of(),其功能是将两个double类型变量的数值替换成它们中的较大值。例如,larger_of(x,y)会把x和y中的较大者重新赋值给变量x和y。

#include

void larger_of(double *,double *);

int main(void)

{

double a,b;

printf("Input two doubles: ");

scanf("%lf%lf",&a,&b);

larger_of(&a,&b);

printf("the result is: a=%0.3lf,b=%0.3lf\n",a,b);

return 0;

}

void larger_of(double *x,double *y)

{

*x=*y=(*x>*y?*x:*y);

}

6、编写一个程序,使其从标准输入读取字符,下到遇到文件结尾。对于每个字符,程序需要检查并报告该字符是否是一个字母。如果是的话,程序还应报告字母在字母表中的位置。例如,c和C的字母位置都是3.可以先实现这样一个函数:接受一个字符参数,如果该字符是字母则返回该字母的数值位置,否则返回-1。

#include

#include

int adress(char);

int main(void)

{

char ch;

printf("Input a ch: ");

while((ch=getchar())!='\n')

{

printf("the position of the char in ABC is: %d\n",adress(ch));

}

return 0;

}

int adress(char ch)

{

if(isalpha(ch))

return tolower(ch)-'a'+1;

else

return -1;

}

7、在第6章“c控制语句:循环”的程序清单6.20中,函数power()的功能是返回一个double类型数的某个正整数次幂。现在改进该函数使用能正确的计算负幂。同时,用该函数实现0的任何次幂为0,并且任何数值的0次幂为1。使用循环的方法编写该函数并在程序中测试它。

#include

double power(double ,int);

int main(void)

{

double x;

int exp;

printf("input the base number and the exponent: ");

scanf("%lf%d",&x,&exp);

printf("%.3g to the power %d is %.5g\n",x,exp,power(x,exp));

return 0;

}

double power(double n,int p)

{

int i;

double pow=1;

if (p>0)

for(i=1;i<=p;i++)

pow*=n;

else if (p<0)

for(i=-1;i>=p;i--)

pow/=n;

else if(n!=0)

pow=1;

else pow=1/n; //0的0次幂无意义,所以用1/0这个无意义气数代替

return pow;

}

8、使用递归函数重做练习7.

#include

double power(double ,int);

int main(void)

{

double x;

int exp;

printf("input the base number and the exponent: ");

scanf("%lf%d",&x,&exp);

printf("%.3g to the power %d is %.5g\n",x,exp,power(x,exp));

return 0;

}

double power(double n,int p)

{

int i;

double pow=1;

if (p>0)

for(i=1;i<=p;i++)

pow*=n;

else if (p<0)

for(i=-1;i>=p;i--)

pow = 1 / power(n,-p);

else if(n!=0)

pow=1;

else pow=1/n; //0的0次幂无意义,所以用1/0这个无意义气数代替

return pow;

}

9、为了使程序清单9.8中的函数to_binary()更一般化,可以在新的函数to_base_n中使用第二个参数,且该参数的范围从2-10.然后,新函数输出第一个参数在第二个参数规定的进制数下的数值结果。例如,to_base_n(129,8)的输出结果是201,也就是129的八进制数值。最后在一个完整的程序中对该函数进行测试。

#include

void to_base_n(unsigned long,unsigned int);

int main(void)

{

unsigned long number;

unsigned int base;

printf("Enter an integer (q to quit):\n");

while(scanf("%lu%u",&number,&base)==2)

{

printf("%lu's base %u equivalent: ",number,base);

to_base_n(number,base);

putchar('\n');

printf("Enter an integer (q to quit):\n");

}

printf("Done.\n");

return 0;

}

void to_base_n(unsigned long n,unsigned int base)

{

int r ;

r=n%base;

if(n>=base)

to_base_n(n/base,base);

putchar('0'+r);

return 0 ;

}

10、编写并测试一个函数FIbonacci(),在该函数中使用循环代替递归完成斐波纳契数列的计算。

#include

long Fibonacci(int);

int main(void)

{

int n;

printf("Enter an integer (q to quit):\n");

while(scanf("%d",&n)==1)

{

printf("The term %d of Fibonacci sequence is %d\n",n,Fibonacci(n));

printf("Enter an integer (q to quit):\n");

}

printf("Done.\n");

return 0;

}

long Fibonacci(int n)

{

int n1,n2,temp,i;

if(n>2)

for(n1=1,n2=1,i=3;i<=n;i++)

{

temp=n1+n2; //计算前两个数的和

n1=n2; //把第二个数的值赋给第一个数

n2=temp; //把两者之后再赋给第二个变量

}

else n2=1;

return n2;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值