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

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

ex4.1

// ex_4.1
#include <stdio.h>

int main(void)
{
    char firstName[12];
    char lastName[12];
    printf("Enter your first name: ");
    scanf("%s", &firstName);
    printf("Enter your last name: ");
    scanf("%s", &lastName);
    printf("%s, %s", lastName, firstName);

    return 0;
}

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

ex4.2

// ex_4.2
#include <stdio.h>
#include <string.h>
char name[20];
int stl = 0;
int main(void)
{
    printf("Enter your first name: ");
    scanf("%s", name);
    stl = strlen(name);
    printf("\"%s\"\n", name);
    printf("\"%20s\"\n", name);
    printf("\"%-20s\"\n", name);
    printf("\"%*s\"\n", stl + 3, name);

    return 0;
}

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

ex4.3

// ex_4.3
#include <stdio.h>

int main(void)
{
    float num = 21.290;
    printf("a. The input is %.1f or %.1e.\n", num, num);
    printf("b. The input is +%.3f or %.3E.\n", num, num);

    return 0;
}

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

ex4.4

// ex_4.4
#include <stdio.h>
#define CENTIMETER_TO_METER  0.01

int main(void)
{
    float height_in_meters = 0;
    float height_in_centimeters = 0;
    char name[40];
    printf("Enter your height in centimeters(e.g. 170): ");
    scanf("%f", &height_in_centimeters);
    printf("Enter your name (e.g. Dabney):");
    scanf("%s", name);
    height_in_meters = height_in_centimeters * CENTIMETER_TO_METER;
    printf("%s, you are %.2f meters tall.\n",name, height_in_meters);

    return 0;
}

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

ex4.5

// ex_4.5
#include <stdio.h>
#define BITS_TO_BYTE 1.0/8

int main(void)
{
    float download_speed = 0;
    float file_size = 0;
    float download_time = 0;

    printf("Enter your download speed in megabits per second (Mbs): ");
    scanf("%f", &download_speed);
    printf("Enter your the size of a file in megabytes (MB): ");
    scanf("%f", &file_size);

    download_time = (file_size / BITS_TO_BYTE) / download_speed;
    printf("At %.2f megabits per second, a file of %.2f megabytes\ndownloads in %.2f seconds.", download_speed, file_size, download_time);

    return 0;
}

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

ex4.6

// ex_4.6
#include <stdio.h>
#include <string.h>

int main(void)
{
    char first_name[20];
    char last_name[20];
    int stl_f_name = 0;
    int stl_l_name = 0;

    printf("Enter your first name: ");
    scanf("%s", first_name);
    printf("Enter your last name: ");
    scanf("%s", last_name);
    stl_f_name = strlen(first_name);
    stl_l_name = strlen(last_name);

    printf("%s %s\n", first_name, last_name);
    printf("%*d %*d\n", stl_f_name, stl_f_name, stl_l_name, stl_l_name);
    printf("%s %s\n", first_name, last_name);
    printf("%-*d %-*d\n", stl_f_name, stl_f_name, stl_l_name, stl_l_name);

    return 0;
}

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

ex4.7

// ex_4.7
#include <stdio.h>
#include <float.h>

int main(void)
{
    double a = 1.0/3.0;
    float b = 1.0/3.0;

    printf("DOUBLE  %.4f  %.12f  %.16f\n", a, a, a);
    printf("FLOAT   %.4f  %.12f  %.16f\n", b, b, b);
    printf("FLT_DIG %.4f  %.12f  %.16f\n", FLT_DIG, FLT_DIG, FLT_DIG);
    printf("DBL_DIG %.4f  %.12f  %.16f\n", DBL_DIG, DBL_DIG, DBL_DIG);

    return 0;
}

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

ex4.8

// ex_4.8
#include <stdio.h>
#define MILE_TO_KM 1.609
#define GALLON_TO_LITER 3.785

int main(void)
{
    float mile = 0;
    float gasoline_in_gallon = 0;
    float miles_per_gallon = 0;
    float liter_per_100km = 0;

    printf("Enter miles you traveled: ");
    scanf("%f", &mile);
    printf("Enter number of gallons of gasoline consumed: ");
    scanf("%f", &gasoline_in_gallon);

    miles_per_gallon = mile / gasoline_in_gallon;
    liter_per_100km = 100 * (gasoline_in_gallon * GALLON_TO_LITER) / (mile * MILE_TO_KM);
    printf("European scheme(miles-per-gallon): %.3f\n", miles_per_gallon);
    printf("U.S. scheme (liters-per-100-km)  : %.3f\n", liter_per_100km);

    return 0;
}

猜你喜欢

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