c primer plus第九章习题

本文详细解答了C Primer Plus这本书第九章的习题,涵盖了指针、内存管理和函数调用等核心概念,旨在帮助读者深入理解C语言中这些关键知识点。
摘要由CSDN通过智能技术生成
//第一题 
#include <stdio.h>
double h(double a, double b);
int main(void)
{
	double min, a, b;
	scanf("%lf %lf", &a, &b);
	min = h(a, b);
	printf("%f\n", min);
	return 0;
}
double h(double a, double b){
	if(a >= b)
	    return b;
	else 
	    return a;
}
//第二题 
#include <stdio.h>
void chline(char ch, int i, int j);
int main(void)
{
	int i, j;
	char ch;
	scanf("%c %d %d", &ch, &i, &j);
	chline(ch, i, j);
	return 0;
}
void chline(char ch, int i, int j){
	int row, column;
	
	for(column = 0; column < j; column++){
		for(row = 0; row < i; row++){
			printf("%c", ch);
		}
		printf("\n");
}
}
//第三题 
#include <stdio.h>
void chline(char ch, int i, int j);
int main(void)
{
	int i, j;
	char ch;
	scanf("%c %d %d", &ch, &i, &j);
	chline(ch, i, j);
	return 0;
}
void chline(char ch, int i, int j){
	int row, column;
	
	for(column = 0; column < j; column++){
		for(row = 0; row < i; row++){
			printf("%c", ch);
		}
		printf("\n");
}
}
//第四题 
#include <stdio.h>
double m(double a, double b);
int main(void)
{
	double a, b, s;
	scanf("%lf %lf", &a, &b);
	s = m(a, b);
	printf("%lf\n", s);
	return 0;
}
double m(double a, double b){
	double s;
	s = 1.0 / ((1.0 / a + 1.0 / b) / 2.0);
	return s; 
}
//第五题 
#include <stdio.h>
double larger_of(double *a, double *b);
int main(void)
{
	double a, b;
	scanf("%lf %lf", &a, &b);
	larger_of(&a, &b);
	printf("%lf %lf\n", a, b);
	return 0;
}
double larger_of(double *a, double *b){
	if(*a > *b) //注意是a、b两个地址中的内容比较,要加*
	    *b = *a;
	else
	    *a = *b;
}
//第六题 
#include <stdio.h>
void sort(double *a, double *b, double *c);
int main(void)
{
	double a, b, c;
	scanf("%lf %lf %lf", &a, &b, &c);
	sort(&a, &b, &c);
	printf("%lf %lf %lf", a, b, c); 
	return 0;
}

void sort(double *a, double *b, double *c){
	double t;
	if(*a < *b){
		t = *a;
		*a = *b;
		*b = t;
	}
	
	if(*a < *c){
		t = *a;
		*a = *c;
		*c = t;
	}
	
	if(*b < *c){
		t = *b;
		*b = *c;
		*c = t;
	}
	return a, b , c;
}
//第七题文件先空着
//第八题 
#include <stdio.h>
double p(double x, double y);
int main(void)
{
	double x, y, s;
	while((scanf("%lf %lf", &x, &y)) == 2){
		s = p(x, y);
		printf("%lf\n", s); 
	}
	return 0;
}

double p(double x, double y){
	int i;
	double s = 1;
	if(y == 0){
		if(x == 0)
		return 0;
		else
		return 1;
	}
	else if(y < 0){
		x = 1 / x;
		
		for(i = 0; i < -y; i++)
		    s *= x;
		    
	    return s;
	}
	else if(y > 0){
		for(i = 0; i < y; i++)
		   s *= x;
		    
		    return s;
	}
}
//第九题(有返回值的调用用return) 
#include <stdio.h>
double p(double x, double y);
int main(void)
{
	double x, y, s;
	while((scanf("%lf %lf", &x, &y)) == 2){
		s = p(x, y);
		printf("%lf\n", s); 
	}
	return 0;
}

double p(double x, double y){
	int m = 1;
	double s = 1;
	if(y == 0){
		if(x == 0)
		return 0;
		else
		return 1;
	}
	else if(y < 0){
		if(m = 1)
		    x = 1 / x;
		    
			return x * p(x, y+1);//不能用y++ 
	}
	else if(y > 0){
		return x * p(x, y-1);
	}
}
//第⑩题
#include <stdio.h>
void to_base_n(int x, int y);
int main(void)
{
	int x, y;
	while((scanf("%d %d", &x, &y)) == 2){
		to_base_n(x, y);
	}
	return 0;
}

void to_base_n(int x, int y){
	int r;
	
	r = x % y;
	printf("%d", r);
	
	if(x >= y)
	    to_base_n(x / y, y);
	
	return;
}
//第11题
#include <stdio.h>
void Fibonacci(int n)
{
    if(n == 1)
        printf("1\n");
        
    if(n == 2)
        printf("1\n");
        
    int  a = 1, b = 1, sum, i;
    for(i = 3; i <= n; i++)
    {
        sum = a + b;
        a = b;
        b = sum;
    }
    printf("%d\n", sum);
    return;
}
 
int main(void)
{
    int n;
    scanf("%d", &n);
    Fibonacci(n);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值