c primer plus 第六版 第三单元

前提:在ubuntu(17.0.0)中使用gcc(11.4.0)编译,以伪代码形式展示。

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

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

3.11 编程练习
1.通过试验(即编写带有此类问题的程序)观察系统如何处理整数上
溢、浮点数上溢和浮点数下溢的情况。

上溢:超出数据范围的最大值;下溢:小于数据范围的最小值,导致数据精度丢失。
头文件 定义宏 INT8_MAX FLT_MAX

include <stdint.h>

include <float.h>

void overflow(void)
{
char ch;
float fl,fl1;

    ch = INT8_MAX;/*127*/
    printf("Max number of char %d, overflow number of char %d.\n",ch,ch+1);/*char数据范围为 -127 ~ 128*/

    fl = FLT_MAX;/*float 数据范围float -3.4e+38 ~ 3.4e+38*/
    printf("Max number of float %e, up overflow number of float %e.\n",fl,fl*100.0);

    fl1 = 1.2345e-45;  /*数据的小数精度大于float 的数据精度*/
    printf("Max number of float %e, down overflow number of float %e.\n",fl1,fl1/2);

}
结果:
Max number of char 127, overflow number of char 128.
Max number of float 3.402823e+38, up overflow number of float 3.402823e+40.
Max number of float 1.401298e-45, down overflow number of float 0.000000e+00.

2.编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印
输入的字符。
void get_put(void)
{
int ch;

    printf("Please enter an ASCII value:\n");
    scanf("%d",&ch);
    printf("The ASCII value %d is %c.\n ",ch,ch);

}
结果:
Please enter an ASCII value:
66
The ASCII value 66 is B.

3.编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that!"

void alert(void)
{
printf("\a");
printf("Startled by the sudden sound, sally shouted,\n"By the great pumpkin, what was that!"\n");
}
结果:
Startled by the sudden sound, sally shouted,
"By the great pumpkin, what was that!"

4.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指
数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。
按以下格式输出(实际显示的指数位数因系统而异):
Enter a floating-point value: 64.25
fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.01p+6

void float1(void)
{
float fl;

    printf("Enter a floating-point vlaue: ");
    scanf("%f",&fl);
    printf("fixed-point notation: %f\n",fl);
    printf("exponential notation: %e\n",fl);
    printf("p notation: %a\n",fl);

}
结果:
Enter a floating-point vlaue: 66
fixed-point notation: 66.000000
exponential notation: 6.600000e+01
p notation: 0x1.08p+6

5.一年大约有3.156×107秒。编写一个程序,提示用户输入年龄,然后显
示该年龄对应的秒数。

void second(void)
{
float sec;
int age;

    printf("Please enter your age: ");
    scanf("%d",&age);
    sec = age * 3.156e7;
    printf("Your age %d is %e sec.\n",age,sec);

}
结果:
Please enter your age: 24
Your age 24 is 7.574400e+08 sec.

6.1个水分子的质量约为3.0×10−23克。1夸脱水大约是950克。编写一个
程序,提示用户输入水的夸脱数,并显示水分子的数量。
void water_value(void)
{
int num;
double value;

    printf("Please enter water number: ");
    scanf("%d",&num);
    value = num * 950.0 /3.0e-23;
    printf("Water value is %e \n",value);

}
结果:
Please enter water number: 1
Water value is 3.166667e+25

7.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英
寸),然后以厘米为单位显示身高。
void hight(void)
{
int in;
float hight;

    printf("Please enter your hight(in): ");
    scanf("%d",&in);
    hight = in * 2.54;
    printf("Your hight is %f(cm), %d(in)\n",hight,in);

}
结果:
Please enter your hight(in): 123
Your hight is 312.420013(cm), 123(in)

8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等
于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以
品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用
浮点类型比整数类型更合适?
void cup(void)
{
double cup;

    printf("Please enter cup num: ");
    scanf("%lf",&cup);
    printf(" %.3f cup = %.3f pint \n",cup,cup/2);
    printf(" %.3f cup = %.3f ounce \n",cup,cup*8);
    printf(" %.3f cup = %.3f bigspoon \n",cup,cup*8*2);
    printf(" %.3f cup = %.3f teaspoon \n",cup,cup*8*2*3);

}
结果:
Please enter cup num: 1
1.000 cup = 0.500 pint
1.000 cup = 8.000 ounce
1.000 cup = 16.000 bigspoon
1.000 cup = 48.000 teaspoon

思考:当cup为整数时,除品脱外定义成整数可以的;当cup出现小数时,只能使用浮点类型;综上,使用浮点类型可以包含更多的情况。

posted @ 2023-11-02 15:42  派大宝宝和海绵星  阅读(11)  评论(0)    收藏  举报