《C Primer Plus》第六版第五章课后题

#include <stdio.h>
/* 5-1 */
#define S_PER_M (60)

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

    printf("Please enter the minute number:");
    scanf("%d", &min);

    while (min > 0)
    {
        hour = min / S_PER_M;
        mmin = min % S_PER_M;

        printf("%d minutes equals to %d hour %d minutes.\n", min, hour, mmin);

        printf("Please enter another minute number:");
        scanf("%d", &min);
    }

    return ;
}

/* 5-2 */
void main(void)
{
    int num, ten_num;

    printf("Please enter the integer number:");
    scanf("%d", &num);

    ten_num = num + 10;

    while (num <= ten_num)
    {
        printf("%d \t\n", num);
        num++;
    }
    return ;
}

/* 5-3 */
#define DAY_PER_WEEK (7)

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

    printf("Please enter the days:");
    scanf("%d", &days);

    while (days > 0)
    {
        week = days / DAY_PER_WEEK;
        day = days % DAY_PER_WEEK;

        printf("%d days are %d week, %d days.\n", days, week, day);

        printf("Please enter the another days:");
        scanf("%d", &days);
    }

    return ;
}

/* 5-4 */
const float CM_PER_INCH = 2.54;
const float CM_PER_FEET = 30.48;

void main(void)
{
    float height = 0;
    int feet = 0;
    float inch = 0;

    printf("Enter a height in centimeters:");
    scanf("%f", &height);

    while ( height > 0)
    {

        feet = (int) (height / CM_PER_FEET);
        inch = (height - feet * CM_PER_FEET) / CM_PER_INCH;

        printf("%.1f cm = %d feet, %.1f inchs\n", height, feet, inch);
        printf("Enter a height in centimeters(<=0 to quit):");
        scanf("%f", &height);
    }

    printf("bye\n");

    return ;
}

/* 5-5 */
int main(void)
{
    int count, sum;
    int days = 0;

    count = 0;
    sum = 0;

    printf("Please enter the days:");
    scanf("%d", &days);

    while (count++ < days)
    {
        sum = sum + count;
    }

    printf("sum = %d\n", sum);

    return 0;
}

/* 5-6 */
int main(void)
{
    int count, sum;
    int days = 0;

    count = 0;
    sum = 0;

    printf("Please enter the days:");
    scanf("%d", &days);

    while (count++ < days)
    {
        sum = sum + count * count;
    }

    printf("sum = %d\n", sum);

    return 0;
}

/* 5-7 */
void cube(double n)
{
    double num;
    num = n * n * n;

    printf(" n = %lf, cube n = %lf.\n", n, num);

    return ;
}

int main(void)
{
    double n;

    printf("Enter the double type number:");
    scanf ("%lf", &n);

    cube(n);

    return 0;
}

/* 5-8 */
void main(void)
{
    const int sec_num;
    int fir_num;

    printf("This program computes moduli.\n");
    printf("Enter an integer to serve as the second operand:");
    scanf("%d", &sec_num);
    printf("Now enter the first operand:");
    scanf("%d", &fir_num);

    while (fir_num > 0)
    {
        printf("%d %% %d is %d\n", fir_num, sec_num, fir_num % sec_num);

        printf("Enter next number for first operand(<=0 to quit):");
        scanf("%d", &fir_num);
    }

    printf("Done\n");

    return ;
}

/* 5-9 */
void Temperatures(double Celsius_temp)
{
    const double Fahrenheit_val = 32.0;
    const double Kelvin_val = 273.16;
    const double Fahrenheit_temp = 5.0 / 9.0 * (Celsius_temp - Fahrenheit_val);
    const double Kelvin_temp = Celsius_temp + Kelvin_val;

    printf("Celsius:%.2lf   Fahrenheit:%.2lf    Kelvin:%.2lf\n", Celsius_temp, Fahrenheit_temp, Kelvin_temp);

    return ;
}

int main(void)
{
    double temp;

    printf("Please enter the Celsius temperature(q to quit):");

    while ((scanf("%lf", &temp)) != 0)
    {
        Temperatures(temp);

        printf("Please enter the next Celsius temperature(q to quit):");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41354745/article/details/82665129