Advertisement
darkhelmet125

StackType.cpp

Oct 30th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. /*Matt Short
  2. CPSC 131
  3. Project 5
  4. Purpose: using our stack template write a program that changes
  5. infix expressions to postfix expressions*/
  6. #include<iostream>
  7. #include<string>
  8. #include "StackType.h"
  9. using namespace std;
  10.  
  11. string GetInfix(string &infix);//gets infix expression and stores in a string
  12. void PrintExpressions(string infix, string postfix);//prints infix and postfix expressions
  13. int Precedence(char op);//function that checks the precedence of the operators
  14. string Postfix(string infix);//function to convert infix to postfix
  15.  
  16. int main()
  17. {
  18.     string infix;//string to hold infix
  19.     string postfix;//string to hold postfix
  20.     GetInfix(infix);//gets infix expression
  21.     postfix=Postfix(infix);//prints out postfix expression
  22.     try
  23.     {
  24.         PrintExpressions(infix, postfix);
  25.     }
  26.     catch(FullStack exceptionObject)
  27.     {
  28.         cerr<<"Fullstack exception thrown. Stack is full."<<endl;
  29.         exit(2);
  30.     }
  31.     catch(EmptyStack exceptionObject)
  32.     {
  33.         cerr<<"Emptystack exception thrown. Stack is empty."<<endl;
  34.         exit(2);
  35.     }
  36.     return 0;
  37. }
  38. string GetInfix(string &infix)
  39. {
  40.     cout<<"Please enter an infix expression: ";//prompts user to input an infix expression
  41.     getline(cin, infix);//gets infix expression from keyboard
  42.     return infix;//returns infix string
  43. }
  44. string Postfix(string infix)
  45. {
  46.     StackType<char> stack;//creates a variable stack of type char via StackType
  47.     string postfix;//string to hold postfix expression
  48.     int i=0;//counter variable
  49.     char token;//variable to hold an item from infix
  50.     char popped;//variable that holds the most recently popped operator
  51.     bool done;//bool that is true when it's done
  52.     token = infix[0];//initiates token to the first char of infix
  53.         while(token!='=')//decides where to place char from infix string
  54.         {
  55.                 if(token>='0' && token <='9')//if char is a number it gets put directly in postfix string
  56.                     postfix+=token;//places char on postfix
  57.                 if(token==')')//checks if char is )
  58.                 {
  59.                     popped=stack.Top();//popped=top operator on stack
  60.                     stack.Pop();//pops the top operator from stack
  61.                     while(popped!='(')//if popped isn't ( then
  62.                     {
  63.                         postfix+=popped;//operator added to postfix
  64.                         popped=stack.Top();//popped=top operator on stack
  65.                         stack.Pop();//operator popped from stack
  66.                     }
  67.                 }
  68.                 if(token=='+' || token=='-' || token=='*' || token=='/' || token=='(' || token=='=')//checks if its an operator
  69.                 {
  70.                     if(token!='(')//checks if token isn't (
  71.                     {
  72.                         done=false;//done initiated to false
  73.                         while(!stack.IsEmpty() && !done)//while stack isn't empty and done is true
  74.                             if(Precedence(stack.Top())>=Precedence(token))//compares precedence of top of stack and token
  75.                             {
  76.                                 postfix+=stack.Top();//if top of stack is >= token top is
  77.                                 stack.Pop();//operator popped
  78.                             }else
  79.                                 done=true;//done set to true and jumps out of loop
  80.                     }
  81.                     stack.Push(token);//token is pushed onto postfix
  82.                 }
  83.  
  84.  
  85.                 i++;//i increased by 1
  86.                 token=infix[i];//token set to next char in infix
  87.  
  88.  
  89.         }
  90.         while(!stack.IsEmpty())//while stack isn't empty
  91.         {
  92.         postfix+=stack.Top();//top is added to postfix
  93.         stack.Pop();//top is popped from stack
  94.         }
  95.     return postfix;//returns the postfix expression
  96. }
  97. int Precedence(char op)
  98. {
  99.     switch(op)//switch statement to determine precedence of operators
  100.     {
  101.     case '/': case '*': return 2;//* and / have the highest precedence
  102.     case '+': case '-': return 1;//+ and - are below * and /
  103.     case '(': return 0;//( has the lowest precedence
  104.     default: return -1;
  105.     }
  106. }
  107. void PrintExpressions(string infix, string postfix)
  108. {
  109.     cout<<endl;
  110.     cout<<"Infix expression: "<<infix<<endl;//prints out the infix expression
  111.     cout<<"Postfix expression: "<<postfix<<endl;//prints out the postfix expression
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement