c_primer_plus_CH3

0. 自言自语

忏愧,忏愧。👨
这几个简单的小练习都断断续续搞了一下午,可见水平之低😰。
不过还是有所收获。
这8个小练习主要是锻炼对数据类型的选择, 对code的可读性进行练习,从简单程序开始,养成编程7步。

  1. 定义程序的目标
  2. 设计程序
  3. 编写代码
  4. 编译
  5. 运行
  6. 测试和调试程序
  7. 维护和修改

我这里就是些很简单的练习,维护就没什么可维护的了😄

1. 练习一

/* 
 * 1. 通过实验观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。
 *   OS: linux
 * Time: 2020/04/22
 */
 
// 下面的两个宏定义在float.h头文件里
// #define FLT_MAX         3.402823466e+38F        /* max value */
// #define FLT_MIN         1.175494351e-38F        /* min positive value */

#include <stdio.h>
#include <limits.h>
#include <float.h>

int main()
{
    int int_overflow; // 整型上溢
    float flt_overflow, flt_underflow; // 浮点上溢,浮点下溢
    
    int_overflow  = INT_MAX + 1;
    flt_overflow  = FLT_MAX + 1;
    flt_underflow = FLT_MIN - 1;

    printf("》Hello Okfine\n");
    printf("	int_max              = %d\n", INT_MAX);
    printf("	int_max + 1          = %d\n", int_overflow);
    printf("	flt_min              = %f\n", FLT_MIN);
    printf("	flt_max              = %f\n", FLT_MAX);
    printf("	flt_overflow  * 2    = %f\n", FLT_MAX * 2);
    printf("	flt_overflow  + 1    = %f\n", flt_overflow);
    printf("	flt_underflow - 1    = %f\n", flt_underflow);
    return 0;
}

/* 执行结果 */
》 Hello Okfine
	int_max             = 2147483647
	int_max + 1         = -2147483648
	flt_min             = 0.000000
	flt_max             = 340282346638528859811704183484516925440.000000
	flt_overflow * 2    = inf // infinity 
	flt_overflow + 1    = 340282346638528859811704183484516925440.000000
	flt_underflow - 1   = -1.000000


2. 练习二

/*
 * 2. 编写一个程序,要求提示输入一个ASCII码(如,66),然后打印对应的字符。
 *   - 目标:打印对应的字符
 *   - 设计:定义一个int型变量ascii_code,来存储用户输入的ASCII码,然后将ascii_code里的值用 ‘%c’格式显示。
 * 
 *   OS:linux
 * Time: 2020/04/21
 */

#include <stdio.h>

int main()
{
    int ascii_code;
    printf("》Hello Okfine\n");
    printf("	Please enter an ASCII: ");
    scanf("%d", &ascii_code); // 注意这里的 取地址符'&',要通过变量的地址来修改变量保存的值
    printf("	The ASCII %d is %c\n", ascii_code, ascii_code);
    return 0;
}

/* 执行结果 */
》 Hello Okfine
	 Please enter an ASCII: 66
	 The ASCII 66 is B

3. 练习三

/*
 * 3. 编写一个程序,发出一声警报,然后打印下面的文本:
 * 	  Startled by the sudden sound , Sally shouted,
 *    "By the Great Pumpkin, what was that!"
 *   - 设计:发出警报使用转义序列 “\a”,直接printf打印就行。
 *   OS: linux
 * Time: 2020/04/22
 */

#include <stdio.h>

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

/* 执行结果 */
》 Hello Okfine
	Startled by the sudden sound , Sally shouted,“By the Great Pumpkin, what was that!

4. 练习四

/*
 * 4. 编写一个程序,读取一个浮点数,先打印成小数点形式,再打印出指数形式。
 *    如果系统支持,再打印成p记数法(notation)(十六进制)。
 *    按以下格式输出:
 *        Enter a floating-point value: 64.25
 * 		  fixed-point notation: 64.250000
 *        exponential notation: 6.425000e+01
 * 		  p notation: 0x1.01p+6
 * 
 *   OS: linux
 * Time: 2020/04/22
 */

#include <stdio.h>

int main()
{
    float f_value;
    
    printf("》Hello Okfine\n");
    printf("	Please enter a floating-point value: ");
    scanf("%f", &f_value);
    
    printf("	fixed-point notation: %f\n", f_value);
    printf("	exponential notation: %e\n", f_value);
    printf("	p notation: %a\n", f_value);
    return 0;
}
/* 执行结果 */
》 Hello Okfine
	Please enter a floating-point value: 64.25
	fixed-point notation: 64.250000
	exponential notation: 6.425000e+01
	p notation: 0x1.01p+6

5. 练习五

/*
 * 5. 一年大约有3.156 * 10^7秒。编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。
 *    - 设计:规定一年为365天,0 < 年龄 <= 99岁,如果输入的年龄不在此范围内,则打印输入不合法的提示信息
 * 			 如果输入正确,则计算出该年龄的秒数。
 * 		   - 我们来看看99岁总共有多少秒:99*365*24*60*60 = 3,122,064,000 < 4,294,967,296 = 2^32(unsigned int的最大值)
 * 		   - 所以只需要定义一个unsigned int ui_age;来保存年龄。
 * 				      定义一个unsigned int ui_age_seconds;来存储对应年龄的秒数。
 * 		   - 然后将计算结果打印出来即可。
 * 		   - unsigned int 的范围是:[0, 4,294,967,296]
 * 		     int 的范围是:[-2^31, 2^31 - 1]
 * 
 *   OS: linux
 * Time: 2020/04/22
 */

#include <stdio.h>

#define YEAR_SECONDS(n) (n * 365 * 24 * 60 * 60) /* n年的总秒数 */

int main()
{
    unsigned int ui_age;
    unsigned int ui_age_seconds;

    printf("》Hello Okfine\n");
    printf("	Please enter your age(in years): ");
    scanf("%d", &ui_age);
	
	/* 判断输入的年龄是否在0~99岁之内,并打印出多少秒 */
    if(0 < ui_age && ui_age <= 99)
    {
        ui_age_seconds = YEAR_SECONDS(ui_age);
        printf("	Your age is %u that have %u seconds\n", ui_age, ui_age_seconds);
    } 
    else
    {
        printf("	Your age is illegal.\n");
    }
    return 0;
}

/* 执行结果 */
》 Hello Okfine
	Please enter your age(in years): -1
	Your age is illegal.
	 
》 Hello Okfine
	Please enter your age(in years): 99
	Your age is 99 that have 3122064000 seconds

》 Hello Okfine
	Please enter your age(in years): 100
	Your age is illegal.

》 Hello Okfine
	Please enter your age(in years): 12
	Your age is 12 that have 378432000 seconds

6. 练习六

/*
 * 6. 一个水分子(molecule)的质量约为3.0 * 10^-23克。1夸脱(quart)水大约是950克。
 * 	  编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。
 *  - 设计:
 * 		  先计算出1夸脱水950克有多少水分子:950 / (3.0 * 10^(-23))= 3.17 * 10^25 个。
 * 		  指数形式,可以使用float来装下它。
 * 		  我们使用float quart;来存储用户输入的夸脱数,并显示有多少千克(kilogram),
 * 		  然后打印输出对应的分子数即可。
 *   OS: linux
 * Time: 2020/04/22
 */

#include <stdio.h>

#define MOLECULE_NUMBER_PER_QUART       (950 / (3.0 * 1.0E-23))
#define MASS_PER_QUART_OF_KILOGRAM      (950 * 1.0e-3)

int main()
{
    float 		 ui_quart_value;
    float        f_mass;
    long double  ld_molecule_value;

    printf("》Hello Okfine\n");
    printf("    Please enter the quart value: ");
    scanf("%f", &ui_quart_value);
    
    f_mass = ui_quart_value * MASS_PER_QUART_OF_KILOGRAM;
    ld_molecule_value = ui_quart_value * MOLECULE_NUMBER_PER_QUART;

    printf("	The %.2f quart water have mass %f kg.\n", 		ui_quart_value, f_mass);
    printf("	The %.2f quart water have %Le molecules.\n", 	ui_quart_value, ld_molecule_value);
    
    return 0;
}

/* 执行结果 */
》 Hello Okfine
	Please enter the quart value: 2.11
	The 2.11 quart water have mass 2.004500 kg.
	The 2.11 quart water have 6.681666e+25 molecules.

7. 练习七

/* 
 * 7. 1英寸相当于2.54厘米。
 *    编写一个程序,提示用户输入身高(英寸),然后以厘米为单位显示身高。
 *   OS: linux
 * Time: 2020/04/22
 */

#include <stdio.h>

#define INCH_TO_CM (2.54)
int main()
{
    float i_height;
    printf("Hello Okfine\n");
    printf("	Enter your height(in inch): ");
    scanf("%f", &i_height);

    printf("	You are %.2f centimeters tall.\n", i_height * INCH_TO_CM);
    return 0;
}

/* 执行结果 */
Hello Okfine
	Enter your height(in inch): 70.9
	You are 180.09 centimeters tall.

8. 练习八

/*
 * 8. 在美国的体积测量系统中,1品脱(pint)等于2杯(glass),1杯等于8盎司(ounce),
 * 						   1盎司等于2大汤勺(soup),1大汤勺等于3茶勺(teaspoon)。
 *    编写一个程序,提示用户输入杯数,并以品脱数、盎司、汤勺、茶勺为单位显示等价容量。
 * 	- 设计:
 * 		  我们先来进行单位转换:1 杯 = 1/2 pint = 8 ounces = 16 soup = 48 teaspoon
 * 		  所以只需要进行简单的转换即可。
 * 		  
 * 	- 对于该程序,为何使用浮点数类型比整数类型更适合?
 * 
 *   OS: linux
 * Time: 2020/04/22
 */
 
#include <stdio.h>

int main()
{
    float f_cup, f_pint, f_ounce;
    float f_soup, f_teaspoon;

    printf("Hello Okfine\n");
    printf("Enter the mass of cup: ");
    scanf("%f", &f_cup);

    f_pint     = .5 * f_cup;
    f_ounce    = 8  * f_cup;
    f_soup     = 16 * f_cup;
    f_teaspoon = 48 * f_cup;

    printf("You input the mass of cup is %.4f\n",   f_cup);
    printf("The %.4f cup equal to %.4f pint\n",     f_cup, f_pint);
    printf("The %.4f cup equal to %.4f ounce\n",    f_cup, f_ounce);
    printf("The %.4f cup equal to %.4f soup\n",     f_cup, f_soup);
    printf("The %.4f cup equal to %.4f teaspoon\n", f_cup, f_teaspoon);
    
    return 0;
}

/* 执行结果 */
Hello Okfine
	Enter the mass of cup: 4
	You input the mass of cup is 4.0000
	The 4.0000 cup equal to 2.0000 pint
	The 4.0000 cup equal to 32.0000 ounce
	The 4.0000 cup equal to 64.0000 soup
	The 4.0000 cup equal to 192.0000 teaspoon

9. Makefile

使用Makefile可以很方便的在linux编译c文件,只需要输入make就可以完成编译链接生成可执行文件,清除文件输入make clean即可

source = $(wildcard *.c) 						// 这个函数表示找出当前工作目录下的所有 ".C" 文件,得到的结果保存在source变量里
obj    = $(patsubst %.o, %.c, $(source)) 		// 这个函数是将source里面的所有 ".C“文件名修改为 ".o"

test: $(obj)									// 生成的可执行文件名为test,test依赖 obj里面的 ".o"文件
	gcc -o test $^  							// gcc -o test xxx.c      $^表示所有的依赖文件

obj: $(source)									// .o 文件的生成依赖.C 文件
	gcc -c -o $@ $<          
												//  $@ 表示当规则中有多个目标时,$@ 所指的是其中任何造成规则的命令运行的目标;
												//  $<:表示第一个依赖文件

clean:                                          // 清除所有的 .o 文件和可执行文件test。
	rm -rf *.o test

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值