C Primer Plus(第6版)第三章程式設計練習答案
阿新 • • 發佈:2018-11-06
這裡是第三章程式設計練習的答案,依舊是適合萌新正規中距的程式設計風格,不過對第一問稍微進行了進一步的討論。
萬丈高樓平地起,雖然題目中有很多程式(實際上絕大部分)都能夠辦到腦補,不過推薦新手玩家還是認認真真都寫一遍,畢竟程式應用的演算法可能非常簡單但程式實際執行起來時有各種各樣的問題,這也是程式設計師需要解決的。
很多時候解決一個問題不僅僅能從中得到巨大的滿足感激勵你繼續學下去,更是從新的角度加深了你對某些知識的認識,日積月累將給予豐厚的回饋。
/*使用特殊標頭檔案掌握整形、浮點型的容納範圍,便於挑足夠的數字產生上溢、下溢*/ #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;
}