C Primer Plus 第9章之菜鸟儿的编程题答案

本文仅为记录自己手打编程题答案所用,不能保证正确性,欢迎大家学习交流,有问题请随时指出~

 1、

#include<stdio.h>
double min(double, double);
int main(void)
{
	double x, y;
	printf("Please enter 2 numbers:");
	scanf_s("%lf%lf",&x,&y);
	printf("The smallest one is %lf", min(x, y));
	return 0;
}
double min(double x, double y)
{
	double min;
	min = (x <= y) ? x : y;
	return min;
}

double对应lf

2、

#include<stdio.h>
void chline(char ch, int i, int j);
int main(void)
{
	char ch;
	int row, col;
	printf("Please enter a char:");
	scanf_s("%c", &ch);
	printf("Please enter the number of rows:");
	scanf_s("%d", &row);
	printf("Please enter the number of columns:");
	scanf_s("%d", &col);
	chline(ch,row,col);
    return 0;
}
void chline(char ch, int i, int j)
{
	for (int row = 1;row <= j;row++)
	{
		for (int col = 1; col <= i; col++)
			putchar(ch);
		printf("\n");
	}
}

3、不是和第二题一样的么,略

4、

#include<stdio.h>
double har(double i, double j);
int main(void)
{
	double i, j;
	printf("Please enter a number:");
	scanf_s("%lf", &i);
	printf("Please enter a number again:");
	scanf_s("%lf", &j);
	printf("The harmonic mean is:%lf",har(i,j));
	return 0;
}
double har(double i,double j)
{
	double x;
	x = (1/ i + 1 / j) / 2;
	return x;
}

还是lf问题!

5、

遇到问题:函数要求输入两个数值实参,但调用时实际只改变了形参的值(形参和实参是分开存储的,形参有自己的地址),理论上可以用改变指针来实现,但是题目要求的函数形式却是输入变量值,感觉并不可行。

错版:

#include<stdio.h>
void large_of(double x , double y);
int main(void)
{
	double i, j;
	printf("Please enter a number for i:");
	scanf_s("%lf", &i);
	printf("Please enter a number for j:");
	scanf_s("%lf", &j);
	large_of(i, j);
	printf("Now i is %lf,j is% lf",i,j);
	return 0;
}
void large_of(double x,double y)
{
	?
}

用指针版:

#include<stdio.h>
void large_of(double* x , double* y);
int main(void)
{
	double i, j;
    
	printf("Please enter a number for i:");
	scanf_s("%lf", &i);
	printf("Please enter a number for j:");
	scanf_s("%lf", &j);
	large_of(&i, &j);
	printf("Now i is %lf,j is% lf",i,j);
	return 0;
}
void large_of(double* x,double* y)
{
	if (*x >= *y)
        *y = *x;
    else
        *x = *y;
   
}

6、

初版:

#include<stdio.h>
void large_of(double* x, double* y, double* z);
int main(void)
{
	double x, y,z;
	printf("Please enter a number for x:");
	scanf_s("%lf", &x);
	printf("Please enter a number for y:");
	scanf_s("%lf", &y);
	printf("Please enter a number for z:");
	scanf_s("%lf", &z);
	large_of(&x,&y,&z);
	printf("Now x is %lf,y is% lf,z is %lf",x,y,z);
	return 0;
}
void large_of(double* x,double* y,double* z)
{
	double min, mid, max;
	
	if (*x > *y && *x > *z)
	{
		max = *x;
		if (*y > *z)
		{
			min = *z;
			mid = *y;
		}
		else
		{
			min = *y;
			mid = *z;
		}
	}
	else if (*y > *x && *y > *z)
	{
		max = *y;
		if (*x > *z)
		{
			min = *z;
			mid = *x;
		}
		else
		{
			min = *x;
			mid = *z;
		}
	}
	else 
	{
		max = *z;
		if (*y > *x)
		{
			min = *x;
			mid = *y;
		}
		else
		{
			min = *y;
			mid = *x;
		}
	}

	*x = min;
	*y = mid;
	*z = max;
}

比大小函数过于麻烦,经观察他人答案得出,此处可用冒泡法

更改如下:

#include<stdio.h>
void large_of(double* x, double* y, double* z);
int main(void)
{
	double x, y, z;
	printf("Please enter a number for x:");
	scanf_s("%lf", &x);
	printf("Please enter a number for y:");
	scanf_s("%lf", &y);
	printf("Please enter a number for z:");
	scanf_s("%lf", &z);
	large_of(&x, &y, &z);
	printf("Now x is %lf,y is% lf,z is %lf", x, y, z);
	return 0;
}
void large_of(double* x, double* y, double* z)
{
	double tmp;//临时变量

	if (*x > *y)//x,y交换顺序
	{
		tmp = *x;
		*x = *y;
		*y = tmp;
	}
	if (*y > *z)//y,z交换顺序
	{
		tmp = *y;
		*y = *z;
		*z = tmp;			
	}
	if (*x > *y)//x,y交换顺序(因为y的值有可能已经变成z的值了,所以还得再比较一下大小)
	{
		tmp = *x;
		*x = *y;
		*y = tmp;
	}
}

7、

初版:

#include<stdio.h>
#include<ctype.h>
int alp(char c);

int main(void)
{
	int ch;
	while ((ch = getchar()) != EOF)
	{
		if (isalpha(ch))
			printf("%c is an alpha ",ch);
		alp(ch);
	}
	
	return 0;

}

int alp(char c)
{
	if (isalpha(c))
	{
		if (c <= 'Z')
			printf("and its position is: %d\n", c - 'A' + 1);
		else
			printf("and its position is:%d\n", c - 'a' + 1);
	}
	else
		printf("-1\n");
}

这里漏了要求,alp函数里应该要返回值而不是直接printf

修改如下:

#include<stdio.h>
#include<ctype.h>
int alp(char c);

int main(void)
{
	int ch;
	while ((ch = getchar()) != EOF)
	{
		if (isalpha(ch))
			printf("%c is an alpha ",ch);
		alp(ch);
	}
	
	return 0;

}

int alp(char c)
{
	if (isalpha(c))
	{
		if (c <= 'Z')
			{printf("and its position is: %d\n");
            return  (c - 'A' + 1);}
		else
			{printf("and its position is:%d\n");
            return ( c - 'a' + 1);}
	}
	else
		return -1;
}

8、

#include<stdio.h>

double power(int n, int p);

int main(void)
{
	int n,p;
	
	printf("Please enter a number:\n");
	scanf_s("%d",&n);
	printf("Please enter the power:\n");
	scanf_s("%d", &p);
	printf("Its power is: %lf\n", power(n, p));


	
	return 0;

}

double power(int n,int p)
{
	double pow=1;
	if (n == 0)
	{
		if (p == 0)
		{
			printf("It's not defined!");//注意这里,将被调函数用在printf语句里仍然能打印被调函数中的printf语句
			pow = 1;
		}
		else
			pow = 0;
	}
	else
	{
		if (p > 0)
		{
			while (p-- != 0)
				pow *= n;
		}
		else if (p == 0)//这里原本的else漏了,找了半天的bugQAQ
		{
			pow = 1;
		}
		else
		{
			while (p++ != 0)
				pow *=n;
			pow = 1 / pow;
		}
	}
	return pow;
}

见注释处:被调语句有返回值也有printf语句的情况

咳咳,再用观察他人答案法可优化如下:

·最后一中情况pow直接*=1/n,不用先搞下面的再翻过来那么麻烦

或者直接p=-p即可和上一种情况合并

·把p==0的情况单独拎出来,因为pow都是=1,只要if(n==0)再printf就行了

9、

#include<stdio.h>

double power(int n, int p);

int main(void)
{
	int n,p;
	
	printf("Please enter a number:\n");
	scanf_s("%d",&n);
	printf("Please enter the power:\n");
	scanf_s("%d", &p);
	printf("Its power is: %lf\n", power(n, p));
	
	return 0;

}

double power(int n,int p)
{
	double pow=1;
	if (n == 0)
	{
		if (p == 0)
		{
			printf("It's not defined!");//注意这里,将被调函数用在printf语句里仍然能打印被调函数中的printf语句
			pow = 1;
		}
		else
			pow = 0;
	}
	else
	{
		if (p > 0)
		{
			while (p-- != 0)
				pow*= n;
		}
		else if (p == 0)
		{
			pow = 1;
		}
		else
		{
			pow=1/ power(n, -p);//递归
		}
	}
	return pow;
}

串门回来后表示自己用的递归实在是弱爆了==!

实际上只要留p=0的情况就好了,大佬打的码如下:

double pow_rewrite(double base, double index)
{
    if (index > 0)
    {
        return pow(base, index);
    }
    if (0 == index)
    {
        if (0 == base)
        {
            printf("The base number is 0 is not defined!\n");
        }

        return 1;
    }
    if (index < 0)
    {
        double tmp = pow(base, -index);
        return (1 / tmp);
    }

from:C Primer Plus 第六版 第九章课后编程练习答案 | Jimmy's Blog

--

xiaojimmychen

10、本题为十进制转化的好题!原型详见《c primer plus》的224页(可能会另写一篇文章记录一下自己对进制转化的理解,这里就先将就着看吧hh)

#include<stdio.h>

int to_base_n(int dec,int scale);

int main(void)
{
	int n, p;

	printf("Please enter a decimal number:\n");
	scanf_s("%d", &n);
	printf("Please enter the scale(2-10):\n");
	scanf_s("%d", &p);
	to_base_n(n,p);

	return 0;

}

int to_base_n(int dec,int scale)//dec:decimal(十进制),scale:转化后的进制(这里只支持2-10)
{
	int r;//这是转化后的每一位数字
	
	r = dec % scale;//取余数实则在求转化后的最后一位
	if (dec >= scale)//删掉最后一位后还能继续用新进制表示
	{
		to_base_n(dec/scale,scale);//dec/scale就是把转化后的最后一位给删了,得到前面几位
	}
	printf("%d",r);
}

 11、斐波那契数列的对应项输出(用循环不用递归),注意x,y在变化,不好只用x,y,当找个过渡值

#include<stdio.h>

unsigned long Fibonacci(unsigned n);

int main(void)
{
	int n, p;

	printf("Please enter a number:\n");
	scanf_s("%d", &n);
	printf("Fibonacci(n)=%lu", Fibonacci(n));
	return 0;

}

unsigned long Fibonacci(unsigned n)
{
	unsigned x=1, y=1,a;
	if (n > 2)
	{
		while (n-- > 2)
		{
			a=y;//x,y在变,所以要取个过渡值
			y = x + y;
			x = a;
		}
		return y;
	}
	else
		return 1;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值