C Primer Plus(第六版)第九章 编程练习答案

Posted 水番正文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C Primer Plus(第六版)第九章 编程练习答案相关的知识,希望对你有一定的参考价值。

前言:这周课程比较繁忙,抽出来的时间莫名都去做题了,没太顾着看书,下周开始以看书为主,题目就不怎么做了,emmmmmm扯远了。

仅供参考,新手勿喷。

CH08 Code answer 1:

#include <stdio.h>

double min(double,double);

int main(void)
{
	double a,b;
	a = 1;
	b = 2;
	printf("Return:%lf",min(a,b));	
	
	return 0;
} 

double min(double a,double b)
{
	return (a > b ) ? b : a;
}

CH08 Code answer 2:

#include <stdio.h>

void chline(char,int,int);

int main(void)
{
	char ch = 'P';
	int a = 5;
	int b = 3;
	chline(ch,a,b);
	
	return 0;
}

void chline(char ch,int line,int row)
{
	for(int i=1;i<=line;i++)
	{
		for(int j=1;j<=row;j++)
		{
			printf("%c",ch);
		}
		printf("\\n");
	}
}

CH08 Code answer 3:

//这和上题有区别么?? 
#include <stdio.h>

void chline(char,int,int);

int main(void)
{
	char ch = 'Z';
	int a = 5;
	int b = 3;
	chline(ch,a,b);
	
	return 0;
}

void chline(char ch,int line,int row)
{
	for(int i=1;i<=line;i++)
	{
		for(int j=1;j<=row;j++)
		{
			printf("%c",ch);
		}
		printf("\\n");
	}
}

CH08 Code answer 4:

#include <stdio.h>

double re_avg(double,double);

int main(void) 
{
	double a = 1;
	double b = 2;
	double n; 
	printf("%lf",re_avg(a,b));
	
	return 0;
}

double re_avg(double a,double b)
{
	return 1/((1/a+1/b)/2);
}

CH08 Code answer 5:

#include <stdio.h>

void larger_of(double *,double *);

int main(void)
{
	double x = 5;
	double y = 3;
	larger_of(&x,&y);
	printf("%lf %lf",x,y);
	
	return 0;
} 

void larger_of(double * x,double * y)
{
	(*x>*y) ? *y=*x : *x-*y;
}

CH08 Code answer 6:

#include <stdio.h>

void m3(double *,double *,double *);

int main(void)
{
	double x = 3;
	double y = 2;
	double z = 1;
	m3(&x,&y,&z);
	printf("%lf %lf %lf",x,y,z);
	
	return 0;
}

void m3(double * x,double * y,double * z)
{
	double tmp;
	if(*x > *y)
	{
		tmp = *x;
		*x = *y;
		*y = tmp;
	}
	
	if(*z < *x)
	{
		tmp = *z;
		*z = *y;
		*y = *x;
		*x = tmp;
	}
	else if(*z < *y)
	{
		tmp = *y;
		*y = *z;
		*z = tmp;
	}	
	 			
} 

CH08 Code answer 7:

#include <stdio.h>
#include <ctype.h>

int ch_num(char);

int main(void)
{
	char ch;
	while((ch = getchar()) != EOF)
	{
		printf("%c-%d ",ch,ch_num(ch));
	}
	
	return 0;
}

int ch_num(char ch)
{
	if(isalpha(ch))
	{
		if(ch >= 'a' && ch <= 'z')
			return (int)ch - 96;
		if(ch >= 'A' && ch <= 'Z')
			return (int)ch - 64;
	}
	return -1;
}

CH08 Code answer 8:

#include <stdio.h>

double power(double,double);

int main(void)
{
	double x = 0;
	double n = 0;
	x = power(x,n);
	printf("%lf",x);
	
	return 0;
}

double power(double x,double n)
{
	double pow = 1;
	
	if(x == 0 && n == 0)
	{
		printf("0的0次未定义");
		return 0;
	}
	
	for(int i =1;i <= n;i++) 
	{
		pow *= x;
	}	
	
	return pow;
}

CH08 Code answer 9:

#include <stdio.h>

double power(double,double);

int main(void)
{
	double x = 2;
	double n = 4;
	x = power(x,n);
	printf("%lf",x);
	
	return 0;
}

double power(double x,double n)
{
	if(x == 0 && n == 0)
	{
		printf("0的0次未定义");
		return 0;
	}
	
	if(--n > 0)						//当到最后一层返回参数为x本身,递归的层数就是循环的次数 
	{
		x *= power(x,n);
	}	
	
	return x;
}

CH08 Code answer 10:

/* binary.c -- prints integer in binary form */
#include <stdio.h>
void to_base_n(int,int);

int main(void)
{
    int number;
    int n = 8;
    printf("Enter an integer (q to quit):\\n");
    while (scanf("%lu", &number) == 1)
    {
        printf("Binary equivalent: ");
        to_base_n(number,n);
        putchar('\\n');
        printf("Enter an integer (q to quit):\\n");
    }
    printf("Done.\\n");
    
    return 0;
}

void to_base_n(int n,int x)   /* recursive function */
{
    int r;
    
    r = n % x;
    if (n >= x)
        to_base_n(n / x,x);
    printf("%d",r);
    
    return;
}

CH08 Code answer 11:

/*
Name:Fibonacci
Function:返回斐波那契对应下标的值
Parameter:斐波那契下标 
*/
#include <stdio.h>
int Fibonacci(int);
int main(void)
{
	
	int n = 6;			//参数 
	
	n = Fibonacci(n);
	printf("%d",n);	//显示 
	
	return 0;
 } 
 
int Fibonacci(int n)
 {
 	int a[100] = {0};		//初始化数组 
	a[1] = 1;
	a[2] = 1; 
	
	for(int i = 3; i <= n;i++)
	{
		a[i] = a[i-1] + a[i-2]; 
	}
	return a[n];
 }

以上是关于C Primer Plus(第六版)第九章 编程练习答案的主要内容,如果未能解决你的问题,请参考以下文章

C Primer Plus(第六版)第五章 编程练习答案

C Primer Plus(第六版)第十章 编程练习答案

C Primer Plus(第六版)第十四章 编程练习答案

C Primer Plus(第六版)第十五章 编程练习答案

C Primer Plus(第六版)第二章 编程练习答案

C Primer Plus(第六版)第十二章 编程练习答案