c primer plus chapter 3 exercise record

Chapter Three

1. Design a program that satisfies the overflow and underflow phenomenon of the system. When I was sorting out this, I had a similar example. Let’s unify it here.
#include<stdio.h>

int main() {

    //整型
    int i = 2147483647;
    printf("原值:%d,整型溢出现场:%d\n", i, i+1);
    unsigned j = 4294967295;
    printf("原值:%u,无符号整型溢出:%u\n", j, j+1);
    //浮点型
    printf("浮点型溢出现场\n");
    float big = 3.4e38, small = 3.4e-38;
    printf("原值:%f,上溢:%f\n", big, big*2);
    printf("原值:%.38f,下溢:%.38f\n", small, small/2);
    return 0;
}

Results of Exercise 1

  1. Write a program, input an ASCII value (integer value, such as 66), and output the corresponding character
#include<stdio.h>

int main() {

    int v;
    printf("Please input value of ascii:\n");
    scanf("%d", &v);
    printf("The character of %d is %c\n", v, v);
    return 0;
}

Results of Exercise 2

The conversion character %c means the output character, and when it corresponds to an integer variable, it outputs the corresponding character in the ascii code; the
conversion character %d means the output of a decimal integer, and when it corresponds to a character variable, it outputs the corresponding value of the ascii code, that is, the serial number

  1. Write a program that issues a warning and outputs the following text:

Startled by the sudden sound, Sally shouted,
“By the Great Pumpkin, what was that!”

#include<stdio.h>

int main() {

    printf("\aStartled by the sudden sound, Sally shouted,\n");
    printf("\"By the Great Pumpkin, what was that!\"\n");
    return 0;
}

Results of Exercise 3

The assessment here is the use of transfer sequences and escape characters. You must practice on this machine, otherwise you may not be able to hear the ringtone of program execution (in the case of using a cloud host)

  1. Write a program, read floating-point numbers, output real numbers with decimal points and scientific notation, and output hexadecimal floating-point numbers when the system supports
#include<stdio.h>

int main() {

    float i;
    printf("Please input the actual number:\n");
    scanf("%f", &i);
    printf("%f, %e\n", i, i);
    printf("hex: %a\n", i);
    return 0;
}

Results of Exercise 4

  1. Write a program that prompts the user to enter the age, and then displays the number of seconds corresponding to the age (about 3.156x 1 0 7 10^7 in one year107 seconds)
#include<stdio.h>

int main() {

    int age;
    float sec_each_year = 3.156e7;
    printf("Input your age:\n");
    scanf("%d", &age);
    printf("It has passed %f sec since you were born.\n", age*sec_each_year);
    return 0;
}

Results of Exercise 5

  1. The mass of water molecules is 3.0x10^-23 grams, and one quark is about 950 grams. Write a program that prompts the user to input the number of quarks, and then outputs the corresponding number of water molecules.
#include<stdio.h>

int main() {

    int quark, quark_each_gram = 950;
    float num_each_gram = 3.0e-23;
    printf("Input the quarks:\n");
    scanf("%d", &quark);
    printf("%d夸克有%e个水分子\n", quark, quark*quark_each_gram/
           num_each_gram);
    return 0;
}

Results of Exercise 6

  1. 1 inch is equivalent to 2.54 centimeters. Write a program to output the height value in inches according to the input height value in centimeters.
#include<stdio.h>

int main() {

    float centimetre_each_inch = 2.54, height;
    printf("Input your height: \n");
    scanf("%f", &height);
    printf("%f cm, %f inch\n", height, height/centimetre_each_inch);
    return 0;
}

Because the measurement unit used in our country is centimeters, I changed the title and the output is as follows:
Results of Exercise 7

8. Similar to the above, more similar to description questions, no description.

Guess you like

Origin blog.csdn.net/weixin_44948269/article/details/127329904