第三章编程练习 C_primer_plus

第三章编程练习

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

#include<stdio.h>
#include<limits.h>
int main()
{
	unsigned a1=UINT_MAX+1;
	unsigned a2 = UINT_MAX + UINT_MAX;
	int b1 = INT_MAX + 1;
	int b2 = INT_MAX + INT_MAX;
	int b3 = INT_MIN - 1;
	int b4 = INT_MIN + INT_MIN;
	float c=2.0000e50;
	float d = 1.2345e-44;
	printf("%u,%u,%u\n", 
	UINT_MAX, a1, a2);//溢出时相当于一个循环,最大数+1变成最小数
	printf("%d,%d.%d,%d,%d,%d\n", 
	INT_MAX, b1, b2, INT_MIN, b3, b4);
	printf("%f\n", c);//浮点型上溢输出inf
	printf("%e", d);
	system("pause");
	return 0;
}

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

#include<stdio.h>
int main()
{
	char x;
 	printf("please input a ASCII:x");
 	scanf("%c", &x);
 	printf("%d", x);
 	system("pause");
 	return 0;
}

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

#include<stdio.h>
int main()
{
	printf("\aStartled by the sudden sound,Sally shouted,\n");
 	printf("\"By the Great Pumpkin,what was that!\"");
 	system("pause");
 	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()
{
	float x=64.25;
 	printf("Enter a floating-point value:%.2f\n", x);
 	printf("fixed-point notation:%f\n", x);
 	printf("exponential notation:%e\n", x);
 	printf("P notation:%.2a\n", x);
 	getchar();
 	getchar();
 	return 0;
}

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

#include<stdio.h>
int main()
{
	int age;
 	double seconds;
 	printf("please input your age:\n");
 	scanf("%d", &age);
 	seconds = age*3.156e7;
 	//先用scanf读取了age才能进行计算,否则进行报错
 	printf("%f", seconds);
 	system("pause");
 	return 0;
}

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

#include<stdio.h>
int main()
{
	int kuatuo = 950;
 	int kuatuonum;
 	double weight = 3.0e-23;
 	double sumnumber;
 	printf("please int the number of kuatuo:\n");
 	scanf("%d", &kuatuonum);
 	sumnumber = kuatuonum*(kuatuo / weight);
 	printf("%e", sumnumber);
 	system("pause");
	 return 0;
}

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

#include<stdio.h>
int main()
{
	float heighti=0.0;
 	float heightc=0.0;
 	printf("please input your height(inch):\n");
 	scanf("%f",&heighti);
 	heightc = heighti*2.54;
 	printf("Yor are %.2f cm", heightc);
 	system("pause");
 	return 0;
}

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

#include<stdio.h>
int main()
{
	float pint;
 	float bottle;
 	int ounce;
 	int soup_ladle;
 	int tea_ladle;
 	printf("please input the number of bottle:\n");
 	scanf("%f", &bottle);
 	pint = bottle/2;//若两者为int则只截断到整数部分,若int型/float型
 	ounce = bottle * 8;
 	soup_ladle = bottle * 16;
 	tea_ladle = bottle * 48;
 	printf("%.0f bottle equal to %.1f pint,%d ounce,%d soup_ladle,%d tea_ladle\n",bottle, pint,ounce,soup_ladle,tea_ladle);
 	system("pause");
 	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值