C Primer Plus 第3章 数据和C-编程练习
1.通過試驗(即編寫帶有此類問題的程序)觀察系統如何處理整數上溢、浮點數上溢和浮點數下溢的情況。
#include<stdio.h> #include<float.h> #include<limits.h>int main(void) {int big_int=2147483647;/*有符號整型數據最大值2的31次方減1*/float big_float=3.4e38;/*浮點型數據的最大值一般為3.4E38*/float small_float=10.0/3;/*浮點數據的有效位數,為6位*/printf("The big int data is $d\n",big_int+1);/*整型數據最大值加1,會產生越界,結果為-2147483648*/printf("The big float data is %f\n",big_float*10);/*浮點型最大數據乘10產生越界,輸出inf。如果浮點數據只是加一個*小數據,由于其精確度限制,不會產生越界效果。*/printf("The big float data is %f\n",small_float);/*打印結果3.333333,精度損失。*/printf("The MAX int data is %d\n",INT_MAX);/*打印結果:2147483647*/return 0; }2.編寫一個程序,要求提示輸入一個ASCII碼值(如,66),然后打印輸入的字符。
#include<stdio.h> int main(void) {int input;printf("Enter a value of char int ASCII:");scanf_s("%d",&input);/*通過scanf()函數讀取用戶輸入,并存儲在input變量中。*/printf("You input value is %d,and char is %c\n",input,input);/*通過轉換說明%d,打印整型數值和字符.*/return 0; }3.編寫一個程序,發出一聲警報,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
“By the Greet Pumpkin, what was that!”
4.編寫一個程序,讀取一個浮點數,先打印成小數點形式,再打印成指數形式。然后,如果系統支持,再打印成p計數法(即十六進制計數法)。按以下格式輸出(實際顯示的指數位數因系統而異):
Enter a floating-point value:64.25
fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.01p+6
5.一年大約有3.156*10^7秒。編寫一個程序,提示用戶輸入年齡,然后顯示該年齡對應的秒數。
#include <stdio.h> #define SEC_PER_YEAR 3.156e7 /* 通過預編譯指令定義每年的秒數。*/ int main(void) {float second,year;/* 由于數值需要,年齡也使用浮點型數據。*/printf("Enter how many years old you are:");scanf("%f",&year);/* 讀取用戶輸入的年齡數。 */second = year*SEC_PER_YEAR;/* 計算用戶年齡對應的秒數*/printf("You are: %.1f years old.\n",year);printf("And you are %e seconds old, too.\n",second);return 0; }6.1個水分子的質量約為3.0*10^-23克。1夸脫水大約是950克。編寫一個程序,提示用戶輸入水的夸脫數,并顯示水分子的數量。
#include <stdio.h> #define MASS_PER_MOLE 3.0e-23 #define MASS_PER_QUART 950 /* 使用預編譯指令定義了 水分子質量,一夸脫質量 */ int main(void) {float quart,quantity;printf("Enter how many quart:");scanf("%f",&quart);/* 讀取用戶輸入的夸脫數。 */quantity = quart*MASS_PER_QUART/MASS_PER_MOLE;/* 計算水分子數量 */printf("There are %e molecule.\n",quantity);return 0; }7.1英寸相當于2.54厘米。編寫一個程序,提示用戶輸入身高(\英寸),然后以厘米為單位顯示身高。
#include <stdio.h> #define INCH_TO_CM 2.54 /* 英寸到厘米的轉換系數。 */ int main(void) {float inch,cm;printf("Enter the inch of your heigh:");scanf("%f",&inch);cm = inch*INCH_TO_CM;/* 將英寸轉換成厘米。*/printf("Hi ,your are %0.2f inch ,or %.2f cm heigh\n",inch,cm);/* 打印計算結果。*/return 0; }8.在美國的體積測量系統中,1品脫等于2杯,1杯等于8盎司,1盎司等于2大湯勺,1大湯勺等于3茶勺。編寫一個程序,提示用戶輸入杯數,并以品脫、盎司、湯勺、茶勺為單位顯示等價容量。思考對于該程序,為何使用浮點類型比整數類型更合適?
#include <stdio.h> #define PINT_CUP 2 #define CUP_OUNCE 8 #define OUNCE_SPOON 2 #define SPOON_TEA 3 /* 進制轉換的明示常量定義 */int main(void) {float pint,cup,ounce,spoon,tea_spoon;printf("Enter how many cups:");scanf("%f",&cup);pint = cup/PINT_CUP;ounce = cup*CUP_OUNCE;spoon = ounce*OUNCE_SPOON;tea_spoon = spoon*SPOON_TEA;/* 進制轉換計算 */printf("%.1f cup equals %.1f pint, %.1f ounce, %.1f spoon, %.1f tea_spoon.\n",cup,pint,ounce,spoon,tea_spoon);/* 進制轉換結果打印*/return 0; }總結
以上是生活随笔為你收集整理的C Primer Plus 第3章 数据和C-编程练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 技校计算机专业自我鉴定,关于技校计算机专
- 下一篇: Unity编辑器扩展: 程序化打图集工具