C Primer Plus 第六版编程练习第三章答案和记录一些学习历程

编程题部分:

1,通过试验观察系统如何处理整数上溢,浮点数上溢和浮点数下溢的情况。

整数上溢:编译器会不加提示的依次使用 int ,long ,unsigned long ,long long ,unsigned long long 当达到它能表示的最大值时,他会重新从起始点开始。
#include <stdio.h>
int main(void){
	int i = 2147483647;
	unsigned int j = 4294967295;
	float t = 3.2E38*100;
	float p = 0.1234E-2 / 10; 
	
	printf("%d %d \n",i,i+1);  //int的取值范围在[-32767,32767]  
							//而long类型的取值范围在[-2147483647,2147483647],int类型的i转换为long类型的i
							//所以此时的i为long的最大值,因此i+1从起始点{0}开始。 
	printf("%d %d \n",j,j+1);  //同理,unsigned long 取值范围为[0,4294967295]
	printf("%f %f \n",t,p);     //浮点型上溢,会给该值赋一个表示无穷大的值
								//浮点型下溢,则会损失精度
	
} 

运行结果:
在这里插入图片描述

2,编写一个程序,要求提示输入一个ASCII码值,然后打印输入的字符。

#include <stdio.h>
int main(void) 
{	
	int ascii;
	printf("please enter an ascii code:\n");
	scanf("%d",&ascii);
	printf("%d is ascii code of %c",ascii,ascii);
	
	return 0; 
 } 

运行结果:
在这里插入图片描述

3,编写一个程序,发出一声警报,然后打印下面的文本:

Startled by the sudden sound, Sally shouted,

"By the Great Pumpkin, what was that "

#include <stdio.h>
int main(void) 
{	
	printf("\aStartled by the sudden sound, Sally shouted,\n\"By the Great Pumpkin, what was that\"");
	
	return 0; 
 } 

运行结果:
在这里插入图片描述

4,编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p计数法(即十六进制计数法)。按以下格式输出:

Enter a floating-point value: 64.25

fixed-point notation : 64.250000

exponential notation :6.425000e+01

p notation : 0x1.01p+6

#include <stdio.h>
int main(void) 
{	
	float num;
	printf("Enter a floating-point value: ");
	scanf("%f",&num);
	printf("fixed-point notation: %f\nexponential notation: %e\np notation: %.2a",num,num,num);
	return 0; 
 } 

运行结果:
在这里插入图片描述

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

#include <stdio.h>
int main(void) 
{	
	int age;
	printf("please enter your age: ");
	scanf("%d",&age);
	printf("your age is %d and the seconds of your age is : %.1e",age,3.0e-23*age);
	return 0; 
 } 

运行结果:
在这里插入图片描述

6,1个水分子的质量约为3.0x10^-23克,一夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。

#include <stdio.h>
int main(void) 
{	
	int quart;
	printf("please enter the number of quart: ");
	scanf("%d",&quart);
	printf("%d quarts of water is %.1e water molecules",quart,950*quart/3.0e-23);	
	return 0; 
 } 

运行结果:
在这里插入图片描述

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

这道题第一章已经写过就不写了

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

#include <stdio.h>
int main(void) 
{	
	float num;
	int num1;
	printf("请输入杯数: ");
	scanf("%f",&num);
	num1 = num;
	printf("浮点型:\n对应的品脱数:%.1f 盎司数:%.f 汤勺数:%.f 茶勺数:%.f\n",num/2,num*8,num*16,num*48);
	printf("整型:\n对应的品脱数:%d 盎司数:%d 汤勺数:%d 茶勺数:%d",num1/2,num1*8,num1*16,num1*48);
	return 0; 
 } 

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值