C primer plus 第六版 第九章 编程练习 答案

9.1

//设计一个函数min(x,y)返回两个double类型值的最小值
#include 

double min(double x,double y); // 函数原型

int main(void)
{
	double x, y;
	printf("Please enter two numbers, I will give you the smaller number.\n");
	while (scanf("%lf %lf", &x, &y) == 2)
	{ 
		printf("The smaller number is %.2lf.\n", min(x, y));
		printf("Please enter the next numbers: \n");
	}
	printf("Wrong number! Job Done!");
	return 0;
}

//比大小函数
double min(double x, double y)
{
	double min_num;
	if (x < y)
		min_num = x;
	else
		min_num = y;

	return min_num;
}

9.2

//设计含数chline(ch, i, j),打印指定的字符j行i列。

#include 	

void chline(char ch, int i, int j); //函数原型

int main(void) 
{
	char ch;
	int rows, cols;

	printf("Please enter a char and lines and numbers.\n");
	while ((ch = getchar()) != '\n')
	{
		if (scanf("%d %d", &rows, &cols) != 2)
			break;
		chline(ch, rows, cols);
		while ((ch = getchar()) != '\n')
			continue;
		printf("Enter another character and inegers.\n");
	}

	printf("Job Down!");

	return 0;
}

void chline(char ch, int i, int j)
{
	for (int a = 0; a < i; a++)
	{
		for (int a = 0; a < j; a++)
			putchar(ch);
		putchar('\n');
	}
}

9.3
同9.2
for循环i,j互换位置

9.4

//调和平均数计算

double Harmonic_Mean(double x, double y); //函数原型

#include 

int main(void)
{
	double a, b;
	char ch;
	
	printf("Please enter two integers.\n");
	while (scanf("%lf %lf", &a, &b) == 2)
	{
		printf("The harmonic mean is %lf.\n", Harmonic_Mean(a, b));
		printf("Please enter next two integers:\n");
	}
		
	printf("Job down!");

	return 0;
}

double Harmonic_Mean(double x, double y)
{
	return (double)(1 / ((1 / x + 1 / y) / 2));
}

9-5

//把两个double烈性的值替换为较大的

void lager_of(double * x, double * y); //函数原型

#include 

int main(void)
{
	double a, b;
	printf("Please enter two integers: \n");
	while (scanf("%lf %lf", &a, &b) == 2)
	{
		lager_of(&a, &b);
		printf("The two integers should be %lf and %lf.\n", a, b);
		printf("Please enter the next two integers: \n");
	}
	printf("Job Down!");

	return 0;
}

void lager_of(double * x, double * y)
{
	if (*x > *y)
		*y = *x;
	else
		*x = *y;
}

9.6

//编写并测试一个函数,该函数以3个double变量的地址作为参数
//最小值放入第一个函数,中间值放入第2个变量,最大值放入第三个变量

#include 

void sorted(double * x, double * y, double * z);// 函数原型

int main(void)
{
	double i, j, z;

	printf("Please enter three integers: \n");
	while (scanf("%lf %lf %lf", &i, &j, &z) == 3)
	{
		sorted(&i, &j, &z);
		printf("The min number is %lf.\n", i);
		printf("The middle number is %lf.\n", j);
		printf("The max number is %lf.\n", z);
		printf("Please enter next three integers: \n");
	}
	printf("Job Down!");

	return 0;
}

void sorted(double * x, double * y, double * z)
{
	double t;
	if (*x > *y)
	{
		t = *x;
		*x = *y;
		*y = t;
	}
	if (*x > *z)
	{
		t = *x;
		*x = *z;
		*z = t;
	}
	if (*y > *z)
	{
		t = *y;
		*y = *z;
		*z = t;
	}

}

9.7

#include 

int return_num(char ch); //函数原型

int main(void)
{
	char ch;
	printf("Enter the sentence: \n");
	while((ch = getchar()) != EOF )
		printf("%d\n", return_num(ch)); //-1为换行符

	return 0;
}

int return_num(char ch)
{
	if ((ch >= 'a') && (ch <= 'z'))
		return ch - 'a' + 1;
	if ((ch >= 'A') && (ch <= 'Z'))
		return ch - 'A' + 1;
	else
		return -1;
}

剩余未做

你可能感兴趣的:(C编程练习)