《The C Programming Language》答案(第三章)
P1
#include <stdio.h>
int binsearch ( int x, int v[ ] , int n) {
int low = 0 ;
int high = n- 1 ;
int mid;
while ( low< high) {
mid = ( low+ high) / 2 ;
if ( x<= v[ mid] )
high = mid;
else
low = mid+ 1 ;
}
return ( x== v[ low] ) ? low: - 1 ;
}
int main ( ) {
int x = 19 , n = 10 ;
int v[ 10 ] = { 1 , 1 , 4 , 5 , 14 , 19 , 19 , 81 , 810 , 1024 } ;
printf ( "The index is: %d\n" , binsearch ( x, v, n) ) ;
return 0 ;
}
P2
void escape ( char s[ ] , char t[ ] ) {
int i, j;
for ( i= 0 , j= 0 ; t[ i] != '\0' ; i++ ) {
switch ( t[ i] ) {
case '\n' :
s[ j++ ] = '\\' ;
s[ j++ ] = 'n' ;
break ;
case '\t' :
s[ j++ ] = '\\' ;
s[ j++ ] = 't' ;
break ;
case '\v' :
s[ j++ ] = '\\' ;
s[ j++ ] = 'v' ;
break ;
case '\b' :
s[ j++ ] = '\\' ;
s[ j++ ] = 'b' ;
break ;
case '\r' :
s[ j++ ] = '\\' ;
s[ j++ ] = 'r' ;
break ;
case '\f' :
s[ j++ ] = '\\' ;
s[ j++ ] = 'f' ;
break ;
case '\a' :
s[ j++ ] = '\\' ;
s[ j++ ] = 'a' ;
break ;
case '\?' :
s[ j++ ] = '\\' ;
s[ j++ ] = '\?' ;
break ;
case '\'' :
s[ j++ ] = '\\' ;
s[ j++ ] = '\'' ;
break ;
case '\"' :
s[ j++ ] = '\\' ;
s[ j++ ] = '\"' ;
break ;
default :
s[ j++ ] = t[ i] ;
break ;
}
}
s[ j] = '\0' ;
}
void unescape ( char s[ ] , char t[ ] ) {
int i, j;
for ( i= 0 , j= 0 ; t[ i] != '\0' ; i++ ) {
if ( t[ i] == '\\' ) {
switch ( t[ ++ i] ) {
case 'n' :
s[ j++ ] = '\n' ;
break ;
case 't' :
s[ j++ ] = '\t' ;
break ;
case 'v' :
s[ j++ ] = '\v' ;
break ;
case 'b' :
s[ j++ ] = '\b' ;
break ;
case 'r' :
s[ j++ ] = '\r' ;
break ;
case 'f' :
s[ j++ ] = '\f' ;
break ;
case 'a' :
s[ j++ ] = '\a' ;
break ;
case '\\' :
s[ j++ ] = '\\' ;
break ;
case '\?' :
s[ j++ ] = '\?' ;
break ;
case '\'' :
s[ j++ ] = '\'' ;
break ;
case '\"' :
s[ j++ ] = '\"' ;
break ;
default :
s[ j++ ] = '\\' ;
-- i;
break ;
}
} else {
s[ j++ ] = t[ i] ;
}
}
s[ j] = '\0' ;
}
P3
#include <stdio.h>
#define MAXLINE 1024
int getchars ( char s[ ] , int lim) {
int c, i, l;
for ( i= 0 , l= 0 ; ( c= getchar ( ) ) != EOF ; i++ ) {
if ( i< lim- 1 )
s[ l++ ] = c;
}
s[ l] = '\0' ;
return l;
}
void expand ( char s1[ ] , char s2[ ] ) {
int ptr1, ptr2, ptrsp;
int c;
for ( ptr1= 0 , ptr2= 0 ; s1[ ptr1] != '\0' ; ptr1++ ) {
if ( s1[ ptr1+ 1 ] == '-' && ( c= s1[ ptr1+ 2 ] ) != '\0' &&
( ( 'a' <= s1[ ptr1] && s1[ ptr1] <= 'z' && s1[ ptr1] <= c&& c<= 'z' ) ||
( 'A' <= s1[ ptr1] && s1[ ptr1] <= 'Z' && s1[ ptr1] <= c&& c<= 'Z' ) ||
( '0' <= s1[ ptr1] && s1[ ptr1] <= '9' && s1[ ptr1] <= c&& c<= '9' ) ) ) {
ptrsp= 0 ;
while ( ptrsp<= c- s1[ ptr1] ) {
s2[ ptr2++ ] = s1[ ptr1] + ptrsp+ 1 ;
ptrsp++ ;
}
ptr1+ = 2 ;
} else {
s2[ ptr2++ ] = s1[ ptr1] ;
}
}
s2[ ptr1] = '\0' ;
}
int main ( ) {
char s1[ MAXLINE] ;
char s2[ MAXLINE] ;
printf ( "Input: \n" ) ;
while ( getchars ( s1, MAXLINE) == 0 )
;
expand ( s1, s2) ;
printf ( "The result is %s\n" , s2) ;
return 0 ;
}
P4
#include <stdio.h>
#include <limits.h>
#define MAXLINE 1024
void itoa ( int n, char s[ ] )
{
int i, sign, remainder;
sign = n;
i = 0 ;
do {
remainder = n % 10 ;
s[ i++ ] = ( ( sign < 0 ) ? - remainder : remainder) + '0' ;
} while ( n / = 10 ) ;
if ( sign < 0 )
s[ i++ ] = '-' ;
s[ i] = '\0' ;
reverse ( s) ;
}
void reverse ( char s[ ] ) {
int i, j;
int tmp;
for ( j = 0 ; s[ j] != '\0' ; ++ j)
;
-- j;
for ( i = 0 ; i < j; ++ i, -- j) {
tmp = s[ i] ;
s[ i] = s[ j] ;
s[ j] = tmp;
}
}
int main ( ) {
char s[ MAXLINE] ;
itoa ( INT_MIN, s) ;
printf ( "%d ---> %s\n" , INT_MIN, s) ;
}
P5
#include <stdio.h>
#include <limits.h>
#define MAXLINE 1024
void reverse ( char s[ ] )
{
int i, j;
int tmp;
for ( j = 0 ; s[ j] != '\0' ; ++ j)
;
-- j;
for ( i = 0 ; i < j; ++ i, -- j) {
tmp = s[ i] ;
s[ i] = s[ j] ;
s[ j] = tmp;
}
}
void itob ( int n, char s[ ] , int b)
{
int i, sign, remainder;
if ( b < 2 || b > 36 )
return ;
sign = n;
i = 0 ;
do {
remainder = n % b;
s[ i++ ] = ( ( sign < 0 ) ? - remainder : remainder) + '0' ;
} while ( n / = b) ;
if ( sign < 0 )
s[ i++ ] = '-' ;
s[ i] = '\0' ;
reverse ( s) ;
}
int main ( ) {
char s[ MAXLINE] ;
itob ( INT_MIN, s, 8 ) ;
printf ( "%d ---> %s" , INT_MIN, s) ;
}
P6
#include <stdio.h>
#include <limits.h>
#define MAXLINE 1024
void reverse ( char s[ ] )
{
int i, j;
int tmp;
for ( j = 0 ; s[ j] != '\0' ; ++ j)
;
-- j;
for ( i = 0 ; i < j; ++ i, -- j) {
tmp = s[ i] ;
s[ i] = s[ j] ;
s[ j] = tmp;
}
}
void itoa ( int n, char s[ ] , int w)
{
int i, sign, remainder, k, j;
sign = n;
i = 0 ;
do {
remainder = n % 10 ;
s[ i++ ] = ( ( sign < 0 ) ? - remainder : remainder) + '0' ;
} while ( n / = 10 ) ;
if ( sign < 0 )
s[ i++ ] = '-' ;
while ( i < w)
s[ i++ ] = ' ' ;
s[ i] = '\0' ;
reverse ( s) ;
}
int main ( ) {
char s[ MAXLINE] ;
int width = 14 ;
itoa ( INT_MIN, s, width) ;
printf ( "%12d ---> %s\n" , INT_MIN, s) ;
}
第三章 完