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

部分习题代码丢失,需要请联系博主。
编译环境:Visual Studio 2017

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
//1
double minn(double x, double y);
int main(void)
{
    double a, b;
    while ((scanf("%lf %lf", &a, &b)) == 2)
    {
        a = minn(a, b);
        printf("min=%lf\n", a);
    }
    system("pause");
    return 0;
}
double minn(double x, double y)
{
    if (x < y)
        return x;
    else
        return y;
}
//2
void chline(char ch, int a, int b);
int main(void)
{
    int a, b;
    char ch;
    while ((scanf("%c %d %d", &ch, &a, &b)) == 3)
    chline(ch, a, b);
    system("pause");
    return 0;
}
void chline(char ch, int a, int b)
{
    int i;
    for (;a>0;a--)
    {
        for (i=0; i<b; i++)
            printf("%c", ch);
        printf("\n");
    }
}
//3
void chlist(char ch, int a, int b);
int main(void)
{
    int a, b;
    char ch;
    while ((scanf("%c %d %d", &ch, &a, &b)) == 3)
    chlist(ch, a, b);
    system("pause");
    return 0;
}
void chlist(char ch, int a, int b)
{
    int i;
    for (;b>0;b--)
    {
        for (i=0; i<a; i++)
            printf("%c", ch);
        printf("\n");
    }
}
//4
double tiao(double x, double y);
int main(void)
{
    double a, b;
    while ((scanf("%lf %lf", &a, &b)) == 2)
    {
        a = tiao(a, b);
        printf("tiao=%lf\n", a);
    }
    system("pause");
    return 0;
}
double tiao(double x, double y)
{
    double z;
    z = 1 / ((1 / x + 1 / y) / 2);
    return z;
}
//5
void maxx(double *p_1, double *p_2);
int main(void)
{
    double x, y;
    while ((scanf("%lf %lf", &x, &y)) == 2)
    maxx(&x, &y);
    system("pause");
    return 0;
}
void maxx(double *p_1, double *p_2)
{
    if (*p_1 < *p_2)
        *p_1 = *p_2;
    else
        *p_2 = *p_1;
    printf("x=%lf,y=%lf\n", *p_1, *p_2);
}
//6
void paixv(double *p_1, double *p_2, double *p_3);
int main(void)
{
    double x, y, z;
    while((scanf("%lf %lf %lf",&x,&y,&z))==3)
    paixv(&x, &y, &z);
    system("pause");
    return 0;
}
void paixv(double *p_1, double *p_2, double *p_3)
{
    double test;
    if (*p_1 > *p_2)
    {
        test = *p_1;//可以专门写一个用于交换的函数
        *p_1 = *p_2;
        *p_2 = test;
    }
    if (*p_1 > *p_3)
    {
        test = *p_1;
        *p_1 = *p_3;
        *p_3 = test;
    }
    if (*p_2 > *p_3)
    {
        test = *p_2;
        *p_2 = *p_3;
        *p_3 = test;
    }
    printf("%lf %lf %lf\n", *p_1, *p_2, *p_3);
}
//7
void duqv(char ch);
int main(void)
{
    char ch;
    while ((ch = getchar()) != EOF)
        duqv(ch);
    system("pause");
    return 0;
}
void duqv(char ch)
{
    int i;
    if (isalpha(ch))
        i = toupper(ch) - 64;
    else
        i = -1;
    printf("%d ", i);
}
//8
double pow(double x, double y);
int main(void)
{
    double x, y;
    while ((scanf("%lf %lf", &x, &y)) == 2)
    {
        if (x == 0 && y == 0)
        {
            printf("0的0次幂未定义\n");
            continue;
        }
        else x = pow(x, y);
        printf("The result is %lf\n", x);
    }
    system("pause");
    return 0;
}
double pow(double x, double y)
{
    double r=1;
    if (x == 0)
        r = 0;
    else if (y == 0)
        r = 1;
    else if(y>0)
    {
        for (; y > 0; y--)
            r = r * x;
    }
    else
    {
        for (; y < 0; y++)
            r = 1/(r * x);
    }
    return r;

}
//9
double poww(double x, double y);
int main(void)
{
    double x, y;
    while ((scanf("%lf %lf", &x, &y)) == 2)
    {
        if (x == 0 && y == 0)
        {
            printf("0的0次幂未定义\n");
            continue;
        }
        if (x == 0)
            x = 0;
        else if (y == 0)
            x = 1;
        else x = poww(x, y);
        printf("the result is %lf\n", x);
    }
    system("pause");
    return 0;
}
double poww(double x, double y)
{
    double r=1;
    if(y>0)
        r = x * poww(x, y - 1);
    return r;

//11
int main(void)
{
    int i,a[10];
    a[0] = 1, a[1] = 1;
    printf("%d\n%d\n", a[0],a[1]);
    for (i = 2; i < 10; i++)
    {
        a[i] = a[i - 1] + a[i - 2];
        printf("%d\n", a[i]);
    }
    system("pause");
    return 0;
}


//测试
double qiuhe(double x, double y);
double qiuhe2(double x, double y);
int main(void)
{
    double x, y, r;
    scanf("%lf%lf", &x, &y);
    if(y>0)
    r=qiuhe(x, y);
    if(y<0) 
    r = qiuhe2(x, y);
    printf("%lf", r);   
    system("pause");
    return 0;
}
double qiuhe(double x, double y)
{
    int r=1;
    if (y > 0)
        r = x * qiuhe(x, y - 1);
    return r;
}
double qiuhe2(double x, double y)
{
    double r = 1;
    if (y < 0)
        r = x * qiuhe2(x, y + 1);
    return (1/r);
}

2018.6.1

猜你喜欢

转载自blog.csdn.net/qq_38967295/article/details/80532310