daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //converts infix expression to postfix expression
  2. void ArithmeticExpression::convertToPostfix(char *const inFix, char *const postFix)
  3. {
  4.  
  5.  //create a stack2 object named cow
  6.  Stack2<char> cow;
  7.  cout<<postFix;
  8.  
  9.  char thing = '(';
  10.  
  11.  //push a left parenthesis onto the stack
  12.  cow.push(thing);
  13.  
  14.  
  15. //append a right parenthesis to the end of inFix array
  16. strcat(inFix, ")");
  17.  
  18. int i = 0;//declare an int that will control posFix position
  19.  
  20.  
  21. //if the stack is not empty
  22. if (!cow.isEmpty())
  23. {
  24.  
  25.     //loop to run until the last character in inFix array
  26.     for (int x = 0; inFix[x]!= ''; x++ )
  27.     {
  28.  
  29.         //if the inFix element is a digit
  30.         if (isdigit(inFix[x]))
  31.         {
  32.             postFix[i]=inFix[x];//it is assigned to the next element in postFix array
  33.             i++;//move on to next element in postFix
  34.  
  35.         }
  36.  
  37.         //if the inFix element is a left parenthesis
  38.         else if (inFix[x]=='(')
  39.         {
  40.             cow.push(inFix[x]);//push it unto the stack
  41.         }
  42.  
  43.         //if the inFix element is an operator
  44.         else if (isOperator(inFix[x]))
  45.         {
  46.  
  47.             char oper2 = inFix[x];//char variable holds inFix operator
  48.  
  49.             if (isOperator(cow.stackTop()))//if the top node in the stack is an     operator
  50.             {
  51.             while (isOperator(cow.stackTop()))//and while the top node in the stack is an operator
  52.             {
  53.  
  54.                 char oper1 = cow.stackTop();//char variable holds node operator
  55.                 if(precedence( oper1, oper2))//if the node operator has higher presedence than node operator
  56.                 {
  57.                     postFix[i] = cow.pop();//we pop such operator and insert it in postFix array's next element
  58.                     cow.push(inFix[x]);//and push inFix operator unto the stack
  59.                     i++;//move to the next element in posFix
  60.                 }
  61.  
  62.             }
  63.         }
  64.             //if the top node is not an operator
  65.             //we push the current inFix operator unto the top of the stack
  66.             else
  67.             cow.push(inFix[x]);
  68.  
  69.         }
  70.  
  71.         //if the inFix element is a right parenthesis
  72.         else if (inFix[x]==')')
  73.         {
  74.             //we pop everything in the stack and insert it in postFix
  75.             //until we arrive at a left paranthesis
  76.             while (cow.stackTop()!='(')
  77.             {
  78.                 postFix[i] = cow.pop();
  79.                 i++;
  80.             }
  81.             //we then pop and discard left parenthesis
  82.             cow.pop();
  83.         }
  84.  
  85.  
  86.     }
  87.  
  88.      postFix[i]='';
  89.  
  90.     //print !!postFix array!! (not stack)
  91.     print();//code for this is just cout<<postFix;
  92.  
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top