这里是第三章编程练习的答案,依旧是适合萌新正规中距的编程风格,不过对第一问稍微进行了进一步的讨论。
万丈高楼平地起,虽然题目中有很多程序(实际上绝大部分)都能够办到脑补,不过推荐新手玩家还是认认真真都写一遍,毕竟程序应用的算法可能非常简单但程序实际运行起来时有各种各样的问题,这也是程序员需要解决的。
很多时候解决一个问题不仅仅能从中得到巨大的满足感激励你继续学下去,更是从新的角度加深了你对某些知识的认识,日积月累将给予丰厚的回馈。
/*使用特殊头文件掌握整形、浮点型的容纳范围,便于挑足够的数字产生上溢、下溢*/
#include <stdio.h>
#include <float.h> //新头文件,包含一些关于int、char最大、最小值等本身信息的信息参见P79
#include <limits.h> //新头文件,包含一些关于float、double最大、最小值等本身信息的信息参见P79
int main(void)
{
int a, b;
float c, d, e1, e2;
double e, f, e3, e4;
printf("int类型的最大值:%d\n", INT_MAX); // INT_MAX和下面命名相似的常量存在于上面两个头文件中,常量含义参见P79
scanf("%d", &a);
printf("%d\n", a);
printf("int类型的最小值:%d\n", INT_MIN);
scanf("%d", &b);
printf("%d\n", b); //这里只是单纯观察输入小于int最小值的数会怎么样,不叫下溢
printf("float类型的最大值:%for%e\n", FLT_MAX, FLT_MAX);
scanf("%f,%e", &c, &e1);
printf("%f,%e\n", c, e1);
printf("float类型的最小值:%for%e\n", FLT_MIN, FLT_MIN);
scanf("%f,%e", &d, &e2);
printf("%f,%e\n", d, e2); //用于观察输入小于float最小值数的结果,非下溢
printf("%f\n", FLT_MIN / 2); //这里才是下溢
printf("double类型的最大值:%lfor%le\n", DBL_MAX, DBL_MAX);
scanf("%lf,%le", &e, &e3);
printf("%lf,%le\n", e, e3);
printf("double类型的最小值:%lfor%le\n", DBL_MIN, DBL_MIN);
scanf("%lf,%le", &f, &e4);
printf("%lf,%le\n", f, e4); //用于观察输入小于double最小值数的结果,非下溢
printf("%f", DBL_MIN / 2); //这里才是下溢
return 0;
}
#include <stdio.h>
int main(void)
{
int x;
printf("请输入一个ASCⅡ码值:");
scanf("%d", &x);
printf("%c", x);
return 0;
}
#include <stdio.h>
int main(void)
{
printf("\aStartled by the sudden sound, Sally should,\n\"By the Great Pumpkin, what was that!\"");
return 0;
}
#include <stdio.h>
int main(void)
{
float x;
scanf("%f", &x);
printf("Enter a floating-point value: %f\n", x);
printf("fixed-point notation: %f\nexponetial notation: %e\n p notation: %p", x, x, x);
return 0;
}
#include <stdio.h>
int main(void)
{
int Age;
double Second;
printf("请输入你的年龄:");
scanf("%d", &Age);
Second = Age * 3.156e7; //Second表达式若放在scanf()函数取值前,最后的printf()函数无法得到正确的值
printf("你的年龄对应: %e秒", Second);
return 0;
}
#include <stdio.h>
int main(void)
{
int Kt;
printf("请输入水的夸脱数:");
scanf("%d", &Kt);
printf("%d夸脱的水的分子数量是%e", Kt, Kt * 950 / 3.0e-23);
return 0;
}
#include <stdio.h>
int main(void)
{
float high;
printf("请以英寸为单位输入你的身高:");
scanf("%f", &high);
printf("换算成厘米你的身高是:%f", high * 2.54);
return 0;
}
#include <stdio.h>
int main(void)
{
float cap;
printf("请输入一个杯数:");
scanf("%f", &cap);
printf("%f杯等于%f品脱%f盎司%f汤勺%f茶勺", cap, cap / 2, cap * 8, cap * 8 * 2, cap * 48);
return 0;
}