C primer plus课后习题 第九章

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

int main(void)
{
printf("%lg \n",min(3,2)) ;
printf("%lg \n",min(3,3)) ;
printf("%lg",min(134E-10,134.1e-10));
}

double min(double x, double y)
{
return (x>y) ? y : x ;
}

2.设计一个函数chline(ch, i, j), 打印指定的字符j行i列。 在一个简单的驱动程序中测试该函数。
#include <stdio.h>
void chline(int, int, int) ;
int main(void)
{
chline(‘A’,10,5) ;
}
void chline(int ch, int i, int j)
{
int col,row ;
for (row=1;row<=j ;row++)
{
for (col=1;col<=i ;col++)
putchar(ch) ;
putchar(’\n’) ;
}
}

3.编写一个函数, 接受3个参数: 一个字符和两个整数。 字符参数是待
打印的字符, 第1个整数指定一行中打印字符的次数, 第2个整数指定打印指
定字符的行数。 编写一个调用该函数的程序。
2.c
#include <stdio.h>
void chline(int ch, int i, int j)
{
int col,row ;
for (row=1;row<=j ;row++)
{
for (col=1;col<=i ;col++)
putchar(ch) ;
putchar(’\n’) ;
}
}

1.c
#include <stdio.h>
void chline(int, int, int) ;
int main(void)
{
chline(‘A’,10,5) ;
getchar() ;
}
//使用gcc -o 1.exe 1.c 2.c编译后运行1.exe

4.两数的调和平均数这样计算: 先得到两数的倒数, 然后计算两个倒数
的平均值, 最后取计算结果的倒数。 编写一个函数, 接受两个double类型的
参数, 返回这两个参数的调和平均数。
#include <stdio.h>
double f1(double,double) ;
int main(void)
{
printf("%lg",f1(133,77)) ;
}
double f1(double x,double y)
{
if(x0||y0)
return 0 ;
return(1 / ( (1/x+1/y)/2 ) ) ;
}

5.编写并测试一个函数larger_of(), 该函数把两个double类型变量的值替换为较大的值。
例如, larger_of(x, y)会把x和y中较大的值重新赋给两个变量。
#include <stdio.h>
double larger_of(double *,double *) ;
int main(void)
{
double a=199,b=177;
larger_of(&a,&b) ;
printf(“a=%lg,b=%lg”,a,b) ;
}
double larger_of(double * u,double * v)
{
(*u>*v) ? (*v=*u) : (*u=*v) ;
//如果U最大,就把U赋给V,否则把V赋值给U
}

6.编写并测试一个函数, 该函数以3个double变量的地址作为参数, 把最
小值放入第1个函数, 中间值放入第2个变量, 最大值放入第3个变量。
#include <stdio.h>
double f1(double *,double *,double *) ;
/f2:调换2个参数的值,把较大值放在前面/
double f2(double *,double *) ;

int main(void)
{
double fst=3453,scd=-33,thd=66.123e2;
printf(“Before calling function:fst=%lg,scd=%lg,thd=%lg \n”,
fst,scd,thd) ;

f1(&fst,&scd,&thd) ;//调用排序函数 

printf("After calling function:fst=%lg,scd=%lg,thd=%lg",
fst,scd,thd) ;

}

double f1(double * u,double * v,double * w)
{
f2(u,v) ; //此时u>v
f2(u,w) ; //此时u>w,u为最大值
f2(v,w) ; //再排序剩下的v,w,将较大值给v
}

double f2(double * x,double * y)
{
double temp ;
if(*x<*y) //如果x较小,调换X和Y的值,保证第一个参数是最大的
{
temp=*x ;
*x=*y ;
*y=temp ;
}
}

7.编写一个函数, 从标准输入中读取字符, 直到遇到文件结尾。 程序要
报告每个字符是否是字母。 如果是, 还要报告该字母在字母表中的数值位
置。 例如, c和C在字母表中的位置都是3。 合并一个函数, 以一个字符作为
参数, 如果该字符是一个字母则返回一个数值位置, 否则返回-1。
#include <stdio.h>
#include <ctype.h>
void f1(void) ;
int f2(int) ;

int main(void)
{
f1() ;
}

void f1(void)
{ int ch ;
while((ch=getchar())!=EOF)
{
if(f2(ch)!=-1)
printf("%c:%d \n",ch,f2(ch)) ;
}
}

int f2(int ch)
{
int i , a[26] ;
for (i=0;i<26;i++)
a[i]=‘a’+i ;

if (isalpha(ch))
{
	for(i=0;i<26;i++)
	{
		if (a[i]==tolower(ch))
			return i+1 ;
	}	
}
else
	return -1 ;

}

8.第6章的程序清单6.20中, power()函数返回一个double类型数的正整数
次幂。 改进该函数, 使其能正确计算负幂。 另外, 函数要处理0的任何次幂
都为0, 任何数的0次幂都为1(函数应报告0的0次幂未定义, 因此把该值处
理为1) 。 要使用一个循环, 并在程序中测试该函数。
#include <stdio.h>
double power(double n, int p); // ANSI函数原型
int main(void)
{
double x, xpow;
int exp;
printf(“Enter a number and the integer power”);
printf(" to which\nthe 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;
if (p>0)
{
for (i = 1; i <= p; i++)
pow *= n;
}
else if(p<0)
{
for (i = 1; i <= -p; i++)
pow *=n ;
pow=1.0/pow ;
}

return pow; // 返回pow的值

}

9.使用递归函数重写编程练习8。
#include <stdio.h>
double power(double n, int p); // ANSI函数原型
int main(void)
{
double x, xpow;
int exp;
printf(“Enter a number and the integer power”);
printf(" to which\nthe 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;
if (p==0) //递归停止条件
return 1;
if (p>0)
return npower(n,p-1) ;
if (p<0)
return 1/(n
power(n,-p-1)) ;

}

10.为了让程序清单9.8中的to_binary()函数更通用, 编写一个to_base_n()
函数接受两个在2~10范围内的参数, 然后以第2个参数中指定的进制打印第
1个参数的数值。 例如, to_base_n(129, 8)显示的结果为201, 也就是129的
八进制数。 在一个完整的程序中测试该函数。
#include <stdio.h>
void to_binary(unsigned long ,int );
int main(void)
{
unsigned long number;
int m ;
printf(“Enter two integers : (q to quit):\n”);
while (scanf("%lu %d", &number,&m) == 2)
{
printf("The value of base %d is : ",m);
to_binary(number,m);
putchar(’\n’);
printf(“Enter an integer (q to quit):\n”);
}
printf(“Done.\n”);
return 0;
}

void to_binary(unsigned long n,int m) /* 递归函数 */
{
int r; //保存余数
r = n % m; //第一个余数,在最后显示
if (n >= m)
to_binary(n / m,m);
if (r==0)
putchar(‘0’) ;
else
printf("%d",r) ;
return;
}

11.编写并测试Fibonacci()函数, 该函数用循环代替递归计算斐波那契数。
#include <stdio.h>
long Fibonacci(unsigned);
int main(void)
{
printf("%ld",Fibonacci(12)) ;
return 0;
}

long Fibonacci(unsigned n)
{
int i;
int n1=1 ; //前1位值
int n2=1 ; //前2位值
long s ; //保存当前位数值

if (n <=2 ) //1,2位都为1 
	return 1 ;
//从第三项开始 ;到n为止;每次递增后,
//都将前1位值传给前2位,当前值传给前1位 
for (i=3;i<=n;i++,n1=n2,n2=s)
{
	s=n1+n2 ; //当前项=前1位值+前2位值 
}
return s ;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值