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

10 篇文章 0 订阅

ex3.1

// ex_3.1
#include <stdio.h>
#include <limits.h>
#include <float.h>

int main(void)
{
    int intMax = INT_MAX;
    float floatMax = FLT_MAX;
    printf("The maximum value of int = %d\n", intMax);
    printf("The maximum value of int + 1 = %d\n", intMax + 1);
    printf("The maximum value of float = %.10e\n", FLT_MAX);

    return 0;
}

// 可以自行查看limits.h 和 float.h库的说明文档并尝试

 

ex3.2

// ex_3.2
#include <stdio.h>

int main(void)
{
    int ch;
    printf("Enter an ASCII code: ");
    scanf("%d", &ch);
    printf("The value of ASCII code is %d.\n", ch);
    printf("The character of ASCII code is %c.\n", ch);

    return 0;
}

 

ex3.3

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

int main(void)
{
    printf("\a");
    printf("Startled by the sudden sound, sally shouted,\n\"By the Great Pumpkin, what was that!\"");

    return 0;
}

 

ex3.4

// ex_3.4
#include <stdio.h>

int main(void)
{
    float num = 0;
    printf("Enter a floating-point value: ");
    scanf("%f", &num);
    printf("fixed-point notation: %10f\n", num);
    printf("exponential notation: %e\n", num);
    printf("p notation: %a\n", num);
    
    return 0;
}

本人编译环境是winXP上的CodeBlocks 17.12,编译器添加C11标准。运行结果如下:

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

ex3.6

// ex_3.6
#include <stdio.h>
#define MOLECULE_OF_WATER (3.0e-23)
#define QUART_OF_WATER 950

int main(void)
{
    double num_of_quart = 0;
    printf("How many quarts of water you have: ");
    scanf("%lf", &num_of_quart);
    double num_of_molecule = num_of_quart * QUART_OF_WATER / MOLECULE_OF_WATER;
    printf("%e", num_of_molecule);

    return 0;
}

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

ex3.7

// ex_3.7
#include <stdio.h>
#define INCH_TO_CM 2.54

int main(void)
{
    float inch = 0;
    float cm = 0;
    printf("What's your height in inch:");
    scanf("%f", &inch);
    cm = INCH_TO_CM * inch;
    printf("Your height is %.1f cm.", cm);

    return 0;
}

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

ex3.8

// ex_3.8
#include <stdio.h>
#define PINT_TO_CUP 2
#define CUP_TO_OUNCE 8
#define OUNCE_TO_TABLESPOON 2
#define TABLESPOON_TO_TEASPOON 3

int main(void)
{
    float pints = 0;
    float cups = 0;
    float ounces = 0;
    float tablespoons = 0;
    float teaspoons = 0;

    printf("Enter a volume in cups: ");
    scanf("%f", &cups);
    pints = cups / PINT_TO_CUP;
    ounces = cups * CUP_TO_OUNCE;
    tablespoons = ounces * OUNCE_TO_TABLESPOON;
    teaspoons = tablespoons * TABLESPOON_TO_TEASPOON;
    printf("PINT: %.2f\n", pints);
    printf("CUP: %.2f\n", cups);
    printf("OUNCE: %.2f\n", ounces);
    printf("TABLE SPOON: %.2f\n", tablespoons);
    printf("TEA SPOON: %.2f\n", teaspoons);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值