C Primer Plus 编程习题(第9章)

1

#include<stdio.h>
double min(double x, double y);

int main(void)
{
    double a, b;

    printf("Please enter two double numbers (q to quit):");
    while ((scanf("%lf %lf", &a, &b)) == 2)
    {
        printf("The min number between %lf and %lf is %lf.\n", a, b, min(a, b));
        printf("Enter again (q to quit):");
    }
    printf("Bye!\n");
    return 0;
}

double min(double x, double y)
{
    double min = x;
    if (y < x)
        min = y;
    return min;
}

2

#include<stdio.h>
void chline(char ch, int i, int j);

int main(void)
{
    char ch;
    int i, j;
    printf("Please enter a character , cols and rows (q to quit):\n");
    while ((scanf("%c %d %d", &ch, &i, &j) == 3))
    {
        chline(ch, i, j);
        printf("Please  enter again (q to quit):\n");
    }
    printf("Bye!\n");

    return 0;
}

void chline(char ch, int num1, int num2)
{
    int i, j;

    for (j = 1; j <= num2; j++)
        {
            for (i = 1; i <=num1; i++)
            {
                putchar(ch);
            }
            printf("\n");
        }
}

3

#include<stdio.h>
void chline(char ch, int i, int j);

int main(void)
{
    char ch;
    int i, j;
    printf("Please enter a character , cols and rows (q to quit):\n");
    while ((scanf("%c %d %d", &ch, &i, &j) == 3))
    {
        chline(ch, i, j);
        printf("Please  enter again (q to quit):\n");
    }
    printf("Bye!\n");

    return 0;
}

void chline(char ch, int num1, int num2)
{
    int i, j;

    for (j = 1; j <= num2; j++)
        {
            for (i = 1; i <=num1; i++)
            {
                putchar(ch);
            }
            printf("\n");
        }
}

4

#include<stdio.h>
double thpjs(double x, double y);

int main(void)
{
    double x, y;

    printf("请输入两个浮点数(q to quit):\n");
    while ((scanf("%lf %lf", &x, &y)) == 2)
    {
        printf("%lf和%lf的调和平均数为%lf.\n", x, y, thpjs(x, y));
        printf("Please enter again (q to quit):\n");
    }
    printf("Done!\n");

    return 0;
}

double thpjs(double x, double y)
{
    double temp1, temp2, sum1, sum2;

    temp1 = 1.0 / x;
    temp2 = 1.0 / y;
    sum1 = (temp1 + temp2) / 2.0;
    sum2 = 1.0 / sum1;
    return sum2;
}

5

#include<stdio.h>
void larger_of(double x, double y);

int main(void)
{
    double x, y;
    printf("请输入两个浮点数(q to quit):\n");
    while ((scanf("%lf %lf", &x, &y)) == 2)
    {
        larger_of(x, y);
        printf("Please enter again (q to quit):\n");
    }
    printf("Bye!\n");

    return 0;
}

void larger_of(double x, double y)
{
    double max = x;

    printf("Original x = %lf and y = %lf.\n", x, y);
    if (y >= x)
    {
        max = y;
        x = max;
    }
    else
        y = max;
    printf("Now x = %lf and y = %lf.\n", x, y);
}

6

#include<stdio.h>
void min_to_max(double *x, double *y, double *z);

int main(void)
{
    double x, y, z;

    printf("Please enter three double numbers (q to quit):\n");
    while ((scanf("%lf %lf %lf", &x, &y, &z)) == 3)
    {
        min_to_max(&x, &y, &z);
        printf("Please enter again (q to quit):\n");
    }
    printf("Done!\n");

    return 0;
}

void min_to_max(double *x, double *y, double *z)
{
    double min = *x;
    double middle = *y;
    double max = *z;
    if (min >= middle && min >= max)
    {
        max = *x;
        if (middle >= max)
        {

            min = *z;
        }
        else
        {
            min = *y;
            middle = *z;

        }
    }
    else if (middle >= min && middle >= max)
    {
        max = *y;
        if (min >= max)
        {
            middle = *x;
            min = *z;
        }
        else
            middle = *z;
    }
    else if (max >= middle && max >= min)
    {
        if (min >= middle)
        {
            min = *y;
            middle = *x;
        }
    }
    printf("min = %lf, middle = %lf, max = %lf\n", min, middle, max);
}

7

#include<stdio.h>
#include<ctype.h>
int jug_alpha(char ch);

int main(void)
{
    char ch;
    while ((ch = getchar()) != EOF)
    {
        printf("%d", jug_alpha(ch));
    }

    return 0;
}

int jug_alpha(char ch)
{
    int back;

    if (isalpha(ch))
    {
        if (islower(ch))
             back = ch - 96;
        if (isupper(ch))
             back = ch - 64;
    }
    else
        back = -1;
    return back;
}

8

#include<stdio.h>
double power(double n, int p);

int main(void)
{
    double x, xpow;
    int exp;

    printf("Enter a number and the positive integer power");
    printf(" to which\nthe number will be raised. Enter q");
    printf(" to quit.\n");
    while (scanf("%lf%d", &x, &exp) == 2)
    {
        xpow = power(x, exp);
        printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
        printf("Enter next pair of numbers or q to quit.\n");
    }
    printf("Hope you enjoyed this power trip -- bye!\n");

    return 0;
}

double power(double n , int p)
{
    double pow = 1, temp = 1;
    int i;

    if (n == 0)
    {
        if (p == 0)
            printf("0 power of 0 is undefined. So it is 1.\n");
        else
            pow = 0.0;
    }
    else if (p == 0)
        pow = 1.0;
    else if (p > 0)
    {
        for (i = 1; i <= p; i++)
            pow *= n;
    }
    else
    {
        for(i = 1; i <= (-p); i++)
            temp *= n;
        pow = 1.0 / temp;
    }
    return pow;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值