c primer plus 第四章编程练习

2018-04-08  本文已影响20人  Leonzai

1

#include <stdio.h>

int main(void) {
    char first_name[20];
    char last_name[20];

    printf("your first name:\n");
    scanf("%s", first_name);
    printf("your last name:\n");
    scanf("%s", last_name);
    printf("%s,%s", last_name, first_name);

    return 0;
}

2

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

int main(void) {
    char first_name[20];

    printf("your first name:\n");
    scanf("%s", first_name);
    printf("\"%s\"\n", first_name);
    printf("%20s\n", first_name);
    printf("%-20s\n", first_name);

    int len = strlen(first_name);
    len += 3;
    printf("%*s", len, first_name);

    return 0;
}

3

#include <stdio.h>

int main(void) {
    float input;

    printf("input a floating-point number:\n");
    scanf("%f", &input);
    printf("%.1f\n", input);
    printf("%.1e\n", input);

    scanf("%f", &input);
    printf("%.3f\n", input);
    printf("%.3e\n", input);

    return 0;
}

4

#include <stdio.h>

#define INCH_TO_CM 2.54

int main(void) {
    float inch;
    char name[20];

    printf("name:\n");
    scanf("%s", name);
    printf("height:\n");
    scanf("%f", &inch);
    printf("%s, you are %.3f cm tail", name, inch / INCH_TO_CM);

    return 0;
}

5

#include <stdio.h>

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

    printf("your network speed: (bit) ");
    scanf("%f", &network_speed);

    printf("size of your download file: (Byte) ");
    scanf("%f", &file_size);

    download_time = file_size / (network_speed / 8);

    printf("At %.2f megabits per secnod, a file of %.2f magebytes\n download "
           "in %.2f seconds.\n", network_speed, file_size, download_time);

    return 0;
}

6

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

int main(void) {
    char first_name[20];
    char last_name[20];
    int length_first, length_last;

    printf("your first name:\n");
    scanf("%s", first_name);
    printf("your last name:\n");
    scanf("%s", last_name);

    length_first = strlen(first_name);
    length_last = strlen(last_name);

    printf("%s %s\n", last_name, first_name);
    printf("%*d %*d\n\n", length_last, length_last, length_first, length_first);

    printf("%s %s\n", last_name, first_name);
    printf("%-*d %-*d\n\n", length_last, length_last, length_first, length_first);

    return 0;
}

7

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

int main(void) {
    double a = 1.0 / 3.0;
    float b = 1.0 / 3.0;
    printf("the value of FLT_DIG:%d, the value of DBL_DIG:%d\n", FLT_DIG, DBL_DIG);
    printf("-------- a ---------\n");
    printf("%.4e\n", a);
    printf("%.12e\n", a);
    printf("%.16e\n\n", a);
    printf("-------- b ---------\n");
    printf("%.4f\n", b);
    printf("%.12f\n", b);
    printf("%.16f\n\n", b);

    return 0;
}

8

上一篇下一篇

猜你喜欢

热点阅读