《C Primer Plus》第六版第三章课后题

/* 3-1*/
int main(void)
{
    float salary;

    printf("\aEnter your desired monthly salary:");
    printf(" $_______\b\b\b\b\b\b\b");
    scanf("%f", &salary);
    printf("\n\t%.2f a month is $%.2f a year.", salary, salary * 12.0);
    printf("\rGee!\n");

    return 0;
}

/* 3-2 */
int main(void)
{
    int ascii;

    printf("Enter the ascii number:\n");
    scanf("%d", &ascii);

    printf("the ascii number is %d and the character is %c\n", ascii, ascii);

    return 0;
}

/* 3-3 */
int main(void)
{
    printf("\aStartled by the sudden sound,");
    printf(" Sally shouted,\n");
    printf("\"By the Great Pumpkin, what was that!\"\n");

    return 0;
}


/* 3-4 */
int main(void)
{
    float num;
    printf("Please enter a floating-point value:");
    printf("____\b\b\b\b");
    scanf("%f", &num);

    printf("fixed-point notation: %.6f\n", num);
    printf("exponential notation: %e\n", num);
    printf("p notation: %a\n", num);

    return 0;
}

/* 3-5 */
#define SECONDS_PER_YEAR    (3.156e7)
int main(void)
{
    int age;
    double seconds;

    printf("Please enter your age:");
    scanf("%d", &age);
    //getchar();

    seconds = age * SECONDS_PER_YEAR;

    printf("you have spent %f seconds.\n", seconds);

    return 0;
}

/* 3-6 */
#define QUALITY_OF_WATER_MOLECULE   (3.0e-23)
#define PER_QUART_OF_WATER  (950)

int main(void)
{
    int quart_cnt;
    double molecule_cnt, quart_qty;

    printf("How many quart of water:");
    scanf("%d", &quart_cnt);

    quart_qty = quart_cnt * PER_QUART_OF_WATER;
    molecule_cnt = quart_qty / QUALITY_OF_WATER_MOLECULE;

    printf("The %d quarts water has %.1e water molecules.\n", quart_cnt, molecule_cnt); 

    return 0;
}

/* 3-7 */
#define INCH_PER_CM (2.54)

int main(void)
{
    float height;
    float cm;

    printf("Please enter your height:");
    scanf("%f", &height);

    cm = height * INCH_PER_CM;

    printf("your height is %f inch,equals to %f centimetre.\n", height, cm);

    return 0;
}

/* 3-8 */
/* 不一定都是整杯,所以用float更合适 */
#define CUP_PER_PINT    (2)
#define OUNCE_PER_CUP   (8)
#define SPOON_PER_OUNCE (2)
#define TEASPOON_PER_SPOON  (3)

int main(void)
{
    float cup;
    float pint;
    float ounce;
    float spoon;
    float teaspoon;

    printf("Please enter the cup count:");
    scanf("%f", &cup);

    pint = cup / CUP_PER_PINT;
    ounce = cup * OUNCE_PER_CUP;
    spoon = ounce / SPOON_PER_OUNCE;
    teaspoon = spoon * TEASPOON_PER_SPOON;

    printf("%f equals to %f pint, %f ounce, %f spoon, %f teaspoon.\n", cup, pint, ounce, spoon, teaspoon);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41354745/article/details/82595321
今日推荐