欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C Primer Plus 第六版(中文版)第五章(完美版)编程练习答案

程序员文章站 2024-03-11 12:08:07
...

//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽所能修改;
//附上课后编程练习题目;
C Primer Plus 第六版(中文版)第五章(完美版)编程练习答案
C Primer Plus 第六版(中文版)第五章(完美版)编程练习答案
//5.11 - 1.c

#include <stdio.h>
#define MIN_PER_HOUR 60

int main(void)
{
    int m, hour, min;

    printf("请输入分钟数(<=0退出):");
    while ((scanf("%d", &m) == 1) && (m > 0))
    {
        hour = m / MIN_PER_HOUR;
        min = m % MIN_PER_HOUR;
        printf("%d分钟相当于%d小时%d分钟\n", m, hour, min);
        printf("您可以再次输入分钟数(<=0退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

//-------------

//5.11 - 2.c

#include <stdio.h>
#define LEN 10

int main(void)
{
    int n;
    int i = 0;

    printf("请您输入一个整数:");
    scanf("%d", &n);

    printf("打印从%d到比%d大10的所有整数是:\n", n, n);
    while (i <= LEN)
    {
        printf("%d\n", n + i);
        i++;
    }
    printf("本程序完成!\n");

    return 0;
}

//-------------

//5.11 - 3.c

#include <stdio.h>
#define DAYS_PER_WEEK 7

int main(void)
{
    int n, week, days;

    printf("Please enter days(<=0 to quit):");
    while ((scanf("%d", &n) == 1) && (n > 0))
    {
        week = n / DAYS_PER_WEEK;
        days = n % DAYS_PER_WEEK;
        printf("%d days are %d weeks, %d days.\n", n, week, days);
        printf("You can enter again(<=0 to quit):");
    }

    return 0;
}

//-------------

//5.11 - 4.c

#include <stdio.h>
#define CM_PER_INCH 2.54f
#define CM_PER_FEET 30.48f

int main(void)
{
    int feet;
    float cm, inch;

    printf("Enter a height in centimeters:");
    while ((scanf("%f", &cm) == 1) && (cm > 0))
    {
        feet = (int)(cm / CM_PER_FEET);
        inch = (cm - CM_PER_FEET * feet) / CM_PER_INCH;
        printf("%.1f cm = %d feet, %.1f inches\n", cm, feet, inch);
        printf("Enter a height in centimeters(<=0 to quit):");
    }
    printf("bye\n");

    return 0;
}

//-------------

//5.11 - 5.c

#include <stdio.h>

int main(void)
{
    int n, sum, count;
    sum = 0;

    printf("Enter the number of days you work:");
    scanf("%d", &count);
    n = count;
    while (count > 0)
    {
        sum += count;
        count--;
    }
    printf("You earned $%d in %d days.\n", sum, n);

    return 0;
}

//-------------

//5.11 - 6.c

#include <stdio.h>

int main(void)
{
    int n, sum, count;
    sum = 0;

    printf("Enter the number of days you work:");
    scanf("%d", &count);
    n = count;
    while (count > 0)
    {
        sum += count * count;
        count--;
    }
    printf("You earned $%d in %d days.\n", sum, n);

    return 0;
}

//-------------

//5.11 - 7.c

#include <stdio.h>

void cube(double c);

int main(void)
{
    double n;

    printf("Please you enter a double number:");
    scanf("%lf", &n);
    cube(n);

    return 0;
}

void cube(double c)
{
    printf("The cube of %g is %g.\n", c, c * c * c);
    return;
}

//-------------

//5.11 - 8.c

#include <stdio.h>

int main(void)
{
    int f_o, s_o;

    printf("This program computes moduli.\n");
    printf("Enter an integer to serve ");
    printf("as the second operand:");
    scanf("%d", &s_o);
    printf("Now enter the first operand:");
    while ((scanf("%d", &f_o) == 1) && (f_o > 0))
    {
        printf("%d %% %d is %d\n", f_o, s_o, f_o % s_o);
        printf("Enter next number for first operand(<= 0 to quit):");
    }
    printf("Done\n");

    return 0;
}

//-------------

//5.11 - 9.c

#include <stdio.h>

void Temperatures(double f_t);

int main(void)
{
    double temp;

    printf("请输入一个温度(或按q退出):");
    while (scanf("%lf", &temp) == 1)
    {
        Temperatures(temp);
        printf("您可以再次输入一个温度(按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void Temperatures(double f_t)
{
    const double f_val = 32.0;
    const double k_val = 273.16;
    double t = 5.0 / 9.0 * (f_t - f_val);
    //↑摄氏温度 = 5.0/9.0 * (华氏温度 - 32);
    double k_t = t + k_val;
    //↑开氏温度 = 摄氏温度 + 273.16;
    printf("摄氏温度:%.2lf°C\n", t);
    printf("华氏温度:%.2lf°C\n", f_t);
    printf("开氏温度:%.2lf°C\n", k_t);

    return;
}

//-------------

//----------------------------2020年4月3日 -------------------------------