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

//本博主所写的代码仅为阅读者提供参考;

//若有不足之处请提出,博主会尽所能修改;

//附上课后编程练习题目;

//若是对您有用的话请点赞或分享提供给它人;



//4.8 - 1.c

#include <stdio.h>

int main(void)
{
    
    
    char fname[20], lname[20];

    printf("Please enter your first name: ");
    scanf("%19s", fname);
    printf("Please enter your last name: ");
    scanf("%19s", lname);
    printf("Hello! %s, %s.\n", fname, lname);

    return 0;
}

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

//4.8 - 2.c

#include <stdio.h>
#include <string.h>

int main(void)
{
    
    
    int len = 0;
    char name[20];

    printf("Please enter your name:");
    scanf("%19s", name);
    len = strlen(name);
    printf("Print your name:\n");
    printf("a.\"%s\"\n", name);
    printf("b.\"%20s\"\n", name);
    printf("c.\"%-20s\"\n", name);
    printf("d.%*s\n", len + 3, name);

    return 0;
}

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

//4.8 - 3.c

#include <stdio.h>

int main(void)
{
    
    
    float num;

    printf("Please enter a float number:");
    scanf("%f", &num);
    printf("The input is %.1f or %.1e.\n", num, num);

    return 0;
}

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

//4.8 - 4.c

#include <stdio.h>
#define LEN 30

int main(void)
{
    
    
    float heigh;
    char name[LEN];

    printf("Please enter your name:");
    scanf("%29s", name);
    printf("Hello! %s, how tall you are(inch):", name);
    scanf("%f", &heigh);
    printf("%s, you are %.3f feet tall.\n", name, heigh / 12.0);

    return 0;
}

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

//4.8 - 5.c

#include <stdio.h>
#define BIT 8

int main(void)
{
    
    
    float speed, size, time;

    printf("Please enter net speed(Mbit/s):");
    scanf("%f", &speed);
    printf("Please enter file size(MB):");
    scanf("%f", &size);
    time = size * BIT / speed;
    printf("At %.2f megabits per secnod, ", speed);
    printf("a file of %.2f megabytes ", size);
    printf("downloads in %.2f seconds.\n", time);

    return 0;
}

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

//4.8 - 6.c

#include <stdio.h>
#include <string.h>

int main(void)
{
    
    
    int x, y;
    char fname[20], lname[20];

    printf("Please enter your first name: ");
    scanf("%19s", fname);
    printf("Please enter your last name: ");
    scanf("%19s", lname);
    x = strlen(fname);
    y = strlen(lname);
    printf("%s %s\n", fname, lname);
    printf("%*d %*d\n", x, x, y, y);
    printf("%s %s\n", fname, lname);
    printf("%-*d %-*d\n", x, x, y, y);

    return 0;
}

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

//4.8 - 7.c

#include <stdio.h>
#include <float.h>

int main(void)
{
    
    
    float f_value = 1.0 / 3.0;
    double d_value = 1.0 / 3.0;

    printf("1.0 / 3.0 display 6 decimal places:\n");
    printf("f_value = %.6f\nd_value = %.6lf\n", f_value, d_value);
    printf("\n1.0 / 3.0 display 12 decimal places:\n");
    printf("f_value = %.12f\nd_value = %.12lf\n", f_value, d_value);
    printf("\n1.0 / 3.0 display 16 decimal places:\n");
    printf("f_value = %.16f\nd_value = %.16lf\n", f_value, d_value);
    printf("\nfloat and double maximum significant digits:\n");
    printf("FLT_DIG = %d, DBL_DIG = %d\n", FLT_DIG, DBL_DIG);
    //↑FLTDIG代表float有效十进制数字位数;
    //↑DBL_DIG代表double有效十进制数字位数;

    return 0;
}

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

//4.8 - 8.c

#include <stdio.h>
#define GALLON_TO_LITRE 3.785f
#define MILE_TO_KM 1.609f

int main(void)
{
    
    
    float range, oil;

    printf("Please input the range you traveled(in mile):");
    scanf("%f", &range);
    printf("Please input the oil you spend(in gallon):");
    scanf("%f", &oil);

    printf("Fuel consumptions:\n");
    printf("In USA, your oil wear is %.1f mile/gallon.\n", range / oil);
    printf("In Europe, your oil wear is ");
    printf("%.1f litre/100km.\n", (oil * GALLON_TO_LITRE) / (range * MILE_TO_KM));

    return 0;
}

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

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

猜你喜欢

转载自blog.csdn.net/m0_46181359/article/details/105289283