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
0

2.答案

#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 1 

4.

#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];}
else
       return (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):kkkkkkkkk

Enter 8 char.qqqqqqqq

Enter 8 char.RRRRRRRR

Enter 8 char.BBBBBBBB

Enter 8 char.NNNNNNNN

Enter 8 char.PPPPPPPP

Enter 8 char.PPPPPPPP

Enter 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");
else
	printf("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>
#define N 100
void print(int array[],int n);
int sort(int array[],int low,int );
void all_sort(int array[],int high,int low);
int main()
{
int array[] = {12,4,7,5,3,121,23,45,5,6,4,6,5,7,8};
print(array,sizeof(array) / sizeof(array[0]));
printf("\n");
sort(array,0,sizeof(array)/ sizeof(array[0])-1);
all_sort(array,0,sizeof(array)/sizeof(array[0]) - 1);
print(array,sizeof(array)/sizeof(array[0]));
return 0;
}

int sort(int array[],int low,int high)
{
if(low >= high) return 0;
int temp = array[low];	
   while(low < high)
   {
    //第一步:如果高位的数都比基准数大,且high != low
    //注意这个地方一定要high > low才能运行,也就是当high == low,已经迭代完毕
    //就不能再运行了
     while(array[high] >= temp && high > low)
	      high --;
      if(high == low)break;
      array[low++] = array[high];
      //这里也是,high > low才能运行,high == low也是不能运行
      //high < low更不能运行
      while(array[low] < temp && high > low)
	      low ++;
      if(high == low) break;
      array[high--] = array[low]; 
   }
array[low] = temp;
return high;
}

void print(int array[],int n)
{
int *p = array;
for(p = array ; p!= array +n;++p)
	printf("%3d ",*p);
}

void all_sort(int array[],int low,int high)
{
//这里注意在运行递归的时候,low>=high都是可能出现的,这个时候要立刻return
if(low >= high) return;
int middle;
middle = sort(array,low,high);
all_sort(array,low,middle-1);
all_sort(array,middle+1,high);
}

下面是指针版本的快速排序版本:

#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

猜你喜欢

转载自blog.csdn.net/digitalkee/article/details/112388967