c语言程序设计现代方法(2th)第12章答案(自己胡乱编写的答案,持续更新)
生活随笔
收集整理的這篇文章主要介紹了
c语言程序设计现代方法(2th)第12章答案(自己胡乱编写的答案,持续更新)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一部分:練習題答案
1.答案見運行結果
#include <stdio.h> int main() { int a[] = {5,15,34,54,14,2,52,72}; int *p =&a[1],*q=&a[5]; //(a) printf("%d\n",*(p+3)); //(b) printf("%d\n",*(q-3)); //(c) printf("%ld\n",q-p); //(d) printf("%d\n",p < q); //(e) printf("%d\n",*p < *q);return 0; } 運行結果如下: 14 34 4 1 02.答案
#include <stdio.h> int main() { int a[] = {1,2,3,4,5,6,7,8,9,10,11,12}; int *low=&a[2],*high = &a[6]; int *p = low + (high - low)/2; //這里是答案 printf("%d",*p);return 0; }3.答案
#include <stdio.h> #define N 10 int main() { int i; int a[N] = {1,2,3,4,5,6,7,8,9,10}; int *p = &a[0],*q=&a[N-1],temp; while(p < q) { temp = *p; *p++= *q; *q-- = temp; } for(i = 0;i != sizeof(a)/ sizeof(a[0]);++i)printf("%d ",a[i]);printf("\n");return 0; }運行結果如下(答案):
10 9 8 7 6 5 4 3 2 14.
#include <stdio.h> #include <stdbool.h> #define STACK_SIZE 100 int contents[STACK_SIZE]; int * top = &contents[0]; void make_empty(void); bool is_empty(void); bool if_full(void); void push(int); int *pop(void); void stack_underflow(void); void stack_overflow(void); void make_empty(void) {top = &contents[0]; } bool is_empty(void) {return (top == &contents[0]); } bool is_full(void) {return (top == &contents[STACK_SIZE]); } void push(int i) { if(is_full())stack_overflow(); else*top++ = i; } int *pop(void) { if(is_empty()) {stack_underflow();return &contents[0];} elsereturn (top --); } void stack_underflow(void) { printf("Stack under flow."); } void stack_overflow(void) {printf("Stack over flow."); }//下面是自己舉的一個例子 int main() { int i,*p; //把1~19的平方壓入棧中: for(i = 0;i != 20;++i)push( i*i); //打印棧中所有元素: for(p = contents;p!=top;++p)printf("%d\n",*p); //彈出棧中除了第1個元素外的其余元素: printf("Now ,it will be reduced:\n"); while(top != &contents[0])pop(); //打印棧的最后一個元素 printf("%d\n",*(top-1)); }5.假設a是一維數組而p是指針變量。如果剛執行了賦值操作p=a,下列哪些表達式會因為類型不匹配而不合法?其他的表達式中哪些為真(有非零值)?
答:(a)不合法(b)合法(c)合法(d)合法
貌似合法的都有菲零值。
14.
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> bool find_temp(const int *,int ,int); int main() { int temperatures[7][12] = {1,2,3,4,24}; const int (*p)[12]; bool result = false; for(p = temperatures; p != temperatures + 7; ++p) { result = find_temp(*p,sizeof(*p) / sizeof((*p)[0]),24); if(result){printf("There is 32 degree.");exit(EXIT_SUCCESS);}}return 0; }bool find_temp(const int *a,int n,int key) {const int *p; bool result = false; for(p = a; p!= a+n; ++p) { if(*p == key)result = true; } return result; }15.
#include <stdio.h> void print(const int a[],int n); int main() { int temperatures[7][12] = {1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12}; int (*p)[12]; p = temperatures; int i; printf("Enter value of i:\n"); scanf("%d",&i); p = p + i - 1; print(*p,sizeof(*p)/sizeof((*p)[0]));return 0; }void print(const int a[],int n) { const int *p; for(p = a;p != a+n;++p)printf("%d ",*p); }16.
#include <stdio.h> int find_max(const int a[],int); int main() { int temperatures[7][12]= {{1,2,3,4,5},{1,2,3,1,4},{1,23,1,4},{12,3,1,2,4},{1,2,4,1},{2,34,52,5},{23,42,3,4}}; int (*p)[12]; p = temperatures; int max_value; for( p = temperatures;p != temperatures + 7;++p) { max_value = find_max(*p,sizeof(*p)/sizeof((*p)[0])); printf("max value is: %d\n",max_value); }return 0; }int find_max(const int a[],int n) { int max = *a; const int *p = a+1; for(; p != a + n;++ p){if(*p > max)max = *p;} return max; }17.這個答案也可以用一維數組實現,但是如果碰到越界檢查的編譯器,會報錯。所以自己做了下面的兩個函數實現的代碼。
#include <stdio.h> #define LEN 5 int sum_array(const int a[],int n); int sum_two_dimensional_array(const int a[][LEN],int n); int main() { int array[4][LEN] ={{1,2,3,4},{11,22,33,44},{111,222,333,444},{1111,2222,3333, 4444}}; int sum; //how to calculate rows of a 2 dimensional array //that's it.see next codes. sum = sum_two_dimensional_array(array,sizeof(array)/sizeof(array[0]));printf("sum is: %d.\n",sum); return 0; } int sum_two_dimensional_array(const int a[][LEN],int n) { const int (*p)[LEN]; int sum = 0; for(p = a; p!= a+n;++p) { sum += sum_array(*p,sizeof(*p)/sizeof((*p)[0])); } return sum; } int sum_array(const int a[],int n) { int sum = 0; const int *p; for(p = a; p!= a+n;++p) { sum += *p; } return sum; }18.
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #define COL 8 #define ROW 8 bool is_big(char ); bool is_good(char ); void initialize(char a[][COL],int n); int analyze(char array[][COL],int n); int main() { char array[ROW][COL]; int result; initialize(array,sizeof(array) / sizeof(array[0])); result = analyze(array,sizeof(array)/ sizeof(array[0])); printf("Result is: %d\n",result);return 0; } void initialize(char a[][COL],int n) { char ch; printf("Enter %d characters(k,q,r,b,n,p):",COL); char (*p)[COL]; p = a; char *p1; for(p = a;p != a + n ;++p){for(p1 = *p;p1 != *p + COL;++p1){ch = getchar();while(!is_good(ch)) { printf("\nBad char,try again.");ch = getchar(); }*p1 = ch;}//for2//if all elements are have given,program will escape.if(p == a+n-1 && p1 == (*p) + COL)return ;printf("\nEnter 8 char.");while((ch = getchar()) != '\n'){; }}//for1}bool is_good(char ch) { if(!(ch == 'k' || ch == 'q' || ch == 'r'|| ch == 'b'|| ch == 'n'|| ch == 'p'||ch=='K'|| ch == 'Q'|| ch == 'R'|| ch == 'B'|| ch == 'N' || ch == 'P') )return false; else return true; }bool is_big(char ch) { if(ch >= 'A' && ch <= 'Z')return true; else if(ch >= 'a' && ch <= 'z')return false; else {printf("Wrong character!"); return false; } } //n is row of array int analyze(char array[][COL],int n) { char (*p)[COL]; char *q; int big_num = 0,small_num = 0; for(p = array; p!= array+n;++p)for(q = (*p); q != (*p) + COL;++q){//if the character is big character if(is_big(*q)){++ big_num;} else ++ small_num; } if(big_num > small_num)return big_num - small_num; else return small_num - big_num;}?在ubuntu系統中運行如下(這里輸入64個字符實在麻煩,所以很多都輸入一樣的了):
r@r:~/coml_/c/12/exercies_self$ gcc 18.c -o 123 r@r:~/coml_/c/12/exercies_self$ ./123 Enter 8 characters(k,q,r,b,n,p):kkkkkkkkkEnter 8 char.qqqqqqqqEnter 8 char.RRRRRRRREnter 8 char.BBBBBBBBEnter 8 char.NNNNNNNNEnter 8 char.PPPPPPPPEnter 8 char.PPPPPPPPEnter 8 char.KQRBNPPP Result is: 32第二部分:編程題
1.
#include <stdio.h> #define N 100 void read(char a[],int n); void reverse(char *array,int ); void set_space(char array[],int ); int main() { char array[N]; //set all elements to ' ' set_space(array,N); read(array,sizeof(array) / sizeof(array[0])); reverse(array,sizeof(array)/sizeof(array[0])); char *p,*pos; //find the first element which is not ' ',put it into pos for(p = array + N -1;p >= array;--p)if(*p != ' '){pos = p;break;} for(p = array;p != pos + 1;++p)printf("%c",*p); return 0; }void read(char a[],int n) { char ch; ch = getchar(); char *p = a;while(ch != '\n' && p != a+n){*p++ = ch;ch =getchar();} } void reverse(char array[],int n) { //detecting how many elements which are not '' int cnt = 0; char *p,*pos; char temp; //find first element which is not ' ',put it to 'pos' for(p = array + n -1 ; p >= array; --p) { if(*p != ' ') {pos = p;break;} } //swap elements char *q = pos; p = array; while(p < q) { temp = *p; *p++ = *q; *q-- = temp; } }void set_space(char array[],int n) { char *p = array; while(p != array + n){*p++ = ' ';}}2.
#include <stdio.h> #include <stdbool.h> #define N 100 bool is_palindrome(char *,int ); bool is_equal(char *,char *,int ); char * no_space_pos(char *,int n); void read(char *,int ); void print(char array[],int n); void set_space(char array[],int n); int main() { //set all elements to ' ' char array[N] = {' '}; set_space(array,N); read(array,N); printf("\n"); bool result = is_palindrome(array,N);if(result)printf("Palindrome"); elseprintf("Not palindrome"); return 0; }bool is_palindrome(char *array,int n) { char *p,*pos; for(p = array + n-1;p >= array;--p){ if(*p != ' '){pos = p;break;}}if(*p == '?' || *p == '.' || *p == ',' || *p == '!')p --;char *q = array; while(q < p) { if(*p-- != *q++)return false; } return true; }bool is_equal(char *array1,char *array2,int n) { char *p =array1,*q = array2; for(p = array1,q = array2;p!=array1+n && q!= array2+n;++p,++q) {if(*p != *q)return false; } return true; }void read(char *array,int n) { char ch; char *p = array; while((ch = getchar()) != '\n') {*p ++ = ch;if(p == array +n)return ; }}void print(char array[],int n) { char *p = array,*q = array + n -1; while(*q == ' ') { q --; } while(p <= q)printf("%c",*p++);} void set_space(char array[],int n) { char *p = array; while(p != array + n) { *p++ = ' '; }}注意以下問題:
1.原始數組array在使用之前必須全部重置為某個非打印字符比如空格,否則數組將會把每個元素隨機初始化為某個不可打印字符(我的編譯器是如此的)
千萬不要認為,char array[N] = {'? '}這一句話會把每個元素置為空格,要調用for或者while把每個元素都置為空格
2.做了第一步后,很容易編寫循環偵測到語句是從哪個地方開始結束了。不然很難。
5.
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #define N 100 void set(char array[],int n); void print(char *,char *); char *find_last(char *p1); void reverse(char array[],int n); void read(char array[],int n,char *last_char); int main() { char last; char array[N]; set(array,N); read(array,N,&last);reverse(array,N); printf("%c",last);return 0; }//set all elements to ' ' void set(char array[],int n) { char *p = array; while(p != array +n){*p = ' ';++ p;} } //reverse and output the word of the input statement void reverse(char array[],int n) { //find the last element's position? char *pos= array + n -1; //set the position to last element ' s position while(*pos ==' ') {--pos;} char *head = pos,*tail = pos; while(*head != ' ' && head != array)head --; if(*head == ' ')++head;while(true) { print(head,tail);if(head != array)printf(" ");if(head == array)break;head --;while(*head == ' ' && head != array){head --; }if(*head == ' ')break;//頭部有空格的情況,則立刻終止循環tail = head;while(head != array && *head != ' ')--head; if(*head == ' ')head ++;}}void print(char *p,char * q) { char *a; for(a = p; a <= q;++a)printf("%c",*a); }//find the last element's postion of the array char* find_last(char * p1) { char *p = p1; while(*p == ' ') { --p; } return p; } void read(char array[],int n,char *last_char) { printf("Eneter a setence:"); char ch; char *p = array; ch = getchar();while(ch != '\n'){if(ch == '.' || ch == '?' || ch == '!'){*last_char=ch;break;}*p = ch;++p;if(p == array + n)break;ch = getchar();}//while}6.這里是快速排序用
指針改寫,下面先來個下標版本的快速排序程序:
下面是指針版本的快速排序版本:
#include <stdio.h> int * split(int array[],int *,int *); void sort(int array[],int *,int * ); void print(int array[],int n); void print2(int array[],int n); int main() { int array[]= {21,13,23,4,23,2,1,-1023,987,123124}; split(array,array,array + sizeof(array) / sizeof(array[0]) - 1); sort(array,array,array + sizeof(array) / sizeof(array[0]) -1); print(array,sizeof(array)/sizeof(array[0])); print2(array,sizeof(array) / sizeof(array[0])); return 0; }int * split(int array[],int *low,int *high) { if(low >= high) return low; //這里注意一定是 temp = *low;絕對不要寫成 temp = *array; //每次迭代時候是把指針參數范圍最左邊元素賦值給temp,也就是基準數. int temp = *low; while(high > low) {while(*high >= temp && high > low)high --;if(high == low) break;*low ++ = *high;if(low == high) break;while(*low < temp && high > low)low ++;if(high == low)break;*high -- = *low; }//while *high = temp;return low; }void sort(int array[],int *low ,int *high) { if(low >= high) return; int *middle = split(array,low,high); sort(array,low,middle -1); sort(array,middle + 1,high); }void print(int array[],int n) { int *p = array; for(;p != array + n;++p)printf("%4d",*p); printf("\n"); }void print2(int array[],int n) { int i; for(i = 0; i != n; ++i)printf("%7d",array[i]); }注意分割函數的temp的內容,一定是指針范圍內的最左邊元素,而不是數組最左邊元素。因為每次迭代左邊元素不一定是數組最左邊元素array[0]
7.
#include <stdio.h> #define N 10 void read(int array[],int ); void max_min(int *array,int *,int*); int main() { int min,max; int array[N],number; read(array,sizeof(array) / sizeof(array[0])); max_min(array,&max,&min); printf("max number: %d\nmin number: %d\n",max,min); return 0; } void read(int array[],int n) { printf("Enter ten numbers:\n"); int num,cnt = 0; int *p = array; while(cnt != 10) { scanf("%d",&num); *p++ = num; ++cnt; } } void max_min(int array[],int *max,int *min) { int *p; *max = *array,*min = *array; for(p = array; p != array + N; ++p) { if(*p > *max)*max = *p; if(*p < *min)*min = *p; } } r@r:~/coml_/c/12/program$ gcc 7.c -o 123 r@r:~/coml_/c/12/program$ ./123 Enter ten numbers: 1 23 1 25 1025 -1 234 999 2455 324 max number: 2455 min number: -1?
?
?
總結
以上是生活随笔為你收集整理的c语言程序设计现代方法(2th)第12章答案(自己胡乱编写的答案,持续更新)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 迭代器和反向迭代器,常量迭代器和非常量迭
- 下一篇: 如何学习计算机网络