Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. bool isOperator(char symbol)          //definition of isOperator
  8. {
  9.     if( (symbol == '*') || (symbol == '+')  || (symbol == '/')  || (symbol == '-' ) )
  10.                         return true;
  11.     else
  12.                         return false;
  13. }
  14.  
  15. class Stack {
  16.  
  17.  public:
  18.    int MaxStack;
  19.    int EmptyStack;
  20.    char* items;
  21.    int top;
  22.  
  23.  
  24.    Stack(int);
  25.    ~Stack();
  26.    int TOP();
  27.    void push(char);
  28.    char pop();
  29.    int empty();
  30.    int full();
  31.  
  32. };
  33. Stack::Stack(int size) {
  34.    MaxStack = size;
  35.    EmptyStack = -1;
  36.    top = EmptyStack;
  37.    items = new char[MaxStack];
  38. }
  39.  
  40. Stack::~Stack() {delete[] items;}
  41. int Stack::TOP(){
  42. return items[top];
  43. }
  44. void Stack::push(char c) {
  45.    items[++top] = c;
  46. }
  47.  
  48. char Stack::pop() {
  49.    return items[top--];
  50. }
  51.  
  52. int Stack::full()  {
  53.    return top + 1 == MaxStack;
  54. }
  55.  
  56. int Stack::empty()  {
  57.    return top == EmptyStack;
  58. }
  59.  
  60.  
  61.  
  62. int main()
  63. {
  64.   cout <<"WELCOME TO PROJECT 4 PROGRAM"<<endl;
  65.  
  66.   // basic components
  67.   string line;
  68.   string outputLine;
  69.  
  70.   //stacks
  71.  
  72.   Stack stack(20);
  73.   char c;
  74.  
  75.  
  76.   ifstream myfile ("Prefix.in");
  77.   if (myfile.is_open())
  78.   {
  79.     while ( myfile.good() )
  80.     {
  81.       getline(myfile,line);
  82.       cout << line << endl;
  83.  
  84.     }
  85.   }
  86.  
  87.   else cout << "Unable to open file";
  88.  
  89. //flags, operator method
  90. while (line.length()!=0)    //while not end of blank line
  91.             {
  92.                    Stack flags(20);
  93.                    Stack operators(20);
  94.  
  95.             for(int i=0; i < line.size(); i ++)  //reiterate through ‘line’
  96.                         {
  97.                           char symbol = line [i];
  98.                           if( isOperator(symbol))    //if the symbol is an operator
  99.                             {
  100.                                 operators.push(symbol);    //push operator on stack
  101.                                  flags.push(0);  //push associated flag on stack
  102.                                   }
  103.                         if((symbol != ' ') && !isOperator(symbol))     //then it’s a operand
  104.                             {
  105.                               outputLine += symbol;                //append
  106.                              while(flags.TOP())       //while top flag is ON on stack
  107.                               {
  108.                                 outputLine += operators.TOP() ;  //append the associated op
  109.                                 operators.pop();   //remove operator from stack
  110.                                 flags.pop();     //remove flag from stack
  111.                               }
  112.                                 flags.pop();                 //set next flag ON
  113.                                 flags.push(1);
  114.                             }
  115.                         }//end of for
  116.  
  117.                         if(!operators.empty() || (!flags.empty()) )
  118.                         {
  119.                                     cout << "SOMETHING WENT WRONG. Prob incorrect input" << endl;
  120.                         }
  121.                         else
  122.                         {
  123.                                     cout << line << endl;
  124.                                     cout << outputLine << endl;
  125.                         }
  126.             } //end of while eof
  127.  
  128.  
  129.   ofstream outfile ("Postfix.out");
  130.   if (outfile.is_open())
  131.   {
  132.     outfile << outputLine << endl;
  133.     outfile << line << endl;
  134.  
  135.     myfile.close();
  136.   }
  137.   else cout << "Unable to open file";
  138.   myfile.close();
  139.   //outFile.close();
  140.   return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement