C Primer Plus第六版第九章编程题目与参考答案⭐

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

#include <stdio.h>

double min(double x,double y); 
int main() 
{
	double a,b;
    printf("请您输入2个数,中间用英文逗号隔开(按q退出本程序):");
    while (scanf("%lf,%lf", &a, &b) == 2)
    {
        printf("最小的数是:%g\n", min(a, b));
        printf("您可以再次输入2个数(或按q退出):");
    }
    printf("本程序完成!\n");
	return 0;
}

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

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

#include <stdio.h>
#include <ctype.h>

void chline(int ch, int cols, int rows);
int main(void)
{
    int str, i, j;

    printf("请输入一个字符:");
    do
    {
        str = getchar();
    } while (isspace(str)); 
    while (getchar() != '\n')
        continue;
    printf("请输入2个正整数(按q退出本程序):");
    while ((scanf("%d %d", &j, &i)) == 2)
    {
        if ((j <= 0) || (i <= 0))
        {
            printf("数据无效!请重新输入:");
            continue;
        }
        printf("打印%c的%d行%d列是:\n", str, j, i);
        chline(str, i, j);
        printf("您可以再次输入2个正整数(或按q退出):");
    }
    printf("本程序完成!\n");
    return 0;
}

void chline(int ch, int cols, int rows)
{
    int a, b;
    for (a = 1; a <= rows; a++)
    {
        for (b = 1; b <= cols; b++)
        {
            putchar(ch);
        }
        putchar('\n');
    }
    return;
}

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

#include <stdio.h>
#include <ctype.h>

void show(int ch, int cols, int rows);

int main(void)
{
    int str, i, j;

    printf("请输入一个字符:\n");
    do
    {
        str = getchar();
    } while (isspace(str)); 
    while (getchar() != '\n')
        continue;

    printf("请输入2个正整数(按q退出本程序):");
    while ((scanf("%d %d", &i, &j)) == 2)
    {
        if ((i <= 0) || (j <= 0))
        {
            printf("数据无效!请重新输入:");
            continue;
        }
        printf("打印%c的%d行%d列是:\n", str, j, i);
        show(str, j, i);
        printf("您可以再次输入2个正整数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void show(int ch, int cols, int rows)
{
    int a, b;

    for (a = 1; a <= rows; a++)
    {
        for (b = 1; b <= cols; b++)
        {
            putchar(ch);
        }
        putchar('\n');
    }
    return;
}

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

#include <stdio.h>

double harmean(double x, double y);

int main(void)
{
    double a, b;

    printf("请您输入2个数(按q退出本程序):");
    while ((scanf("%lf %lf", &a, &b)) == 2)
    {
        printf("%g和%g的调和平均数是:%g\n", a, b, harmean(a, b));
        printf("您可以再次输入2个数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

double harmean(double x, double y)
{
    return 2 / (1 / x + 1 / y);
}

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

#include <stdio.h>

void larger_of(double *x, double *y);

int main(void)
{
    double a, b;

    printf("请输入2个数(按q退出本程序):");
    while (scanf("%lf %lf", &a, &b) == 2)
    {
        larger_of(&a, &b);
        printf("替换成较大的值是%g和%g\n", a, b);
        printf("您可以再次输入2个数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void larger_of(double *x, double *y)
{
    *x = *y = (*x > *y ? *x : *y);
    return;
}

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

#include <stdio.h>

void test(double *a, double *b, double *c);

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

    printf("请输入3个数(按q退出本程序):");
    while (scanf("%lf %lf %lf", &x, &y, &z) == 3)
    {
        test(&x, &y, &z);
        printf("最小值是%g\n", x);
        printf("中间值是%g\n", y);
        printf("最大值是%g\n", z);
        printf("您可以再次输入3个数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void test(double *a, double *b, double *c)
{
    double tp;

    if (*a > *b)
    {
        tp = *a;
        *a = *b;
        *b = tp;
    }
    if (*a > *c)
    {
        tp = *a;
        *a = *c;
        *c = tp;
    }
    if (*b > *c)
    {
        tp = *b;
        *b = *c;
        *c = tp;
    }
    return;
}

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

#include <stdio.h>
#include <ctype.h>

void attain_pos(void);
int position(int ch);

int main(void)
{
    attain_pos();

    return 0;
}

void attain_pos(void)
{
    int ch;
    int figure;

    printf("请您输入一些字符(遇到EOF结束):\n");
    while ((ch = getchar()) != EOF)
    {
        figure = position(ch);
        if ((figure != -1) && isupper(ch))
        {
            printf("%c是第%d个大写字母.\n", ch, figure);
        }
        else if ((figure != -1) && islower(ch))
        {
            printf("%c是第%d个小写字母.\n", ch, figure);
        }
        else
        {
            printf("%c不是字母!\n", ch); 
        }
    }
    printf("本程序完成!\n");
}

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

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

#include <stdio.h>

double power(double n, int p);

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

    printf("Enter a number and the 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)
{
    int i;
    double pow = 1.0;

    if ((0 == p) && (0 == n))
    {
        printf("0 to the 0 undefined, using 1 as the value.\n");
        return pow;
    }
    if (0 == n)
    {
        pow = 0.0;
        return pow;
    }
    if (0 == p)
    {
        return pow;
    }
    if (p > 0)
    {
        for (i = 1; i <= p; i++)
        {
            pow *= n;
        }
        return pow;
    }
    else
    {
        for (i = 1; i <= -p; i++)
        {
            pow *= 1 / n;
        }
        return pow;
    }
}

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

#include <stdio.h>

double power(double n, int p);

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

    printf("Enter a number and the 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.0;

    if ((0 == p) && (0 == n))
    {
        printf("0 to the 0 undefined, using 1 as the value.\n");
        return pow;
    }
    if (0 == n)
    {
        pow = 0.0;
        return pow;
    }
    if (0 == p)
    {
        return pow;
    }
    if (p > 0)
    {
        return n * power(n, p - 1);
    }
    else
    {
        return power(n, p + 1) / n;
    }
}

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(int x, int base);
int main(void)
{
    int b;
    long int n;
    printf("请输入一个正整数(按q退出本程序):");
    while (scanf("%ld", &n) == 1)
    {
        if (n <= 0)
        {
            printf("数据无效!请您重新输入(或按q退出):");
            continue;
        }
        printf("请输入一个需要转换的进制(2-10):");
        while ((scanf("%d", &b) != 1) || (b < 2 || b > 10))
        {
            while (getchar() != '\n')
                continue;
            printf("输入无效,进制转换的范围仅限于2-10:");
        }
        printf("%d的%d进制是:", n, b);
        to_base_n(n, b);
        printf("\n您可以再次输入一个正整数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}
void to_base_n(int x, int base)
{
    int r;

    r = x % base;
    if (x >= base)
    {
        to_base_n(x / base, base);
    }
    printf("%d", r);
    return;
}

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

#include <stdio.h>

void Fibonacci(int len);
int main(void)
{
    int n;
    printf("请您输入一个正整数(按q退出本程序):");
    while (scanf("%d", &n) == 1)
    {
        if (n <= 0)
        {
            printf("数据无效!请重新输入:");
            continue;
        }
        printf("斐波那契数列前%d项是:\n", n);
        Fibonacci(n);
        printf("你可以再次输入一个正整数(或按q退出):");
    }
    printf("本程序完成!\n");
    return 0;
}
void Fibonacci(int len)
{
    int i;
    unsigned long int t, x, y;
    x = y = 1;
    for (i = 0; i < len; i++)
    {
        printf("%-lu\n", x);
        t = x + y;
        x = y;
        y = t;
    }
    return;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值