c primer plus 第六版 第九章编程练习

(编译环境 Microsoft Visual Studio 2019)

1.

/*1.*/
#include<stdio.h>
double min(double x, double y);
int main(void)
{
    double value_1, value_2;

    printf("Please enter two decimal numbers.\n");
    scanf_s("%lf %lf", &value_1, &value_2);
    printf("The smallest is %f\n", min(value_1, value_2));

    return 0;
}

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

2.

/*2.*/
#include<stdio.h>
void chline(char ch, unsigned int i, unsigned int j);
int main(void)
{
    char ch;
    unsigned int row, column;

    printf("Please enter a character: ");
    ch = getchar();
    printf("Please enter the number of rows: ");
    scanf_s("%u", &row);
    printf("Please enter the number of columns: ");
    scanf_s("%u", &column);
    chline(ch, column, row);

    return 0;
}

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

    for (row = 0; row <j; row++)
    {
        for (column = 0; column < i; column++)
            putchar(ch);
        putchar('\n');
    }
}

3.

/*3.*/
#include<stdio.h>
void chline(char ch, unsigned int i, unsigned int j);
int main(void)
{
    char ch;
    unsigned int row, column;

    printf("Please enter a character: ");
    ch = getchar();
    printf("Please enter the number of rows: ");
    scanf_s("%u", &row);
    printf("Please enter the number of columns: ");
    scanf_s("%u", &column);
    chline(ch, column, row);

    return 0;
}

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

    for (row = 0; row <j; row++)
    {
        for (column = 0; column < i; column++)
            putchar(ch);
        putchar('\n');
    }
}

4.


/*4.*/
#include<stdio.h>
double h_average(double a, double b);
int main(void)
{
    double value_1, value_2;

    printf("Please enter two decimal numbers.\n");
    scanf_s("%lf %lf", &value_1, &value_2);
    printf("Harmonic average:  %f\n",h_average(value_1,value_2));

    return 0;
}

double h_average(double a, double b)
{
    double average;

    average = ((1 / a) + (1 / b)) / 2;

    return 1 / average;
}

5.

/*5.*/
#include<stdio.h>
void larger_of(double* a, double* b);
int main(void)
{
    double value_1, value_2;

    printf("Please enter two decimal numbers.\n");
    scanf_s("%lf %lf", &value_1, &value_2);
    printf("Originally x= %.3f y= %.3f\n", value_1, value_2);
    larger_of(&value_1, &value_2);
    printf("Now x= %.3f y=%.3f\n", value_1, value_2);

    return 0;
}

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

6.

/*6.*/
#include<stdio.h>
void sort(double* a, double* b, double* c);
void swap(double* x, double* y);
int main(void)
{
    double a, b, c;

    printf("Please enter three decimal numbers.\n");
    scanf_s("%lf %lf %lf", &a, &b, &c);
    printf("Originally a= %.3f b= %.3f c=%.3f\n", a, b, c);
    sort(&a, &b, &c);
    printf("Now a= %.3f b=%.3f c=%.3f\n", a, b, c);

    return 0;
}

void sort(double* a, double* b, double* c)
{
    if (*a > * b)
        swap(a, b);
    if (*b > * c)
        swap(b, c);
    if (*a > * b)
        swap(a, b);
}

void swap(double* x, double* y)
{
    double temp;

    temp = *x;
    *x = *y;
    *y = temp;
}

7.

/*7.*/
#include<stdio.h>
#include<ctype.h>
int position(char ch);
int main(void)
{
    char ch;
    int pos;

    printf("Please enter some text.(end with EOF)\n");
    while ((ch = getchar()) != EOF)
    {
        pos = position(ch);
        if (pos != -1)
            printf("The alphabet position of %c is %d\n", ch, pos);
        else
            printf("%c is not a letter\n", ch);
    }

    return 0;
}

int position(char ch)
{
    if (isupper(ch))
        return ch - 'A' + 1;
    else if (islower(ch))
        return ch - 'a' + 1;
    else
        return -1;
}

8.

/*8.*/
#include<stdio.h>
double power(double n, int p);
int main(void)
{
    double x, xpow;
    int exp;

    printf("Please enter a number and integer power(q to quit)\n");
    while (scanf_s("%lf %d", &x, &exp) == 2)
    {
        if (x == 0 && exp == 0)
            printf("0 to the power of 0 is undefined,processed as 1\n");
        else
        {
            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("bye\n");

    return 0;
}

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

    if (n == 0)
        return 0;
    if (p == 0)
        return 1;
    else if (p < 0)
    {
        for (per = 0; per < -p; per++)
            pow *= n;
        return 1 / pow;
    }
    else
    {
        for (per = 0; per < p; per++)
            pow *= n;
        return pow;
    }
}

9.

/*9.*/
#include<stdio.h>
double power(double n, int p);
int main(void)
{
    double x, xpow;
    int exp;

    printf("Please enter a number and integer power(q to quit)\n");
    while (scanf_s("%lf %d", &x, &exp) == 2)
    {
        if (x == 0 && exp == 0)
            printf("0 to the power of 0 is undefined,processed as 1\n");
        else
        {
            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("bye\n");

    return 0;
}

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

    if (n == 0)
        return 0;
    if (p == 0)
        return 1;
    else if (p > 0)
    {
        pow = n * power(n, p - 1);//递归要注意每级函数都有自己的变量,每级函数的变量是不变的
        return pow;
    }
    else
    {
        p = -p;
        pow = n * power(n, p - 1);
        return 1 / pow;
    }
}

10.

/*10.*/
#include<stdio.h>
void to_base_n(unsigned long n, int p);
int main(void)
{
    unsigned long number;
    int radix;

    printf("Please enter an integer and a radix(q to quit).\n");
    while (scanf_s("%lu %d", &number, &radix)==2)
    {
        if (radix < 2 || radix>10)
            printf("The radix must be between 2 and 10");
        else
        {
            printf("Conversion result: ");
            to_base_n(number, radix);
            putchar('\n');
        }
        printf("Enter an integer and a radix.(q to quit)\n");
    }
    printf("bye\n");

    return 0;
}

void to_base_n(unsigned long n, int p)
{
    int r;

    r = n % p;
    if (n >= p)
        to_base_n(n / p, p);
    printf("%d", r);
}

11.

/*11.*/
#include<stdio.h>
unsigned int fibonacci(int n);
int main(void)
{
    int n;

    printf("Please enter the number of terms in the Fibonacci sequence(q to quit): ");
    while (scanf_s("%d", &n))
    {
        if (n <= 0)
            printf("Please enter a positive integer.\n");
        else
            printf("Value: %lu\n", fibonacci(n));
        printf("Enter the number of terms: ");
    }
    printf("bye\n");

    return 0;
}

unsigned int fibonacci(int n)
{
    unsigned int last_1 = 1;
    unsigned int last_2 = 1;
    unsigned int value;
    int per;

    if (n == 1 || n == 2)
        return 1;
    else
    {
        for (per = 3; per <= n; per++)
        {
            value = last_1 + last_2;
            last_1 = last_2;
            last_2 = value;
        }
    }

    return value;
}

  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值