《C与指针》第七章练习
本章問題
1.具有空函數(shù)體的函數(shù)可以作為存根使用,你如何對這類函數(shù)進(jìn)行修改,使其更有用?
answer:Have the stub(存根) print out a message when it is called,perhaps printing the values it was given as arguments.
(存根可以在它被調(diào)用的時候打印出相應(yīng)的信息,可能打印出參數(shù)的值)
?
2.在ANSI C中,函數(shù)的原型非必需,請問這個規(guī)定是優(yōu)點還是缺點?
answer:An advantage is that it allows you to be lazy;there is less code to write.The other consequences,such as being able to call functions with the wrong numbers or types of arguments,are all disadvantages.
(一個優(yōu)點是它允許你偷懶,可以寫更少的代碼,其他后果比如調(diào)用函數(shù)時使用錯誤的數(shù)字或參數(shù)類型都是缺點)
?
3.如果在一個函數(shù)的聲明中它的返回類型為A,但它的函數(shù)體內(nèi)有一條return語句,返回了一個類型為B的表達(dá)式,請問,這將導(dǎo)致什么后果?
answer:The value is converted to the type specified by the function,The Standard indicates that this is done the same as if the value had been assigned to a variable of that type.
(這個值將會被函數(shù)轉(zhuǎn)換成指定的類型A,標(biāo)準(zhǔn)顯示將會把該值指定為返回類型的變量)
?
4.如果一個函數(shù)聲明的返回類型為void,但它的函數(shù)體中包含一條return語句,返回了一個表達(dá)式,請問,這將導(dǎo)致什么后果?
answer:This is not allowed;the compiler should give an error message.
(編譯器不會允許編譯通過而將會給出一條錯誤信息)
?
5.如果一個函數(shù)被調(diào)用之前,編譯器無法看到它的原型,那么當(dāng)這個函數(shù)返回一個不是整型的值時,會發(fā)生什么情況?
answer:The value returned is interpreted as if it were an interger.
(這個值將會被理解為整型)
?
6.如果一個函數(shù)被調(diào)用之前,編譯器無法看到它的原型,如果當(dāng)這個函數(shù)被調(diào)用時,實際傳遞給它的參數(shù)與它的形參類型不匹配,會發(fā)生什么情況?
answer:The argument values are interpreted as the types of the formal paramenters,not their real types.
(參數(shù)的值將會被理解為形參的類型而不是它們實際的類型)
?
7.下面的函數(shù)有沒有錯誤,如果有,錯在哪里?
int find_max(int array[10]) {int i;int max = array[0];for(i = 1; i < 10; i += 1){if(array[i] > max)max = array[i];}return max; }?answer:The function assumes that it will be called with an array of exactly ten elements.If called with a larger array.it ignores the remaining elements,if called with a shorter array ,it accesses values outside of the array.
?(這個函數(shù)假定被調(diào)用的時候數(shù)組有十個元素,當(dāng)被一個更大的數(shù)組調(diào)用是,將忽略一些元素,當(dāng)被一個更小的數(shù)組調(diào)用時,將訪問數(shù)組之外的元素)
?
8.遞歸和while循環(huán)之間為什么相似?
answer:There must be some goal at which the recursion(遞歸) or the iteration(迭代) stops.and each recursive call and each iteration of the loop must make some progress toward this goal.
(它們必須有一些目標(biāo)使遞歸和迭代停止運行,每個遞歸和迭代產(chǎn)生一些過程來接近目標(biāo))
?
9.請解釋把函數(shù)原型單獨放在#include文件中的優(yōu)點
answer:
a. It is easier to use a #include in several source files than to copy the prototype.
(使用include包含與許多源文件中比福祉原型更容易)
b. There is only one copy of the prototype itself.
(只有一份原型的拷貝)
c. #includeing the prototype in the file that defines the function ensures that they match.
(包括原型在文件中,在定義函數(shù)時確保它們匹配)
?
10.在你的系統(tǒng)中,進(jìn)入遞歸形式的菲波那契函數(shù),并在函數(shù)的起始出增加一條語句,它增加一個全局整型變量的值。現(xiàn)在編寫一個main函數(shù),把這個全局變量設(shè)置為0并計算Fibonacci(1).重復(fù)這個過程,計算FIbonacci(2)至Fibonacci(10)。在在每個計算過程中分別調(diào)用了幾次Fibonacci函數(shù)?這個全局變量值的增加和菲波那契數(shù)列本身有沒有任何關(guān)聯(lián)?基于上面這些信息,你能不能計算出Fibonacci(11)、Fibonacci(25)、Fibonacci(50)分別調(diào)用了多少次Fibonacci函數(shù)?
answer:
//參考程序,不含有檢錯操作 #include <stdio.h>int Fibonacci(int n); static int count;int main() {count = 0;int x;printf("please input the x:\n");scanf("%d",&x);Fibonacci(x);printf("the count is %d",count);return 0; }int Fibonacci(int n) {count++;if(n == 1)return 1;else if(n == 2)return 1;elsereturn Fibonacci(n - 1) + Fibonacci(n - 2); }The progression is indeed related to the Fibonacci numbers:each count is the sum of the two preceding counts plus one.Here are the values requested.plus some additional counts to show how bad the recursive function really is.
(這個過程確實跟fibonacci數(shù)字有關(guān)系,每一個計數(shù)加一是前兩項的總和,下面是測試的結(jié)果,count顯示使用遞歸計算fibonacci數(shù)列有多糟糕)
?
| Fibonacci(n) | Number of Calls |
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | 5 |
| 5 | 9 |
| 6 | 15 |
| 7 | 25 |
| 8 | 41 |
| 9 | 67 |
| 10 | 109 |
| 11 | 177 |
| 15 | 1219 |
| 20 | 13529 |
| 25 | 150049 |
| 30 | 1664079 |
| 40 | 204668309 |
| 50 | 25172538049 |
| 75 | 4222970155956099 |
| 100 | 708449696358523830149 |
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
本章練習(xí)
1.Hermite Polynominal(厄米多項式)是這樣定義的
? ? ? ? ? ?? n?≤ 0 :? 1
Hn(x) =? ? n = 1 : 2x
n?≥ 2 : 2xHn-1(x) - 2(n - 1) Hn-2(x)
例如,H3(2)的值是40,請編寫一個遞歸函數(shù),計算Hn(x)的值,你的函數(shù)應(yīng)該與下面的原型匹配:
answer:
#include <stdio.h>int hermite(int n, int x);int main(void) {int n;int x;printf("please input n and x:\n");scanf("%d%d",&n,&x);printf("the Hn(x) is: %d",hermite(n,x)); }int hermite(int n, int x) {if(n <= 0)return 1;else if(n == 1)return 2 * x;elsereturn 2 * x * hermite(n - 1,x) - 2 * (n - 1) * hermite(n - 2,x); }?
2.兩個整型值M和N(M、N均大于0)的最大公約數(shù)可以按照下面的方法計算:
M % N = 0 ?: ? N
gcd(M,N) ? = ? ?
M % N = R, R?> 0 : ? ? gcd(N,R)
answer:
//迭代法 int gcd(int m, int n) {int r;if(m <= 0 || n <= 0)return 0;do{r = m % n;m = n;n = r;}while(r > 0);return m; }//遞歸法 int gcd(int m, int n) {int r;if(m <= 0 || n <= 0)return 0;r = m % n;if(r == 0)return n;else if(r > 0)return gcd(n,r); }?
3.為下面這個函數(shù)原型編寫函數(shù)定義:
int ascii_to_integer(char *string);這個字符串參數(shù)必須包含一個或多個數(shù)字,函數(shù)應(yīng)該把這些數(shù)字字符轉(zhuǎn)換為整數(shù)并返回這個整數(shù),如果字符串參數(shù)包含了任何非數(shù)字字符,函數(shù)就返回零。請不必?fù)?dān)心算數(shù)溢出。提示:這個技巧很簡單,你每發(fā)現(xiàn)一個數(shù)字,把當(dāng)前值乘以10,并把這個值和新數(shù)字所代表的值相加。
answer:
int ascii_to_integer(char *string) {int result = 0;char *p = string;while(*p >= '0' && *p <= '9'){result *= 10;result += *p - '0';p++;}if(*p != '\0')result = 0;return result; }?
4.編寫一個名叫max_list的函數(shù),它用于檢查任意數(shù)目的整型參數(shù)并返回它們中最大值。參數(shù)列表必須以一個負(fù)數(shù)結(jié)尾,提示列表結(jié)束。
answer:
int max_list(int x,...){int max = x;va_list var_list;va_start(var_list,x);do{int temp = va_arg(var_list,int);if(temp > max)max = temp;}while(temp > 0);va_end(var_list);return max; }
?
5.實現(xiàn)一個簡化的printf函數(shù),它能夠處理%d,%f,%s和%c格式碼,根據(jù)ANSI標(biāo)準(zhǔn)的原則,其他格式碼的行為是未定義的,你可以假定已經(jīng)存在函數(shù)print_integer和print_float,用于打印這些類型的值,對于另外兩種類型的值,使用putchar來打印。
answer:
void myprintf(char *string,...) {char *p = string;va_list var_list;va_start(var_list,string);while(*p != '\0'){if(*p == '%'){switch(*++p){case 'd':print_integer(va_arg(var_list,int));break;case 'f':print_float(va_arg(var_list,float));break;case 'c':putchar(va_arg(var_list,int));case 's':char *q = va_arg(var_list,char *);while(*q != '\0')putchar(*q++);break;default:break;}}else{putchar(*p);}p++;}va_end(var_list); }?
6.編寫函數(shù):
void written_amount(unsigned int amount,char *buffer);
它把amount表示的值轉(zhuǎn)化為單詞形式,并存儲于buffer中,這個函數(shù)可以在一個打印支票的程序中使用。例如,如果amount的值是16312,那么buffer中存儲的字符串應(yīng)該是:SIXTEEN THOUSAND THREE HUNDRED TWELVE
調(diào)用程序應(yīng)該保證buffer緩沖區(qū)的空間足夠大。
有些值可以用兩種不同的方法進(jìn)行打印。例如,1200可以是ONE THOUSAND TWO HUNDRED或TWELVE HUNDRED。你可以選擇一種你喜歡的形式。
answer:
#include <stdio.h> #include <string.h>#define LONG 1000char *unit[10] = {" zero"," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"};char *ten[10] = {" ten"," eleven"," twelve"," thirteen"," fourteen"," fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};char *decade[10] = {" "," "," twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"};void written_amount(unsigned long int amount,char *buffer);int main() {unsigned long int amount;char buffer[LONG] = "";scanf("%lu",&amount);written_amount(amount,buffer);printf("%s",buffer);return 0; }void written_amount(unsigned long int amount,char *buffer) {unsigned long int n = 100000000;if(amount / n != 0){int temp = amount / n;amount %= n;written_amount(temp,buffer);strcat(buffer," billion");}n = 100000;if(amount / n != 0){int temp = amount / n;amount %= n;written_amount(temp,buffer);strcat(buffer," million");}n = 100;if(amount / n != 0){int temp = amount / n;amount %= n;written_amount(temp,buffer);strcat(buffer," hundred");}n = 10;if(amount / n != 0){int temp = amount / n;amount %= n;if(temp == 1){strcat(buffer,ten[amount]);}else{strcat(buffer,decade[temp]);if(amount != 0)strcat(buffer,unit[amount]);}}else{strcat(buffer,unit[amount]);} }?
轉(zhuǎn)載于:https://www.cnblogs.com/monster-prince/p/6072607.html
總結(jié)
以上是生活随笔為你收集整理的《C与指针》第七章练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: reactNative 打包那些事儿
- 下一篇: Python 【第八章】:JavaScr