《C与指针》第七章练习
本章問題
1.具有空函數體的函數可以作為存根使用,你如何對這類函數進行修改,使其更有用?
answer:Have the stub(存根) print out a message when it is called,perhaps printing the values it was given as arguments.
(存根可以在它被調用的時候打印出相應的信息,可能打印出參數的值)
?
2.在ANSI C中,函數的原型非必需,請問這個規定是優點還是缺點?
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.
(一個優點是它允許你偷懶,可以寫更少的代碼,其他后果比如調用函數時使用錯誤的數字或參數類型都是缺點)
?
3.如果在一個函數的聲明中它的返回類型為A,但它的函數體內有一條return語句,返回了一個類型為B的表達式,請問,這將導致什么后果?
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.
(這個值將會被函數轉換成指定的類型A,標準顯示將會把該值指定為返回類型的變量)
?
4.如果一個函數聲明的返回類型為void,但它的函數體中包含一條return語句,返回了一個表達式,請問,這將導致什么后果?
answer:This is not allowed;the compiler should give an error message.
(編譯器不會允許編譯通過而將會給出一條錯誤信息)
?
5.如果一個函數被調用之前,編譯器無法看到它的原型,那么當這個函數返回一個不是整型的值時,會發生什么情況?
answer:The value returned is interpreted as if it were an interger.
(這個值將會被理解為整型)
?
6.如果一個函數被調用之前,編譯器無法看到它的原型,如果當這個函數被調用時,實際傳遞給它的參數與它的形參類型不匹配,會發生什么情況?
answer:The argument values are interpreted as the types of the formal paramenters,not their real types.
(參數的值將會被理解為形參的類型而不是它們實際的類型)
?
7.下面的函數有沒有錯誤,如果有,錯在哪里?
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.
?(這個函數假定被調用的時候數組有十個元素,當被一個更大的數組調用是,將忽略一些元素,當被一個更小的數組調用時,將訪問數組之外的元素)
?
8.遞歸和while循環之間為什么相似?
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.
(它們必須有一些目標使遞歸和迭代停止運行,每個遞歸和迭代產生一些過程來接近目標)
?
9.請解釋把函數原型單獨放在#include文件中的優點
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.
(包括原型在文件中,在定義函數時確保它們匹配)
?
10.在你的系統中,進入遞歸形式的菲波那契函數,并在函數的起始出增加一條語句,它增加一個全局整型變量的值。現在編寫一個main函數,把這個全局變量設置為0并計算Fibonacci(1).重復這個過程,計算FIbonacci(2)至Fibonacci(10)。在在每個計算過程中分別調用了幾次Fibonacci函數?這個全局變量值的增加和菲波那契數列本身有沒有任何關聯?基于上面這些信息,你能不能計算出Fibonacci(11)、Fibonacci(25)、Fibonacci(50)分別調用了多少次Fibonacci函數?
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數字有關系,每一個計數加一是前兩項的總和,下面是測試的結果,count顯示使用遞歸計算fibonacci數列有多糟糕)
?
| 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 |
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
本章練習
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,請編寫一個遞歸函數,計算Hn(x)的值,你的函數應該與下面的原型匹配:
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)的最大公約數可以按照下面的方法計算:
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.為下面這個函數原型編寫函數定義:
int ascii_to_integer(char *string);這個字符串參數必須包含一個或多個數字,函數應該把這些數字字符轉換為整數并返回這個整數,如果字符串參數包含了任何非數字字符,函數就返回零。請不必擔心算數溢出。提示:這個技巧很簡單,你每發現一個數字,把當前值乘以10,并把這個值和新數字所代表的值相加。
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的函數,它用于檢查任意數目的整型參數并返回它們中最大值。參數列表必須以一個負數結尾,提示列表結束。
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.實現一個簡化的printf函數,它能夠處理%d,%f,%s和%c格式碼,根據ANSI標準的原則,其他格式碼的行為是未定義的,你可以假定已經存在函數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.編寫函數:
void written_amount(unsigned int amount,char *buffer);
它把amount表示的值轉化為單詞形式,并存儲于buffer中,這個函數可以在一個打印支票的程序中使用。例如,如果amount的值是16312,那么buffer中存儲的字符串應該是:SIXTEEN THOUSAND THREE HUNDRED TWELVE
調用程序應該保證buffer緩沖區的空間足夠大。
有些值可以用兩種不同的方法進行打印。例如,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]);} }?
轉載于:https://www.cnblogs.com/monster-prince/p/6072607.html
總結
以上是生活随笔為你收集整理的《C与指针》第七章练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: reactNative 打包那些事儿
- 下一篇: Python 【第八章】:JavaScr