Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include <iostream>
  5. using namespace std;
  6. #define MAX 20
  7.  
  8.  
  9. char stack[MAX];
  10. int top =-1;
  11. char pop();
  12. void push(char item);
  13.  
  14. int prcd(char symbol)
  15. {
  16. switch (symbol)
  17. {
  18. case '+':
  19. case '-':
  20.  
  21. return 2;
  22.  
  23. case '*':
  24. case '/':
  25.  
  26. return 4;
  27.  
  28. case '^':
  29. case '$':
  30.  
  31. return 6;
  32.  
  33. case '(':
  34. case ')':
  35. case '#':
  36.  
  37. return 1;
  38. }
  39.  
  40. }
  41. int isoperator(char symbol)
  42. {
  43. switch (symbol)
  44. {
  45. case '*':
  46. case '/':
  47. case '^':
  48. case '$':
  49. case '(':
  50. case ')':
  51. case '#':
  52. return 1;
  53. default :
  54. return 0;
  55.  
  56. }
  57. }
  58. void convertip (char infix [],char prefix[])
  59. {
  60. int i, symbol,j=0;
  61. char test[MAX];
  62.  
  63. infix =strrev(infix);
  64. stack[++top]='#';
  65.  
  66. for(i=0;i<strlen(infix);i++)
  67. {
  68. symbol=infix[i];
  69.  
  70. if(isoperator(symbol)==0)
  71. {
  72. prefix[j]=symbol;
  73. j++;
  74. }
  75. else if(symbol==')')
  76. {
  77. push(symbol);
  78. }
  79. else if(symbol=='(')
  80. {
  81. while(stack[top]!=')')
  82. {
  83. prefix[j]=pop();
  84. j++;
  85. }
  86. pop();
  87. }
  88. else if(prcd(symbol)>prcd(stack[top]))
  89. {
  90. push(symbol);
  91. }
  92. else
  93. {
  94. while(prcd(symbol)<=prcd(stack[top]))
  95. {
  96. prefix[j]=pop();
  97. j++;
  98. }
  99. push(symbol);
  100. }
  101. }
  102.  
  103.  
  104. while(stack[top]!='#')
  105. {
  106. prefix[i]=pop();
  107. j++;
  108. }
  109. prefix[j]='\0';
  110. prefix=strrev(prefix);
  111. }
  112. int main()
  113. {
  114. char infix[20],prefix[20];
  115. cout<<"enter the vaild infix string";
  116. gets(infix);
  117. convertip(infix,prefix);
  118. cout<<"the corrsponding prefix string is";
  119. puts(prefix);
  120. getch();
  121. return 0;
  122. }
  123. void push (char item)
  124. {
  125. top++;
  126. stack[top]=item;
  127. }
  128. char pop()
  129. {
  130. char a;
  131. a=stack[top];
  132. top--;
  133. return a;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement