資源簡(jiǎn)介
C Primer Plus 課后習(xí)題所有答案 完整版,比官方給出的答案更全。
printf(" Which nobody can dcny! \n") 5.編寫一個(gè)程序,創(chuàng)建一個(gè)名為toes的整數(shù)變量。讓程序把toes設(shè)置為10。再讓程序計(jì) 算兩個(gè)toes的和以及toes的平方。程序應(yīng)該輸出所有的3個(gè)值,并分別標(biāo)識(shí)它們 #include<stdio.h> int main( int toes=l0 L loes add int toes square toes add=toes +toes toes square=toes * toes printf("tocs=%dntocs add=%d\ntocs squarc=%d\n", tocs, tocs add, tocs squarc) return(0); 6.編寫一個(gè)能夠產(chǎn)生下列輸出的程序 Smile Smile i smile Smile I Smile Smile I 在程序中定義一個(gè)能顯示字符串 smile卜—次的函數(shù),并在需要時(shí)使用該函數(shù)。 #include<stdio. h void display(void) int main(void) SD display(; display( prinTf\n") display(; printf("in") display printf("\n") return(O) void displaylvoid printf("Smile 7.編寫一個(gè)程序,程序中要調(diào)用名為 one three()的函數(shù)。該函數(shù)要在一行中顯示單詞 nc",再調(diào)用two函數(shù),然后再在另一行中顯示單詞"hrec"。函數(shù)twoO應(yīng)該能在一行中顯 示單詞"two"。 main(函數(shù)應(yīng)該在調(diào)用 one three函數(shù)之前顯示短語(yǔ)" starting now:",函數(shù)調(diào) 用之后要顯示"done!"o這樣,最后的輸出結(jié)果應(yīng)如下所示 starting now one two #include<stdio. h> void one three( void) void two(void int main( void) printf("starting now: in) one three(; printr("done! \n: return(0); void onc thrcc( void) printf("Onen); two(; prinTf"Threen") void two(void) printf("two\n") 第三章數(shù)據(jù)和C 編程練習(xí) 1.通過(guò)試驗(yàn)的方法〔即編寫帶有此類問題的程序)觀察系統(tǒng)如何處理整數(shù)上溢、浮點(diǎn) 數(shù)上溢和浮點(diǎn)數(shù)下滋的情況。 #include<stdio. h> int main(void) unsigned int a=4294967295; float b=3.4E38 loat c=b *10 float d=0.1234E-2 printf("%u+1=%oun",a, a+1) printf( printf("%of/10=%fin",d, d/10) return(0); 2.編寫一個(gè)程序,要求輸入一個(gè)ASCI碼值如66),然后輸出相應(yīng)的字符。 #includes stdio.h> int main( void) char a scanr( %d", &a); printf("%ocn", a return(0); 3.編寫一個(gè)程序,發(fā)出警報(bào)聲,并打印下列文字 Startled by the sudden sound, Sally shouted By the great Pumpkin, what was that #include<stdio h> It main ( void) printf("\aStartled by the sudden sound, Sally shouted, By the great pumpkin, what was that!\n") return(0); 4.編寫—個(gè)程序,讀入一個(gè)浮點(diǎn)數(shù),并分別以小數(shù)形式和指數(shù)形式打印。輸出應(yīng)如同 下面格式(實(shí)際顯示的指數(shù)位數(shù)七許因系統(tǒng)而不同): The input is21.290000or2.129000e+001 tincludesstdio.h> int main(void) oat scanf(%of", &a) printf("The input is %f or %ocin", a, a) return(0 5.一年約有3.156×107S。編寫一個(gè)程序,要求輸入您的年齡,然后顯示該年齡合多少 秒 tincludesstdio. h> int main(void) float printf("Please input your age: " scanf(%of", &a); printf( Your age is %e seconds n, a*3. 156E7) return(O) 6.1個(gè)水分子的質(zhì)量約為30×10~-23g,1夸脫水大約有950g。編寫一個(gè)程序,要求輸 入水的夸脫數(shù), 然后顯示這么多水中包含多少個(gè)水分子 #include<stdio. h> int main(void) float a. printf(" Please input how much quarts the water is: scanf(%of", &a); printf("%of quarts watcr has %c molecules. n, a, a*950/3E-23) return(O) 7.1英寸等于254cm。編寫一個(gè)程序,要求輸入您的身高(以英寸為單位),然后顯示該 身高值等于 多少厘米。如果您愿意,也可以要求以厘米為單位輸入身高,然后以英寸為單位進(jìn)行顯示 #include<stdio. h> Int main(vol float a, printf("Plcasc input your height by inches scanf(%of, &a) printf("Your height is %fcm. n", a 2.54) return(0) 第4章字符串和格式化輸入輸出 編程練習(xí) 1.編寫一個(gè)程序,要求輸入名字和姓氏,然后以“名字,姓氏”的格式打印。 #includes stdio.h2> int main(void) char surname[20], firstname[20] printf("Plcasc input your firstname and surname scanf(%os", firstname) scanf(%os", surname) printf("You are os, %os. n", firstname, surname) return(O 2.編寫一個(gè)程序,要求輸入名字,并執(zhí)行以下操作 a.把名字引在雙引號(hào)中打印出來(lái)。 b.在寬度為20個(gè)字符的字段內(nèi)打印名字,并且整個(gè)字段引在引號(hào)內(nèi)。 c.在寬度為20個(gè)字符的字段的左端打印名字,并且整個(gè)字段引在引號(hào)內(nèi)。 d.在比名字寬3個(gè)字符的字段內(nèi)打印它 #include<stdio h> # nclude≤ string. h> int main(void) char name[20] int width printf("Please input your name: " printf("A: "%s \n", name) printf("B: %20s\n", name) printf("C: \%"in",name width=strlen(name)+3 printf"D:"%*s"n", width,name);/對(duì)應(yīng) width return(0); 3.編寫一個(gè)程序,讀取一個(gè)浮點(diǎn)數(shù),并且首先以小數(shù)點(diǎn)記數(shù)法,然后以指數(shù)記數(shù)法打印 之。翰出使用下列形式(在指數(shù)位置顯示的數(shù)字的位數(shù)可能會(huì)隨系統(tǒng)而不同): The input is 21. 3 or 2.1e+001 b. The input is +21.290 or 2. 129E+001 clude<stdio. h> int main(void) float a: printf("Please input a float: ) scanf("of, &a) printr"a. The input is %.lfor %.le\n, a, a) printf("b The input is %+. 3f or%.3En, a, a); return 4.編寫一個(gè)程序,要求輸入身高(以英寸為單位)和名字,然后以如下形式顯示 Dabney, you are 6. 208 feet tall 使用 float類型,使用作為除號(hào)。如果您愿意,可以要求以厘米為單位輸入身高,并以米 為單位進(jìn)行顯示。 #include<stdio.h> int main ( void) float height char nam e20; printf("Please input your height by inches scant("%", &height) rinf"Please input your name scant("%s", name); printf( %os, you are %f feet tall\n", name, height/12) returne 5編寫—個(gè)程序,首先要求用戶輸入名字,然后要求用戶輸入姓氏。在一行打印輸入的姓 名,在下行打印每個(gè)名字中字母的個(gè)數(shù)。把字母?jìng)€(gè)數(shù)與相應(yīng)名字的結(jié)尾對(duì)齊,如下所示 Melissa Honeybee 然后打印相同的信息,但是字母?jìng)€(gè)數(shù)與相應(yīng)單詞的開始對(duì)齊。 #include<stdio. h> #include<string. h> int main( void) char surname 20, firstname 201 printf("Please input your firstname and surname: " scanf("%s", firstname); scanf(%os" surname) printf("%os %os. n, firstname, surname) printf("%*d %*dn" strlen(firstname), strlen( firstname), strlen(surname), strlen(surname ) //*XyA 寬度變量 tf(%os%os. n", firstname, surname) printf("%-*d %-*dn", strlen(firstname), strlen( firstname), strlen(surname), strlen(surname); // Xy 應(yīng)寬度變量 return(0 6.編寫一個(gè)程序,設(shè)置一個(gè)值為1030的 double類型變量和一個(gè)值為1030的foat 類型變量。每個(gè)變量的值顯示三次:一次在小數(shù)點(diǎn)右側(cè)顯示4個(gè)數(shù)字,一次在小數(shù)點(diǎn)右側(cè)顯 示12個(gè)數(shù)字,另一次在小數(shù)點(diǎn)右側(cè)顯示16個(gè)數(shù)字。同時(shí)要讓程序包括fath文件,并顯 示 FLT-DIG和 DBL DIG的值。1.0/3.0的顯示值與這些值一致嗎? #include<stdio. h> #include<float. h> int main(void) double a=1. 0/3.0 float b=1.0/3.0: printr"double a=1.0/3.0: n") tf("°%.4 f("%.12en",a); intf( % 16en\n",a) printf("float b=1.0/3.0: \n") printf( %o4fn", b) printf("%. 12fn",b)
代碼片段和文件信息
評(píng)論
共有 條評(píng)論