Advertisement
Abir_Ahsan

Compiler

Jun 13th, 2020
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #include <fstream>
  3. #include<stdio.h>
  4. #include<string>
  5. using namespace std;
  6.  
  7. string result1;
  8. string result2;
  9. string prefixConverter(string st);
  10. string postfixConverter(string st);
  11. void consoleInput();
  12. void fileInput();
  13. int setPrecedence(char ch);
  14.  
  15. int main()
  16. {
  17. printf(" Name : MD. Abir Ahsan \n");
  18. printf(" ID : 011 172 077 \n");
  19.  
  20. printf("\n$$~~~ Menu ~~~$$ ");
  21.  
  22. int choice;
  23.  
  24. for( ; ; )
  25. {
  26. printf("\n1. Console Input \n");
  27. printf("2. File Input \n");
  28. printf("3.Exit\n");
  29.  
  30. printf("enter your choice :");
  31. cin >> choice;
  32.  
  33. if (choice == 1)
  34. consoleInput();
  35.  
  36. else if (choice == 2)
  37. fileInput();
  38.  
  39. else if (choice == 3)
  40. exit(0);
  41.  
  42.  
  43. }
  44. return 0;
  45. }
  46.  
  47. void consoleInput()
  48. {
  49. string st;
  50. int choice2;
  51.  
  52. printf(" Enter infix expression: ");
  53. cin >> st;
  54.  
  55. printf("\n1. Convert to Prefix \n");
  56. printf("2. Convert to Postfix \n");
  57.  
  58. printf("Enter your choice :");
  59. cin >> choice2;
  60.  
  61. if (choice2 == 1)
  62. {
  63. cout << "Prefix expression : " << prefixConverter(st) << endl;
  64. result2.clear();
  65. result1.clear();
  66. }
  67. else if (choice2 == 2)
  68. {
  69. cout << "Postfix expression : " << postfixConverter(st) << endl;
  70. result2.clear();
  71. result1.clear();
  72. }
  73. }
  74.  
  75. void fileInput()
  76. {
  77. ifstream f_in;
  78.  
  79. int choice3;
  80. string loc, line;
  81. printf("\nEnter File location: ");
  82. cin >> loc;
  83.  
  84. f_in.open(loc.c_str());
  85.  
  86. if (f_in.is_open())
  87. {
  88. while (getline(f_in,line))
  89. {
  90. printf("\n1. Convert to Prefix \n");
  91. printf("2. Convert to Postfix \n");
  92.  
  93. printf("\nEnter your choice:");
  94. cin >> choice3;
  95.  
  96. if (choice3 == 1)
  97. {
  98. prefixConverter(line);
  99. }
  100. else if (choice3 == 2)
  101. {
  102. postfixConverter(line);
  103. }
  104.  
  105. }
  106. }
  107. else
  108. {
  109. std::cout << "Unable to open file" << std::endl << std::endl;
  110. }
  111.  
  112. f_in.close();
  113.  
  114. ofstream f_out;
  115. f_out.open(loc.c_str());
  116.  
  117. if (choice3==1)
  118. {
  119. f_out << result1 << endl;
  120. cout << "Output written in File." << endl;
  121. result1.clear();
  122. result2.clear();
  123. }
  124.  
  125. else if (choice3==2)
  126. {
  127. f_out << result2 << endl;
  128. cout << "Output written in File." << endl;
  129. result1.clear();
  130. result2.clear();
  131. }
  132. f_out.close();
  133. }
  134.  
  135. string prefixConverter(string st)
  136. {
  137. reverse(st.begin(), st.end());
  138.  
  139. for (int i=0;i<st.size();i++)
  140. {
  141. if (st[i] == '(')
  142. {
  143. st[i] = ')';
  144. i++;
  145. }
  146. else if (st[i] == ')')
  147. {
  148. st[i] = '(';
  149. i++;
  150. }
  151. }
  152. result1 = postfixConverter(st);
  153. reverse(result1.begin(), result1.end());
  154.  
  155. return result1;
  156. }
  157.  
  158. string postfixConverter(string st)
  159. {
  160. st = '(' + st + ')';
  161.  
  162. stack<char> stk1;
  163.  
  164. for (int i= 0; i < st.size(); i++)
  165. {
  166. if (isalpha(st[i]) || isdigit(st[i]))
  167. {
  168. result2 += st[i];
  169. }
  170. else if (st[i] == '(')
  171. {
  172. stk1.push('(');
  173. }
  174. else if (st[i] == ')')
  175. {
  176. while (stk1.top() != '(')
  177. {
  178. result2 += stk1.top();
  179. stk1.pop();
  180. }
  181. stk1.pop();
  182. }
  183. else
  184. {
  185. if (!isalpha(stk1.top()) && !isdigit(stk1.top()))
  186. {
  187. while ( setPrecedence(st[i]) <= setPrecedence((stk1.top())) )
  188. {
  189. result2+= stk1.top();
  190. stk1.pop();
  191. }
  192. stk1.push(st[i]);
  193. }
  194. }
  195. }
  196.  
  197. return result2;
  198. }
  199.  
  200. int setPrecedence(char ch)
  201. {
  202. if (ch == '+' || ch == '-')
  203. return 1;
  204. else if (ch == '*' || ch == '/')
  205. return 2;
  206. else if (ch == '^')
  207. return 3;
  208. else
  209. return -1;
  210. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement