c primer plus 第九章 编程练习

//practice 9_1
#include 
#include 

double min(double a, double b);
int main()
{
    double a = 3, b = 9;

    printf("the minimum is %f]n", min(a, b));

    return 0;
}

double min(double a, double b)
{
    return (a > b? a:b);
}
/*********************************************************/
//practice 9_2
#include 
#include 

void chline(char ch, int row, int list);

int main()
{
    int row = 5, list = 7; //两行三列
    char ch = '@';

    chline(ch, row, list);

    return 0;

}

void chline(char ch, int row, int list)
{
    int i, j;

    for(i = 1;i <= row; i++)
        {
            for(j = 1;j <= list; j++)
                printf("%c", ch);
            printf("\n");
        }
    return;
}
/*********************************************************/
//practice 9_4
#include 
#include 

double harmnoic_mean(double num1, double num2);

int main()
{
    int num1, num2;
    num1 = 1;
    num2 = 2;

    printf("harmonic mean is %lf", harmnoic_mean(num1, num2));

    return 0;
}

double harmnoic_mean(double num1, double num2)
{
    double tmp;

    tmp = (1/num1 + 1/num2) / 2;

    return (1 / tmp);
}
/*********************************************************/
//practice 9_5
#include 
#include 

void larger_of(double * num1, double * num2);

int main()
{
    double num1, num2;
    num1 = 3;
    num2 = 9;

    larger_of(&num1, &num2);
    printf("two numbers are %lf and %lf", num1, num2);

    return 0;
}

void larger_of(double * num1, double * num2)
{
    double tmp;
    tmp = *num1 > *num2? *num1:*num2;
    *num1 = *num2 = tmp;
}
/*********************************************************/
//practice 9_6
#include 
#include 

void sorted(double * num1, double * num2, double * num3);

int main()
{
    double a,b,c;

    a = 3, b = 1, c = 2;
    sorted(&a, &b, &c);
    printf("sort num are a = %lf, b = %lf, c = %lf\n", a, b, c);

    return 0;
}

void sorted(double * num1, double * num2, double * num3)
{
    double tmp;

    if (*num1 > *num2)
    {
        tmp = *num1;
        *num1 = *num2;
        *num2 = tmp;
    }
    if (*num1 > *num3)
    {
        tmp = *num1;
        *num1 = *num3;
        *num3 = tmp;
    }
    if (*num2 > *num3)
    {
        tmp = *num2;
        *num2 = *num3;
        *num3 = tmp;
    }
    return;
}
/*********************************************************/
//practice 9_7
#include 
#include 
#include 

int alp_pos(char ch);

int main()
{
    char ch;
    int count = 0;
    int pos_alp;

    printf("please input a parse:");
    while((ch = getchar()) != '\n')    //读至换行
    {
        count++;
        pos_alp = alp_pos(ch);

        if(pos_alp != -1)
            printf("the %d char is %c , position in alphabet is %d\n", count, ch, pos_alp);
        else
            printf("the %d char is not alpha\n", count);

    }

    return 0;
}

int alp_pos(char ch)
{
    if (isalpha(ch))
        return ((int)ch - (int)'a' + 1);
    else
        return -1;
}

/*********************************************************/

 

你可能感兴趣的:(计算机)