C Primer Plus第六版 第九章 编程练习

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

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

#include <stdio.h>      //驱动程序
int main(void)
{
	double a, b;

	printf("Enter two double number('q' to quit):\n");
	while (scanf_s("%lf %lf", &a, &b) == 2)
	{
		printf("The lower number of %.3lf and %.3lf is %.3lf.\n", a, b, min(a, b));
		printf("Enter two double number('q' to quit):\n");
	}

	return 0;

}

2.设计一个函数chline(ch,i,j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。

void chline(char ch, int i, int j)
{
	int x,y;
	for (x = 1;x <= j;x++)
	{
		for (y = 1;y <= i;y++)
			putchar(ch);
		putchar('\n');

	}
}
#include <stdio.h>         //驱动程序
void chline(char ch,int i,int j);
char get_first(void);
int main(void)
{
	int a, b;     //a列数,b行数
	char c;

	printf("Enter the colums and rows of the number:\n");
	while (scanf_s("%d %d", &a, &b) == 2)
	{
		while (getchar() != '\n')  //去除输入序列中的其他字符
			continue;
		printf("Enter the number to be printed:\n");
		c = getchar();
		chline(c, a, b);
		while (getchar() != '\n')  //去除输入序列中的其他字符
			continue;
		printf("Enter the colums and rows of the number:\n");
	}

	return 0;
}

3. 编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。

void ch_print(char ch, int cols, int rows)
{
	int i, j;
	for (j = 1;j <= rows;j++)
	{
		for (i = 1;i <= cols;i++)
			printf("%c ",ch);
		printf("\n");
	}
}
#include <stdio.h>      //驱动程序
void ch_print(char ch,int col,int row);
int main(void)
{
	char c;
	int a, b;
	printf("Enter the cols and rows of the character:\n");
	while (scanf_s("%d %d", &a, &b) == 2)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值