第4章 字符串和格式化输入/输出

  我的疑惑在于“质量和重量”。以前学的物理数学都忘的差不多咯。

  看到网友是这么说的,所以我就来搬运了:

    平时说的重量,在质量中叫质量,用m 表示,体积用V表示,密度用ρ表示
    当知体积和密度时,可用下面的公式计算
      m=ρV
    即:质量=密度 X 体积

  看到这里就有点理解书中计算啦!

  又看看别的:

    重量=体积*密度*g (g为重力加速度不是克 ,一般为9.8)
    质量=体积*密度

  我只想说以前上学学的东西还是很经典的,可惜的是如果不是学c,我估计不会捡起来啦!

程序清单4.1  talkback.c(talkback--对讲)

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
#include <stdio.h>
#include <string.h>
#include <windows.h>
#define DENSITY 62.4

// density--密度
// volume--体积、容量
// letters--字母(个数)
// cubic feet--立方
int main(void)
{
    
float weight, volume;
    
int size, letters;
    
char name[40];

    printf(
"Hi! What's your first name?  ");
    scanf(
"%s", name);
    printf(
"%s, what's your weight in pounds?  ", name);
    scanf(
"%f", &weight);

    size = 
sizeof name;
    letters = strlen(name);
    volume = weight / DENSITY; 
// 核心问题是你不知到计算公式!

    printf(
"Well, %s, your volume is %2.2f cubic feet.  ", name, volume);
    printf(
"Also, your first name has %d letters, ", letters);
    printf(
"and we have %d bytes to store it in.  ", size);

    
//Sleep(1000 * 10); //还有其他的类似的函数么?
    return 0;
}

程序清单4.2  praise1.c

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include <stdio.h>
#define PRAISE "What a super marvelous name!"

// marvelous--非凡的
int main(void)
{
    
char name[40];

    printf(
"What's your name?  ");
    scanf(
"%s", name);
    printf(
"Hello, %s. %s  ", name, PRAISE);

    
return 0;
}

程序清单4.3  praise2.c

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
#include <stdio.h>
#include <string.h>
#define PRAISE "What a super marvelous name!"

// occupy--占领、占用
// cell--单元格
// phrase--短语
// praise--赞美
int main(void)
{
    
char name[40];

    printf(
"What's your name?  ");
    scanf(
"%s", name);
    printf(
"Hello, %s. %s  ", name, PRAISE);

    printf(
"Your name of %d letters occupies %d memory cells.  ", strlen(name), sizeof name);
    printf(
"The phrase of praise has %d letters", strlen(PRAISE));
    printf(
" and occupies %d memory cells.  "sizeof PRAISE);

    
return 0;
}

程序清单 4.5

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
// defines.c -- 使用 limits.h 和 float.h 中的常量
#include <stdio.h>
#include <limits.h>
#include <float.h>

int main(void)
{
    printf(
"Some number limits for this system:  ");
    printf(
"Biggest int: %d  ", INT_MAX);
    printf(
"Smallest unsigned long: %lld  ", LLONG_MIN);
    printf(
"One byte = %d bits on this system.  ", CHAR_BIT);
    printf(
"Largest double: %e  ", DBL_MAX);
    printf(
"Smallest normal float: %e  ", FLT_MIN);
    printf(
"float precision = %d digits  ", FLT_DIG);
    printf(
"float epsilon = %e  ", FLT_EPSILON);

    
return 0;
}

/*
在 codeblocks 10.5 中编译结果为

Some number limits for this system:
Biggest int: 2147483647
Smallest unsigned long: 0
One byte = 8 bits on this system.
Largest double: 1.797693e+308
Smallest normal float: 1.175494e-038
float precision = 6 digits
float epsilon = 1.192093e-007

*/

原文地址:https://www.cnblogs.com/leefun/p/4053893.html