Sunday, January 22, 2012

Infix to prefix C++

/*The program prompts the user to enter an expression. The expression can
be as long as you want it to be because it is storing it as a string in "infix".
The program takes the users input and changes it to prefix notation. Help from
http://www.c4swimmers.esmartguy.com/in2pre.htm#. this site supplied the
algorythm. Also, http://www.cplusplus.com/forum/beginner/16722/
had some code for infix to "postfix" that i looked over and borrowed some ideas*/

#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;


/*This is a function that declares a temp variable and stores a number based on
the operator that the user inputs.If the operator is *,/,%, then temp is given
the number 2.That means it's priority is higher than +,- because tempgets a 1 when
one of those operators are entered.*/

int priority(char a)
{
    int temp;
 
    if (a == '*' || a == '/' || a == '%')
       temp = 2;
    else  if (a == '+' || a == '-')
       temp = 1;
    return temp;
}

//start
int main()
{
   //declare a string called "infix"

   string infix;

    /*Declare a sringstream called "output"We use "stringStream" this allows a string-based
   object to be treated as a stream. This way we can perform extraction or insertion
   operations from/to strings, which is especially useful to convert strings to numerical values
   and vice versa.*/

   stringstream output;

    /*creates two stacks. s1 is for current data and s2 is for storing data in the
   correct order for output*/

   stack<char> s1, s2;

   cout << "Infix to Prefix" << endl;

   //prompts the user to eneter a string without including parenthesis

   cout << "Enter an arithmetic expression with no perenthesis: " << endl;

    //gets the users input and stores it in infix as a string

   getline(cin, infix);

   /*loop backwards to reverse the infix string and read one charcter at a time.
   from right to left*/

   for(int i = infix.length() - 1; i >= 0; i--)
   {
     //check the input against +,-,/,*,%

     if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '%')
     {
         /*While the stack is not empty, and the top operator in the stack has greater
         priority than the operator it is currently reading, send it to the top of
         stack s1. Then, push the top of s1 to s2 to store it. After that, pop the top of s1.*/

         while(!s1.empty() && priority(s1.top()) > priority(infix[i]))
         {      
             output << s1.top();
             s2.push(s1.top());
             s1.pop();          
         }
         /*if stack s1 is empty, then add the current operator to infix string*/

         s1.push(infix[i]);
     }
     /*if the current input is not an operator, send it to the infix string, then
     push the infix to stack s2 to store it*/

     else
     {  
        output << infix[i];
        s2.push(infix[i]);
     }
   }
   /*While s1 stack is not empty send the output to the top of it. Then, push the top of stack s1
   to stack s2 for storage. we don't need the top of s1 stack antmore because we stored it in stack s2
   so pop it off.*/

   while(!s1.empty())
   {
       output << s1.top();
       s2.push(s1.top());
       s1.pop();
   }

   cout << "\nAnswer: ";

   /*While stack s2 is not empty, display what is in the top position, then pop it off so we can check to
   see if there are more charcters in the stack.*/

   while(!s2.empty())
   {
       cout << s2.top();
       s2.pop();
   }

   cout <<"\n\nPress enter to exit" << endl;
}

1 comment: