C Primer Plus 第6版 Chapter 5 课后编程练习

答案为本人自己编写,仅供大家学习参考,如有错误,欢迎大家在评论区留言指正。

ex5.1

// ex_5.1
#include <stdio.h>
#define HOUR_TO_MINUTES 60

int main(void)
{
    int total_minutes = 1;
    int total_hours = 0;
    int current_minutes = 0;

    printf("Enter the time in minutes(<= 0 to quit): ");
    scanf("%d", &total_minutes);
    while (total_minutes > 0)
    {
        total_hours = total_minutes / HOUR_TO_MINUTES;
        current_minutes = total_minutes % HOUR_TO_MINUTES;
        printf("%d minutes is equate to %d Hours %d Minutes\n", total_minutes, total_hours , current_minutes);
        printf("Enter the time in minutes(<= 0 to quit): ");
        scanf("%d", &total_minutes);
    }
    printf("Quit");

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.2

// ex_5.2
#include <stdio.h>

int main(void)
{
    int num = 0;

    printf("Enter an integer: ");
    scanf("%d", &num);
    for (int i = 0; i <= 10; i++)
        printf("%d ", num++);

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.3

// ex_5.3
#include <stdio.h>
#define WEEK_TO_DAYS 7

int main(void)
{
    int total_days = 1;
    int total_weeks = 0;
    int current_days = 0;

    printf("Enter the total days(<= 0 to quit): ");
    scanf("%d", &total_days);
    while (total_days > 0)
    {
        total_weeks = total_days / WEEK_TO_DAYS;
        current_days = total_days % WEEK_TO_DAYS;
        printf("%d days are %d weeks, %d days.\n", total_days, total_weeks , current_days);
        printf("Enter the total days(<= 0 to quit): ");
        scanf("%d", &total_days);
    }
    printf("Quit");

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.4

// ex_5.4
#include <stdio.h>
#define INCH_TO_CM 2.539
#define FOOT_TO_INCH 12

int main(void)
{
    float height_in_cm = 0;
    int current_feet = 0;
    float current_inches = 0;

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

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.5

// ex_5.5
#include <stdio.h>

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

    printf("Enter how many days: ");
    scanf("%d", &days);
    while (count++ < days)
        sum = sum + count;
    printf("sum = %d\n", sum);

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.6

// ex_5.6
#include <stdio.h>
#define SQUARE(x) ((x) * (x))

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

    printf("Enter how many days: ");
    scanf("%d", &days);
    while (count++ < days)
        sum = sum + SQUARE(count);
    printf("sum = %d\n", sum);

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.7

// ex_5.7
#include <stdio.h>
double cube(double);// 函数原型

int main(void)
{
    double input_num = 0;
    double output_num = 0;

    printf("Enter a number: ");
    scanf("%lf", &input_num); // scanf 一个double时一定要用%lf
    output_num = cube(input_num);
    printf("The cubed value of %.2f is %.2f.\n", input_num, output_num);

    return 0;
}

double cube(double n) // 函数定义
{
     return n * n * n;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.8

// ex_5.8
#include <stdio.h>

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

    return 0;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

ex5.9

#include<stdio.h>
int Temperatures(double f);

int main(void)
{
    double f = 0;

    printf("Please enter a Fahrenheit:");
    while((scanf("%lf",&f)) == 1)
    {
        Temperatures(f);
        printf("Please enter next Fahrenheit(q to quit):");
    }

    return 0;
}

int Temperatures(double f)
{
    double c,k;
    const double c1 = 5.0;
    const double c2 = 9.0;
    const double c3 = 32.0;
    const double k1 = 273.16;
    c = c1 / c2 * (f - c3);
    k = c + k1;
    printf("The Fahrenheit is %.2f.\n", f);
    printf("The Celsius is %.2f.\n", c);
    printf("The Kelvin temperature is %.2f.\n", k);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/seeleday/article/details/82863761