C Primer Plus 第6版 第3章 编程练习

1.编写程序查看系统如何处理整数上溢和浮点数上溢和下溢情况。

#include<stdio.h>
int main(void)
{

	int int_a = -1;
	float float_a = -1.0;
	for (int i = 1; i <= 128; i++)
	{
		int_a *= 2;
		float_a *= 2;
		printf("2^%d = %d\t\t\t 2^ %d = %f\n", i, int_a, i, float_a);
	}


	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

2.提示输入一个ASCII码,然后打印对应的字符。

#include<stdio.h>
int main(void)
{
	int ascii_n;
	char ascii_c;
	printf("请输入一个ASCII码:");
	scanf_s("%d", &ascii_n);
	ascii_c = ascii_n;
	printf("%c", ascii_c);
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

 3.发出响铃,然后打印一串文本

#include<stdio.h>
int main(void)
{

	printf("\a");
	printf("Startled by the sudden sound, Sally shouted,\n");
	printf("\"By the Great Pumpkin, what was that! \"");
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

4.读取一个浮点数,以小数和指数形式打印 

#include<stdio.h>
int main(void)
{
	float float_n;
	printf("Enter a floating-point value: ");
	scanf_s("%f", &float_n);
	printf("fixed-point notation:%f\n",float_n);  //小数点形式
	printf("exponential notation:%e\n", float_n); // 指数形式
	printf("p notation: %a\n" ,float_n);          //十六进制
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

5.一年大概有3.156 x 10^7 秒。 输入年龄,转换成对应的秒。

#include<stdio.h>
int main(void)
{
	int age;
	const float secondsOfAYear = 3.156e7;
	printf("输入年龄: ");
	scanf_s("%d", &age);
	printf("相当于 %f 秒\n" ,secondsOfAYear*age);        
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

6.   1个水分子质量为 3 * 10 ^-23 g , 1 夸脱的水是950g.输入夸脱数,转换成水分子数,

#include<stdio.h>
int main(void)
{
	float quart;
	const float water = 3.0e23;
	const int aquart = 950;
	printf("输入水的夸脱数: ");
	scanf_s("%f", &quart);
	printf("相当于 %f 个水分子\n" ,quart*aquart*water);        
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

 7.  1英寸相当于2.54厘米,输入英寸转换厘米。

#include<stdio.h>
int main(void)
{
	const float inchToCm = 2.54;
	float inch;
	printf("请输入英寸:");
	scanf_s("%f", &inch);
	printf(" %f英寸相当于%f厘米", inch, inch*2.54);
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

8.单位转换

1品脱 = 2 杯; 1杯 = 8盎司; 1盎司 = 2大汤勺; 1大汤勺 = 3 茶勺

#include<stdio.h>
int main(void)
{
	float cup;
	printf("输入杯数:");
	scanf_s("%f", &cup);
	printf("相当于%f品脱\n%f盎司\n%f大汤勺\n%f茶勺", cup / 2,8*cup,16*cup,48*cup);

	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41068877/article/details/83271500