C primer plus编程练习-第9章

1.设计一个函数min(x, y), 返回两个double类型值的较小值。 在一个简单的驱动程序中测试该函数

#include<stdio.h>
double min(double x, double y);
int main(void)
{
        double a, b;

        printf("enter two numbers to get smaller: ");
        if (scanf("%lf %lf", &a, &b) == 2)
                printf("the smaller one is %g\n", min(a,b));

        return 0;
}

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

2.设计一个函数chline(ch, i, j), 打印指定的字符j行i列。 在一个简单的驱动程序中测试该函数

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

int main(void)
{
        char ch;
        int x, y;

        printf("print a character in columns x and through y\n");
        printf("enter the character: ");
        ch = getchar();
        printf("enter columns and through: ");
        scanf("%d %d", &x, &y);
        chline(ch, x, y);

        return 0;
}

void chline(char ch, int i, int j)
{
        int a, b;
        for (b = 1; b <= j; b++)
        {
                for (a = 1; a <= i; a++)
                        printf("%c", ch);
                printf("\n");
        }
}

3.编写一个函数, 接受3个参数: 一个字符和两个整数。 字符参数是待打印的字符, 第1个整数指定一行中打印字符的次数, 第2个整数指定打印指定字符的行数。 编写一个调用该函数的程序
同第2题

4.两数的调和平均数这样计算: 先得到两数的倒数, 然后计算两个倒数的平均值, 最后取计算结果的倒数。 编写一个函数, 接受两个double类型的参数, 返回这两个参数的调和平均数

#include<stdio.h>
double harmonic_mean(double, double);

int main(void)
{
        double x, y;

        printf("enter numbers except 0\n");
        if (scanf("%le %le", &x, &y) == 2)
                printf("the harmonic mean is %g\n", harmonic_mean(x, y));

        return 0;
}

double harmonic_mean(double x, double y)
{
        x = 1.0 / x;
        y = 1.0 / y;

        return (1.0 / ((x + y) / 2.0) );
}

5.编写并测试一个函数larger_of(), 该函数把两个double类型变量的值替换为较大的值。 例如, larger_of(x, y)会把x和y中较大的值重新赋给两个变量

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

int main(void)
{
        double x, y;

        printf("enter two numbers as x and y respectively\n");
        scanf("%le %le", &x, &y);
        printf("x is %g, y is %g\n", x, y);
        larger_of(&x, &y);
        printf("now x is %g, y is %g.\n", x, y);

        return 0;
}

void larger_of(double * x, double * y)
{
        if (*x > *y)
                *y = *x;
        else
                *x = *y;
}

6.编写并测试一个函数, 该函数以3个double变量的地址作为参数, 把最小值放入第1个函数, 中间值放入第2个变量, 最大值放入第3个变量

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

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

        printf("enter 3 numbers as x, y, z respectively\n");
        scanf("%le %le %le", &x, &y, &z);
        printf("x is %g, y is %g, z is %g\n", x, y, z);
        change(&x, &y, &z);
        printf("now x is %g, y is %g, z is %g\n", x, y, z);

        return 0;
}

void change(double * x, double * y, double * z)
{
        double temp;

        if (*x > *y)
        {
                temp = *x;
                *x = *y;
                *y = temp;
        }
        if (*x > *z)
        {
                temp = *x;
                *x = *z;
                *z = temp;
        }
        if (*y > *z)
        {
                temp = *y;
                *y = *z;
                *z = temp;
        }
}

7.编写一个函数, 从标准输入中读取字符, 直到遇到文件结尾。 程序要报告每个字符是否是字母。 如果是, 还要报告该字母在字母表中的数值位置。 例如, c和C在字母表中的位置都是3。 合并一个函数, 以一个字符作为参数, 如果该字符是一个字母则返回一个数值位置, 否则返回-1

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

int main(void)
{
        char ch;
        int value;

        while ((ch = getchar()) != EOF)
        {
                value = location(ch);
                if (value == -1)
                        printf("%c~N~", ch);
                else
                        printf("%c~Y~", ch);
                printf("%d ", value);
        }

        return 0;
}

int location(char ch)
{
        int value;

        if (ch >= 'a' && ch <= 'z')
                value = ch - 'a' + 1;
        else if (ch >= 'A' && ch <= 'Z')
                value = ch - 'A' + 1;
        else
                value = -1;

        return value;
}

8.第6章的程序清单6.20中, power()函数返回一个double类型数的正整数次幂。 改进该函数, 使其能正确计算负幂。 另外, 函数要处理0的任何次幂都为0, 任何数的0次幂都为1(函数应报告0的0次幂未定义, 因此把该值处理为1) 。 要使用一个循环, 并在程序中测试该函数

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

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

        printf("Enter a number and a integer power to which\n"
                        "the number will be raised. Enter q to quit.\n");
        while (scanf("%le %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;
        int i;

        if (n != 0)
        {
                if (p > 0)
                        for (i = 1; i <= p; i++)
                                pow *= n;
                else if (p < 0)
                {
                        for (i = 1; i <= -p; i++)
                                pow *= n;
                        pow = 1.0 / pow;
                }
                else
                        pow = 1.0;
        }
        else
        {
                if (p != 0)
                        pow = 0;
                else
                {
                        printf("0 to 0 is undefined\n");
                        pow = 1;
                }
        }

        return pow;
}

9.使用递归函数重写编程练习8

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

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

        printf("Enter a number and a integer power to which\n"
                        "the number will be raised. Enter q to quit.\n");
        while (scanf("%le %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;

        if (n != 0)
        {
                if (p > 0)
                        pow = rpow(n, p);
                else if (p < 0)
                {
                        pow = rpow(n, -p);
                        pow = 1.0 / pow;
                }
                else
                        pow = 1.0;
        }
        else
        {
                if (p != 0)
                        pow = 0;
                else
                {
                        printf("0 to 0 is undefined\n");
                        pow = 1.0;
                }
        }

        return pow;
}

double rpow(double n, int p)
{
        double pow;

        if (p > 1)
                pow = n * rpow(n, p - 1);
        else
                pow = n;

        return pow;
}

10.为了让程序清单9.8中的to_binary()函数更通用, 编写一个to_base_n()函数接受两个在2~10范围内的参数, 然后以第2个参数中指定的进制打印第1个参数的数值。 例如, to_base_n(129, 8)显示的结果为201, 也就是129的八进制数。 在一个完整的程序中测试该函数

#include<stdio.h>
void to_base_n(unsigned long n, int base);

int main(void)
{
        unsigned long number;
        int base;

        printf("Enter an integer and another integer in the range 2-10 as base (q to quit)\n");
        while (scanf("%lu %d", &number, &base) == 2)
        {
                printf("base-%d equivalent: ", base);
                to_base_n(number, base);
                putchar('\n');
                printf("Enter an integer and another integer in the range 2-10 as base (q to quit)\n");
        }
        printf("Done\n");

        return 0;
}

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

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

        return;
}

11.编写并测试Fibonacci()函数, 该函数用循环代替递归计算斐波那契数

#include<stdio.h>
unsigned long Fibonacci(unsigned n);

int main(void)
{
        unsigned long number;

        printf("enter a positive integer to show the Fibonacci function\n");
        scanf("%lu", &number);
        printf("Fibonacci(%lu) = %lu\n", number, Fibonacci(number));

        return 0;
}

unsigned long Fibonacci(unsigned n)
{
        int value1, value2, value;
        int i;
        value1 = value2 = 1;
        if (n > 2)
        {
                for (i = 3; i <= n; i++)
                {
                        value = value1 + value2;
                        value1 = value2;
                        value2 = value;
                }
        }
        else
                value = 1;

        return value;
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读