Table of Contents for
Intermediate C Programming

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Intermediate C Programming by Yung-Hsiang Lu Published by CRC Press, 2015
  1. Front Cover
  2. Contents (1/2)
  3. Contents (2/2)
  4. List of Figures (1/2)
  5. List of Figures (2/2)
  6. List of Tables
  7. Foreword
  8. Preface
  9. Author, Reviewers, and Artist
  10. Rules in Software Development
  11. Source Code
  12. I. Computer Storage: Memory and File
  13. 1. Program Execution (1/2)
  14. 1. Program Execution (2/2)
  15. 2. Stack Memory (1/5)
  16. 2. Stack Memory (2/5)
  17. 2. Stack Memory (3/5)
  18. 2. Stack Memory (4/5)
  19. 2. Stack Memory (5/5)
  20. 3. Prevent, Detect, and Remove Bugs (1/2)
  21. 3. Prevent, Detect, and Remove Bugs (2/2)
  22. 4. Pointers (1/6)
  23. 4. Pointers (2/6)
  24. 4. Pointers (3/6)
  25. 4. Pointers (4/6)
  26. 4. Pointers (5/6)
  27. 4. Pointers (6/6)
  28. 5. Writing and Testing Programs (1/4)
  29. 5. Writing and Testing Programs (2/4)
  30. 5. Writing and Testing Programs (3/4)
  31. 5. Writing and Testing Programs (4/4)
  32. 6. Strings (1/3)
  33. 6. Strings (2/3)
  34. 6. Strings (3/3)
  35. 7. Programming Problems and Debugging (1/4)
  36. 7. Programming Problems and Debugging (2/4)
  37. 7. Programming Problems and Debugging (3/4)
  38. 7. Programming Problems and Debugging (4/4)
  39. 8. Heap Memory (1/3)
  40. 8. Heap Memory (2/3)
  41. 8. Heap Memory (3/3)
  42. 9. Programming Problems Using Heap Memory (1/4)
  43. 9. Programming Problems Using Heap Memory (2/4)
  44. 9. Programming Problems Using Heap Memory (3/4)
  45. 9. Programming Problems Using Heap Memory (4/4)
  46. 10. Reading and Writing Files (1/3)
  47. 10. Reading and Writing Files (2/3)
  48. 10. Reading and Writing Files (3/3)
  49. 11. Programming Problems Using File (1/2)
  50. 11. Programming Problems Using File (2/2)
  51. II. Recursion
  52. 12. Recursion (1/4)
  53. 12. Recursion (2/4)
  54. 12. Recursion (3/4)
  55. 12. Recursion (4/4)
  56. 13. Recursive C Functions (1/4)
  57. 13. Recursive C Functions (2/4)
  58. 13. Recursive C Functions (3/4)
  59. 13. Recursive C Functions (4/4)
  60. 14. Integer Partition (1/5)
  61. 14. Integer Partition (2/5)
  62. 14. Integer Partition (3/5)
  63. 14. Integer Partition (4/5)
  64. 14. Integer Partition (5/5)
  65. 15. Programming Problems Using Recursion (1/5)
  66. 15. Programming Problems Using Recursion (2/5)
  67. 15. Programming Problems Using Recursion (3/5)
  68. 15. Programming Problems Using Recursion (4/5)
  69. 15. Programming Problems Using Recursion (5/5)
  70. III. Structure
  71. 16. Programmer-Defined Data Types (1/6)
  72. 16. Programmer-Defined Data Types (2/6)
  73. 16. Programmer-Defined Data Types (3/6)
  74. 16. Programmer-Defined Data Types (4/6)
  75. 16. Programmer-Defined Data Types (5/6)
  76. 16. Programmer-Defined Data Types (6/6)
  77. 17. Programming Problems Using Structure (1/4)
  78. 17. Programming Problems Using Structure (2/4)
  79. 17. Programming Problems Using Structure (3/4)
  80. 17. Programming Problems Using Structure (4/4)
  81. 18. Linked Lists (1/3)
  82. 18. Linked Lists (2/3)
  83. 18. Linked Lists (3/3)
  84. 19. Programming Problems Using Linked List (1/2)
  85. 19. Programming Problems Using Linked List (2/2)
  86. 20. Binary Search Trees (1/4)
  87. 20. Binary Search Trees (2/4)
  88. 20. Binary Search Trees (3/4)
  89. 20. Binary Search Trees (4/4)
  90. 21. Parallel Programming Using Threads (1/5)
  91. 21. Parallel Programming Using Threads (2/5)
  92. 21. Parallel Programming Using Threads (3/5)
  93. 21. Parallel Programming Using Threads (4/5)
  94. 21. Parallel Programming Using Threads (5/5)
  95. IV. Applications
  96. 22. Finding the Exit of a Maze (1/5)
  97. 22. Finding the Exit of a Maze (2/5)
  98. 22. Finding the Exit of a Maze (3/5)
  99. 22. Finding the Exit of a Maze (4/5)
  100. 22. Finding the Exit of a Maze (5/5)
  101. 23. Image Processing (1/3)
  102. 23. Image Processing (2/3)
  103. 23. Image Processing (3/3)
  104. 24. Huffman Compression (1/10)
  105. 24. Huffman Compression (2/10)
  106. 24. Huffman Compression (3/10)
  107. 24. Huffman Compression (4/10)
  108. 24. Huffman Compression (5/10)
  109. 24. Huffman Compression (6/10)
  110. 24. Huffman Compression (7/10)
  111. 24. Huffman Compression (8/10)
  112. 24. Huffman Compression (9/10)
  113. 24. Huffman Compression (10/10)
  114. A. Linux
  115. B. Version Control
  116. C. Integrated Development Environments (IDE) (1/3)
  117. C. Integrated Development Environments (IDE) (2/3)
  118. C. Integrated Development Environments (IDE) (3/3)
Huffman Compression 425
(a)
(b)
(c)
FIGURE 24.13: (a) The next command (the second bit in the eighth byte) is 0. This will
create a common parent for the first two tree nodes. (b) The next command (the third bit
in the eighth byte) is also 0. This will create a common parent for the first two tree nodes.
(c) The next command (the fourth bit in the eighth byte) is 1. This will create a tree node
to store the value g.
426 Intermediate C Programming
(a) (b)
FIGURE 24.14: The remaining commands are 0. Continue building the tree.
(a) (b)
FIGURE 24.15: Finish building the tree.
Huffman Compression 427
The following is the final version of the complete program, for both compression and
decompression.
// main . c1
#in clude " encode .h "2
#in clude " constant .h "3
#in clude < stdlib .h >4
#in clude < string .h >5
int main ( i n t argc , char ** argv )6
{7
// argv [1]: " e" encode8
// "d" decode9
// argv [2]: name of input file10
// argv [3]: name of output file11
i f ( argc != 4)12
{13
return EXIT _FAILUR E ;14
}15
i f ( strcmp ( argv [1] , " e") == 0)16
{17
encode ( argv [2] , argv [3]) ;18
}19
i f ( strcmp ( argv [1] , " d") == 0)20
{21
decode ( argv [2] , argv [3]) ;22
}23
return EXIT _SUCCES S ;24
}25
// encode . h1
#i f n d e f ENCODE_H2
#d ef in e ENCODE_H3
// encode the input ( text ) file4
// save the result in the output ( binary ) file5
// return 0 if cannot read from file or write to file6
// return 1 if success7
int encode ( char * infile , char * outfile );8
// decode the input ( binary ) file9
// save the result in the output ( text ) file10
// return 0 if cannot read from file or write to file11
// return 1 if success12
int decode ( char * infile , char * outfile );13
#e ndif14
// encode . c1
#in clude " encode .h "2
#in clude " constant .h "3
#in clude " freq .h "4
#in clude " list .h "5
#in clude " utility . h"6
#in clude < stdio .h >7
#in clude < strings .h >8
428 Intermediate C Programming
#in clude < stdlib .h >9
#in clude < values .h >10
#d ef in e ENCODE MODE 011
#d ef in e DECODE MODE 112
void prin tFre q uenc yLate x ( CharFreq * freq u encies );13
void buil dCod e Book Helpe r ( TreeNode * tn , i n t * * codebook ,14
int * row , in t col )15
{16
i f ( tn == NULL )17
{18
return ;19
}20
// is it a leaf node ?21
TreeNode * lc = tn -> left ;22
TreeNode * rc = tn -> right ;23
i f (( lc == NULL ) && ( rc == NULL ) )24
{25
// finish one code26
codebook [* row ][0] = tn -> value ;27
(* row ) ++;28
return ;29
}30
i f ( lc != NULL )31
{32
// populat e this column of the entire s u b t r e e33
int numRow = Tree_le af ( lc );34
int ind ;35
for ( ind = * row ; ind < (* row ) + numRow ; ind ++)36
{37
codebook [ ind ][ col ] = 0;38
}39
buil dCod e Book Helpe r ( lc , codebook , row , col + 1) ;40
}41
i f ( rc != NULL )42
{43
int numRow = Tree_le af ( rc );44
int ind ;45
for ( ind = * row ; ind < (* row ) + numRow ; ind ++)46
{47
codebook [ ind ][ col ] = 1;48
}49
buil dCod e Book Helpe r ( rc , codebook , row , col + 1) ;50
}51
}52
void build CodeBoo k ( T reeNode * root , i n t * * cod e book )53
{54
int row = 0;55
// column start at 1 because [0] stores the char acter56
buil dCod e Book Helpe r ( root , codebook , & row , 1) ;57
}58
void print CodeBoo k ( i n t * * codebook , i n t numRow )59
Huffman Compression 429
{60
int row ;61
for ( row = 0; row < numRow ; row ++)62
{63
// print the characte r64
printf (" %c: " , code b oo k [ row ][0]) ;65
int col = 1;66
while ( codebook [ row ][ col ] != -1)67
{68
printf (" %d ", code b o ok [ row ][ col ]) ;69
col ++;70
}71
printf (" \n" );72
}73
}74
int compress ( char * infile , char * outfile ,75
int * * codebook , i n t * mapping )76
{77
FILE * infptr = fopen ( infile , "r ") ;78
i f ( infptr == NULL )79
{80
return 0;81
}82
FILE * outfptr = fopen ( outfile , "a "); // append83
i f ( outfptr == NULL )84
{85
fclose ( outfptr );86
return 0;87
}88
unsigned char whichbit = 0;89
unsigned char curbyte = 0;90
while (! feof ( infptr ) )91
{92
int onechar = fgetc ( infptr ) ;93
i f ( onechar != EOF )94
{95
int ind = mapping [ o n e c h a r ];96
int ind2 = 1;97
while ( codebook [ ind ][ ind2 ] != -1)98
{99
writeBit ( outfptr , ( codebook [ ind ][ ind2 ] == 1) ,100
& whichbit , & curbyte );101
// fprintf ( outfptr , "%d ", c o d ebook [ ind ][ ind2 ]) ;102
ind2 ++;103
}104
}105
}106
padZero ( outfptr , & whichbit , & curbyte );107
fclose ( infptr ) ;108
fclose ( outfptr );109
return 1;110