Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.07 KB | None | 0 0
  1. #include<iostream>
  2. #include<stack>
  3. #include "myStack.h"
  4. #include <string>
  5. #include "Node.h"
  6. #include <cstring>
  7. #include <sstream>
  8. #include <vector>
  9. //#include "valueStack.h"
  10. using namespace std;
  11.  
  12. class PostfixConverter{
  13. string infix;
  14. string postfix;
  15.  
  16. bool isOperand(char C){
  17. if(C >= '0' && C <= '9') return true;
  18. if(C >= 'a' && C <= 'z') return true;
  19. if(C >= 'A' && C <= 'Z') return true;
  20. return false;
  21. }
  22.  
  23. bool IsOperator(char C){
  24. if(C == '+' || C == '-' || C == '*' || C == '/')
  25. return true;
  26. return false;
  27. }
  28.  
  29. int getOperatorWeight(char c){
  30. int weight = -1;
  31. switch(c)
  32. {
  33. case '+': weight = 1;
  34. break;
  35. case '-': weight = 1;
  36. break;
  37. case '*': weight = 2;
  38. break;
  39. case '/': weight = 2;
  40. break;
  41.  
  42. }
  43. return weight;
  44. }
  45.  
  46. bool hasLowerOrEqualPrecedence(char op1, char op2){
  47. int op1Weight = getOperatorWeight(op1);
  48. int op2Weight = getOperatorWeight(op2);
  49. if(op1Weight <= op2Weight){
  50. return true;
  51. }
  52. else
  53. return false;
  54. }
  55.  
  56. bool isOpeningParanthesis(char c){
  57. if(c == '(' )
  58. return true;
  59. return false;
  60. }
  61.  
  62.  
  63. bool isClosingParanthesis(char c){
  64. if(c == ')')
  65. return true;
  66. return false;
  67. }
  68.  
  69. void displayPostfix(){
  70. cout<<"The equivalent postfix expression is: "<<postfix<<endl;
  71. }
  72. bool isMatchingParanthesis(char closing, char opening){
  73.  
  74. if(opening == '(' && closing == ')') return true;
  75.  
  76. return false;
  77. }
  78.  
  79.  
  80. //
  81. //
  82. //
  83. // string add(string last, string first)
  84. // {
  85. // // Before proceeding further, make sure length
  86. // // of str2 is larger.
  87. // if (last.length() > first.length())
  88. // swap(last, first);
  89. //
  90. // // Take an empty string for storing result
  91. // string str = "";
  92. //
  93. // // Calculate lenght of both string
  94. // int n1 = last.length(), n2 = first.length();
  95. // int diff = n2 - n1;
  96. //
  97. // // Initialy take carry zero
  98. // int carry = 0;
  99. //
  100. // // Traverse from end of both strings
  101. // for (int i=n1-1; i>=0; i--)
  102. // {
  103. // // Do school mathematics, compute sum of
  104. // // current digits and carry
  105. // int sum = ((last[i]-'0') +
  106. // (first[i+diff]-'0') +
  107. // carry);
  108. // str.push_back(sum%10 + '0');
  109. // carry = sum/10;
  110. // }
  111. //
  112. // // Add remaining digits of str2[]
  113. // for (int i=n2-n1-1; i>=0; i--)
  114. // {
  115. // int sum = ((first[i]-'0')+carry);
  116. // str.push_back(sum%10 + '0');
  117. // carry = sum/10;
  118. // }
  119. //
  120. // // Add remaining carry
  121. // if (carry)
  122. // str.push_back(carry+'0');
  123. //
  124. // // reverse resultant string
  125. // reverse(str.begin(), str.end());
  126. //
  127. // return str;
  128. // }
  129. //
  130. //
  131. //
  132. // // Returns true if str1 is smaller than str2,
  133. // // else false.
  134. // bool isSmaller(string str1, string str2)
  135. // {
  136. // // Calculate lengths of both string
  137. // int n1 = str1.length(), n2 = str2.length();
  138. //
  139. // if (n1 < n2)
  140. // return true;
  141. // if (n2 > n1)
  142. // return false;
  143. //
  144. // for (int i=0; i<n1; i++)
  145. // {
  146. // if (str1[i] < str2[i])
  147. // return true;
  148. // else if (str1[i] > str2[i])
  149. // return false;
  150. // }
  151. // return false;
  152. // }
  153. //
  154. // // Function for finding difference of larger numbers
  155. // string subtract(string str2, string str1)
  156. // {
  157. // // Before proceeding further, make sure str1
  158. // // is not smaller
  159. // bool isFirstSmall=false;
  160. // if (isSmaller(str1, str2)){
  161. // swap(str1, str2);
  162. // isFirstSmall=true;
  163. //
  164. // }
  165. //
  166. // // Take an empty string for storing result
  167. // string str = "";
  168. //
  169. // // Calculate lengths of both string
  170. // int n1 = str1.length(), n2 = str2.length();
  171. // int diff = n1 - n2;
  172. //
  173. // // Initially take carry zero
  174. // int carry = 0;
  175. //
  176. // // Traverse from end of both strings
  177. // for (int i=n2-1; i>=0; i--)
  178. // {
  179. // // Do school mathematics, compute difference of
  180. // // current digits and carry
  181. // int sub = ((str1[i+diff]-'0') -
  182. // (str2[i]-'0') -
  183. // carry);
  184. // if (sub < 0)
  185. // {
  186. // sub = sub+10;
  187. // carry = 1;
  188. // }
  189. // else
  190. // carry = 0;
  191. //
  192. // str.push_back(sub + '0');
  193. // }
  194. //
  195. // // subtract remaining digits of str1[]
  196. // for (int i=n1-n2-1; i>=0; i--)
  197. // {
  198. // if (str1[i]=='0' && carry)
  199. // {
  200. // str.push_back('9');
  201. // continue;
  202. // }
  203. // int sub = ((str1[i]-'0') - carry);
  204. // if (i>0 || sub>0) // remove preceding 0's
  205. // str.push_back(sub+'0');
  206. // carry = 0;
  207. //
  208. // }
  209. //
  210. // // reverse resultant string
  211. // reverse(str.begin(), str.end());
  212. //
  213. // return "-"+str;
  214. // }
  215. //
  216. // // Multiplies str1 and str2, and prints result.
  217. // string multiply(string num1, string num2)
  218. // {
  219. // int n1 = num1.size();
  220. // int n2 = num2.size();
  221. // if (n1 == 0 || n2 == 0)
  222. // return "0";
  223. //
  224. // // will keep the result number in vector
  225. // // in reverse order
  226. // vector<int> result(n1 + n2, 0);
  227. //
  228. // // Below two indexes are used to find positions
  229. // // in result.
  230. // int i_n1 = 0;
  231. // int i_n2 = 0;
  232. //
  233. // // Go from right to left in num1
  234. // for (int i=n1-1; i>=0; i--)
  235. // {
  236. // int carry = 0;
  237. // int n1 = num1[i] - '0';
  238. //
  239. // // To shift position to left after every
  240. // // multiplication of a digit in num2
  241. // i_n2 = 0;
  242. //
  243. // // Go from right to left in num2
  244. // for (int j=n2-1; j>=0; j--)
  245. // {
  246. // // Take current digit of second number
  247. // int n2 = num2[j] - '0';
  248. //
  249. // // Multiply with current digit of first number
  250. // // and add result to previously stored result
  251. // // at current position.
  252. // int sum = n1*n2 + result[i_n1 + i_n2] + carry;
  253. //
  254. // // Carry for next iteration
  255. // carry = sum/10;
  256. //
  257. // // Store result
  258. // result[i_n1 + i_n2] = sum % 10;
  259. //
  260. // i_n2++;
  261. // }
  262. //
  263. // // store carry in next cell
  264. // if (carry > 0)
  265. // result[i_n1 + i_n2] += carry;
  266. //
  267. // // To shift position to left after every
  268. // // multiplication of a digit in num1.
  269. // i_n1++;
  270. // }
  271. //
  272. // // ignore '0's from the right
  273. // int i = result.size() - 1;
  274. // while (i>=0 && result[i] == 0)
  275. // i--;
  276. //
  277. // // If all were '0's - means either both or
  278. // // one of num1 or num2 were '0'
  279. // if (i == -1)
  280. // return "0";
  281. //
  282. // // generate the result string
  283. // string s = "";
  284. // while (i >= 0)
  285. // s += std::to_string(result[i--]);
  286. //
  287. // return s;
  288. // }
  289.  
  290.  
  291. string trim(string a) // for removing leading 0s
  292. {
  293. string res="";
  294. int i=0;
  295.  
  296. while(a[i]=='0')
  297. i++;
  298.  
  299. for(;i<a.size();i++)
  300. res+=a[i];
  301.  
  302. return res;
  303. }
  304.  
  305. string convertInt(int number)
  306. {
  307. stringstream ss;//create a stringstream
  308. ss << number;//add number to the stream
  309. return ss.str();//return a string with the contents of the stream
  310. }
  311.  
  312. string add(string last,string first){
  313. int a= stoi(trim(first));
  314. int b= stoi(trim(last));
  315.  
  316. int result=a+b;
  317. return convertInt(result);
  318.  
  319. }
  320.  
  321. string subtract(string last,string first){
  322. int a= stoi(trim(first));
  323. int b= stoi(trim(last));
  324.  
  325. int result=a-b;
  326. return convertInt(result);
  327.  
  328. }
  329.  
  330. string multiply(string last,string first){
  331. int a= stoi(trim(first));
  332. int b= stoi(trim(last));
  333.  
  334. int result=a*b;
  335. return convertInt(result);
  336.  
  337. }
  338.  
  339. string divide(string last,string first){
  340. int a= stoi(trim(first));
  341. int b= stoi(trim(last));
  342.  
  343. int result=a/b;
  344. return convertInt(result);
  345.  
  346. }
  347.  
  348.  
  349.  
  350.  
  351. bool isParanthesisBalanced(){
  352. myStack<char> s;
  353.  
  354. for(int i=0;i<infix.length();++i){
  355.  
  356. if(isOpeningParanthesis(infix[i]))
  357. s.push(infix[i]);
  358. else if(isClosingParanthesis(infix[i])){
  359. if(s.isEmpty() || !isMatchingParanthesis(infix[i],s.peek())){
  360. return false;}
  361. s.pop();
  362. }
  363. }
  364. if(s.isEmpty())
  365. return true;
  366. return false;
  367. }
  368.  
  369. public:
  370. PostfixConverter(string infix){
  371. this->infix = infix;
  372. postfix = "";
  373. }
  374.  
  375. void convert(){
  376. if(!isParanthesisBalanced()){
  377. cout<<"Typo! Paranthesis is not balanced in the infix expression"<<endl;
  378. return;
  379. }
  380.  
  381.  
  382.  
  383. //stack<char> ops;
  384. myStack<char> ops;
  385. myStack<string> valueStack;
  386. myStack<char> operationStack;
  387.  
  388. string operand;
  389.  
  390. for(int i=0;i<infix.length();++i){
  391. // cout<<valueStack.peek()<<"\t"<<operationStack.peek()<<endl;
  392.  
  393.  
  394. if (!operationStack.isEmpty()) {
  395. if (operationStack.peek()=='+') {
  396. valueStack.push(add(valueStack.pop()->val, valueStack.pop()->val));
  397. }
  398. else if (operationStack.peek()=='-'){
  399. valueStack.push(subtract(valueStack.pop()->val, valueStack.pop()->val));
  400. }
  401. else if (operationStack.peek()=='*'){
  402. valueStack.push(multiply(valueStack.pop()->val, valueStack.pop()->val));
  403. }
  404.  
  405. operationStack.pop();
  406. }
  407. if(IsOperator(infix[i])){
  408.  
  409. // if (operand!="") {
  410. // postfix+=operand;
  411. // valueStack.push(operand);
  412. //
  413. // operand="";
  414. //
  415. // }
  416.  
  417. while(!ops.isEmpty() && !isOpeningParanthesis(ops.peek()) && hasLowerOrEqualPrecedence(infix[i],ops.peek())){
  418. postfix+= ops.peek();
  419. operationStack.push(ops.peek());
  420. ops.pop();
  421. }
  422. ops.push(infix[i]);
  423. }
  424. else if(isOperand(infix[i])){
  425. postfix+=infix[i];
  426. valueStack.push(infix.substr(i,1));
  427.  
  428. //operand+=infix.substr(i,1);
  429.  
  430. }
  431. else if(isOpeningParanthesis(infix[i])){
  432.  
  433. // if (operand!="") {
  434. // postfix+=operand;
  435. // valueStack.push(operand);
  436. //
  437. // operand="";
  438. //
  439. // }
  440. if (IsOperator(infix[i+1])) {
  441. operand+=infix[i+1];
  442.  
  443. }
  444.  
  445. ops.push(infix[i]);
  446. i++;
  447. }
  448.  
  449. else if(isClosingParanthesis(infix[i])){
  450. // if (operand!="") {
  451. // postfix+=operand;
  452. // valueStack.push(operand);
  453. //
  454. // operand="";
  455. //
  456. // }
  457. //
  458. while(!isOpeningParanthesis(ops.peek())){
  459. postfix+= ops.peek();
  460. operationStack.push(ops.peek());
  461. ops.pop();
  462. }
  463. ops.pop();
  464. }
  465. }
  466.  
  467. // if (operand!="") {
  468. // postfix+=operand;
  469. // valueStack.push(operand);
  470. // operand="";
  471. // }
  472.  
  473. while(!ops.isEmpty()){
  474. if (!operationStack.isEmpty()) {
  475. if (operationStack.peek()=='+') {
  476. valueStack.push(add(valueStack.pop()->val, valueStack.pop()->val));
  477. }
  478. else if (operationStack.peek()=='-'){
  479. valueStack.push(subtract(valueStack.pop()->val, valueStack.pop()->val));
  480. }
  481. else if (operationStack.peek()=='*'){
  482. valueStack.push(multiply(valueStack.pop()->val, valueStack.pop()->val));
  483. }
  484. operationStack.pop();
  485. }
  486.  
  487.  
  488. postfix+=ops.peek();
  489. operationStack.push(ops.peek());
  490. ops.pop();
  491.  
  492.  
  493. }
  494.  
  495. if (!operationStack.isEmpty()) {
  496. if (operationStack.peek()=='+') {
  497. valueStack.push(add(valueStack.pop()->val, valueStack.pop()->val));
  498. }
  499. else if (operationStack.peek()=='-'){
  500. valueStack.push(subtract(valueStack.pop()->val, valueStack.pop()->val));
  501. }
  502. else if (operationStack.peek()=='*'){
  503. valueStack.push(multiply(valueStack.pop()->val, valueStack.pop()->val));
  504. }
  505. operationStack.pop();
  506. }
  507.  
  508.  
  509.  
  510. cout<<endl<<"Final Value Stack "<<valueStack.pop()->val<<endl;
  511. // cout<<endl<<"Operation Stack"<<endl;
  512. //
  513. // while (!operationStack.isEmpty()) {
  514. // cout<<operationStack.pop()->val<<"\t";
  515. // }
  516. //
  517. // cout<<endl<<"Value Stack"<<endl;
  518. //
  519. // while (!valueStack.isEmpty()) {
  520. // cout<<valueStack.pop()->val<<"\t";
  521. // }
  522.  
  523.  
  524. displayPostfix();
  525. }
  526. };
  527.  
  528. int main(){
  529. string expression;
  530. cout<<"Enter the infix expression: ";
  531. getline(cin,expression);
  532. PostfixConverter* postfixConverter = new PostfixConverter(expression);
  533. postfixConverter->convert();
  534. return 0;
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement