第九章 函数 编程练习

9.1

#include <stdio.h>

double min(double, double);
int main()
{
    double x;
    double y;
    printf("Enter two numbers(q to quit):");
    while(scanf("%lf %lf", &x, &y) == 2){
        printf("The smaller number of %f and %f is %f.\n", x, y, min(x, y) );
        printf("Enter another two numbers(q to quit):");
    }
    printf("Bye!");
    
    return 0;
}

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

/* 
Enter two numbers(q to quit):5.66 8.669
The smaller number of 5.660000 and 8.669000 is 5.660000.
Enter another two numbers(q to quit):-123.05 1
The smaller number of -123.050000 and 1.000000 is -123.050000.
Enter another two numbers(q to quit):q
Bye!
*/

9-2 同 9-3

#include <stdio.h>

void chline(char, int, int);
int main()
{
    char ch;
    int i, j;
    printf("Enter a char: ");
    scanf("%c", &ch);
    printf("Enter the number of row: ");
    scanf("%d", &j);
    printf("Enter the number of column: ");
    scanf("%d", &i);
    printf("OK,here is the %c with %d row and %d column:\n", ch, i, j);
    chline(ch, i, j);
    
    return 0;
}

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

/* 
Enter a char: $
Enter the number of row: 2
Enter the number of column: 3
OK,here is the $ with 3 row and 2 column:
$$$
$$$
*/

9-4

#include <stdio.h>

double f(double, double);
int main()
{
    double x, y;
    printf("Enter two number(or q to qiut): ");
    while (scanf("%lf %lf", &x, &y) == 2){
        printf("The th_ave of %f and %f is %f.\n", x, y ,f(x,y));
        printf("Enter two number(or q to qiut): ");
    }
    printf("Bye!");
    
    return 0;
}

double f(double a, double b){
    return 1.0/((1.0/a + 1.0/b)/2.0);
}

/* 
Enter two number(or q to qiut): 2.0 3
The th_ave of 2.000000 and 3.000000 is 2.400000.
Enter two number(or q to qiut): 1 1
The th_ave of 1.000000 and 1.000000 is 1.000000.
Enter two number(or q to qiut): q
Bye!
*/

9-5

#include <stdio.h>

void larger_of(double *, double *);
int main()
{
    double x, y;
    printf("Enter two number(or q to qiut): ");
    while (scanf("%lf %lf", &x, &y) == 2){
        printf("The originaln number are x = %5f ; y = %5f.\n", x, y);
        larger_of(&x, &y);
        printf("The larger number are x = %5f ; y = %5f.\n", x, y);
        printf("Enter two number(or q to qiut): ");
    }
    printf("Bye!");
    
    return 0;
}

void larger_of(double *a, double *b){
    if (*a > *b)
        *b = *a;
    else
        *a = *b;
}

/* 
Enter two number(or q to qiut): 1.1    6.6
The originaln number are x = 1.100000 ; y = 6.600000.
The larger number are x = 6.600000 ; y = 6.600000.
Enter two number(or q to qiut): 0.1 0.0001
The originaln number are x = 0.100000 ; y = 0.000100.
The larger number are x = 0.100000 ; y = 0.100000.
Enter two number(or q to qiut): q
Bye!
*/

9-6 没时间了 后面的没做

posted @ 2023-09-02 23:01  园友3218619  阅读(6)  评论(0)    收藏  举报