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)