fork download
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #define MAX 50
  6. struct infix
  7. {
  8. char target[MAX] ;
  9. char stack[MAX] ;
  10. char *s, *t ;
  11. int top, l ;
  12. } ;
  13.  
  14. void initinfix ( struct infix * ) ;
  15. void setexpr ( struct infix *, char * ) ;
  16. void push ( struct infix *, char ) ;
  17. char pop ( struct infix * ) ;
  18. void convert ( struct infix * ) ;
  19. int priority ( char c ) ;
  20. void show ( struct infix ) ;
  21.  
  22. void main( )
  23. {
  24. struct infix q ;
  25. char expr[MAX] ;
  26. clrscr( ) ;
  27. initinfix ( &q ) ;
  28. printf ( "\nEnter an expression in infix form: " ) ;
  29. gets ( expr ) ;
  30. setexpr ( &q, expr ) ;
  31. convert ( &q ) ;
  32. printf ( "The Prefix expression is: " ) ;
  33. show ( q ) ;
  34. getch( ) ;
  35. }
  36. /* initializes elements of structure variable */
  37. void initinfix ( struct infix *pq )
  38. {
  39. pq -> top = -1 ;
  40. strcpy ( pq -> target, "" ) ;
  41. strcpy ( pq -> stack, "" ) ;
  42. pq -> l = 0 ;
  43. }
  44. /* reverses the given expression */
  45. void setexpr ( struct infix *pq, char *str )
  46. {
  47. pq -> s = str ;
  48. strrev ( pq -> s ) ;
  49. pq -> l = strlen ( pq -> s ) ;
  50. *( pq -> target + pq -> l ) = '\0' ;
  51. pq -> t = pq -> target + ( pq -> l - 1 ) ;
  52. }
  53. /* adds operator to the stack */
  54. void push ( struct infix *pq, char c )
  55. {
  56. if ( pq -> top == MAX - 1 )
  57. printf ( "\nStack is full.\n" ) ;
  58. else
  59. {
  60. pq -> top++ ;
  61. pq -> stack[pq -> top] = c ;
  62. }
  63. }
  64. /* pops an operator from the stack */
  65. char pop ( struct infix *pq )
  66. {
  67. if ( pq -> top == -1 )
  68. {
  69. printf ( "Stack is empty\n" ) ;
  70. return -1 ;
  71. }
  72. else
  73. {
  74. char item = pq -> stack[pq -> top] ;
  75. pq -> top-- ;
  76. return item ;
  77. }
  78. }
  79. /* converts the infix expr. to prefix form */
  80. void convert ( struct infix *pq )
  81. {
  82. char opr ;
  83. while ( *( pq -> s ) )
  84. {
  85. if ( *( pq -> s ) == ' ' || *( pq -> s ) == '\t' )
  86. {
  87. pq -> s++ ;
  88. continue ;
  89. }
  90. if ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
  91. {
  92. while ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
  93. {
  94. *( pq -> t ) = *( pq -> s ) ;
  95. pq -> s++ ;
  96. pq -> t-- ;
  97. }
  98. }
  99. if ( *( pq -> s ) == ')' )
  100. {
  101. push ( pq, *( pq -> s ) ) ;
  102. pq -> s++ ;
  103. }
  104. if ( *( pq -> s ) == '*' || *( pq -> s ) == '+' || *( pq -> s ) == '/' || *( pq -> s ) == '%' || *( pq -> s ) == '-' || *( pq -> s ) == '$' )
  105. {
  106. if ( pq -> top != -1 )
  107. {
  108. opr = pop ( pq ) ;
  109. while ( priority ( opr ) > priority ( *( pq -> s ) ) )
  110. {
  111. *( pq -> t ) = opr ;
  112. pq -> t-- ;
  113. opr = pop ( pq ) ;
  114. }
  115. push ( pq, opr ) ;
  116. push ( pq, *( pq -> s ) ) ;
  117. }
  118. else
  119. push ( pq, *( pq -> s ) ) ;
  120. pq -> s++ ;
  121. }
  122. if ( *( pq -> s ) == '(' )
  123. {
  124. opr = pop ( pq ) ;
  125. while ( opr != ')' )
  126. {
  127. *( pq -> t ) = opr ;
  128. pq -> t-- ;
  129. opr = pop ( pq ) ;
  130. }
  131. pq -> s++ ;
  132. }
  133. }
  134. while ( pq -> top != -1 )
  135. {
  136. opr = pop ( pq ) ;
  137. *( pq -> t ) = opr ;
  138. pq -> t-- ;
  139. }
  140. pq -> t++ ;
  141. }
  142. /* returns the priotity of the operator */
  143. int priority ( char c )
  144. {
  145. if ( c == '$' )
  146. return 3 ;
  147. if ( c == '*' || c == '/' || c == '%' )
  148. return 2 ;
  149. else
  150. {
  151. if ( c == '+' || c == '-' )
  152. return 1 ;
  153. else
  154. return 0 ;
  155. }
  156. }
  157. /* displays the prefix form of given expr. */
  158. void show ( struct infix pq )
  159. {
  160. while ( *( pq.t ) )
  161. {
  162. printf ( " %c", *( pq.t ) ) ;
  163. pq.t++ ;
  164. }
  165. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:2:19: fatal error: conio.h: No such file or directory
compilation terminated.
stdout
Standard output is empty