DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Mathematics In A Stack (http://www.daniweb.com/forums/thread19090.html)

alsoagirl Feb 22nd, 2005 11:13 pm
Mathematics In A Stack
 
I have read in problems "INFIX" AND CHANGED THEM TO "POSTFIX" or RPN notation, however i need to evaluate the problems and i can't get this to work. I am not sure where to go from the infix to postifix i have. Please help. My Stack.h is just a basic Stack defined. I am not sure what to do next. Any ideas?

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <stdlib.h>
#include <cstring>

using namespace std;
#include "Stack.h"

struct temp1
{
        char line[30];
};

string RPN(string exp);

void main()
{
        string exp;
        int count= 0;
        int result;
        temp1 temp[128];


        ifstream File;
        File.open("a2.txt");
        if (!File.is_open())
        {
                cout << "File not found!!!!";
                exit(1);
        }

        while(File.peek() != EOF)
        {
        cout << "\nThe Infix Expression is: \n" << endl;
        File.getline(temp[count].line, 30);
        cout << temp[count].line << endl;
        for (;;)
        {
                cout << "\nThe Postfix Expression is: \n";
                exp = temp[count].line;
                cout << RPN(exp) << endl;
                break;
               
        }
       
                cout << "\nThe Evaluated Expression is:\n";
                cout << RPN(exp) << endl;

                cout << "\nThe Answer is:\n" << endl;
        //        result =  -exp;
        //        cout << result << "\n";

        count++;
        }
}


string RPN(string exp)
{
        char token,
                topToken;
        Stack opStack;
        string RPNexp;
        const string BLANK = " ";

        for (int i= 0; i < exp.length(); i++)
        {
                token = exp[i];
                switch(token)
                {
                case ' ' : break;

                case '(' : opStack.push(token);
                                  break;

                case ')' : for (;;)
                                  {
                                          assert (!opStack.empty());
                                          topToken = opStack.top();
                                          opStack.pop();
                                          if (topToken == '(') break;

                                          RPNexp.append(BLANK + topToken);
                                  }       
                                        break;

                case '+' : case '-' :
                case '*' : case '/' :
                                        for (;;)
                                        {
                                                if (opStack.empty() ||
                                                        opStack.top() == '(' ||
                                                        (token == '*' || token == '/') &&
                                                        (opStack.top() == '+' || opStack.top() == '-')
                                                        )

                                        {
                                                opStack.push(token);
                                                break;
                                        }
                                        else
                                        {
                                        topToken = opStack.top();
                                        opStack.pop();
                                        RPNexp.append(BLANK + topToken);
                                        }
                                }
                                        break;

                        default : RPNexp.append(BLANK + token);
                }
        }

        for(;;)
        {
                if (opStack.empty()) break;
                topToken = opStack.top();
                opStack.pop();
                if (topToken != '(')
                {
                        RPNexp.append(BLANK + topToken);
                }
                else
                {
                        cout << " Error in Infix Expressions\n";
                        break;
                }
        }
        return RPNexp;

}

void Stack::push(const StackElement & value)
{
        if (myTop < STACK_CAPACITY - 1)
        {
                ++myTop;
                myArray[myTop] = value;
        }
}

void Stack::display(ostream & out) const
{
        for (int i = myTop; i >= 0; i--)
                out << myArray[i] << endl;
}

StackElement Stack::top() const
{
        if (myTop >= 0)
                return myArray[myTop];
        cout << "--- Stack is Empty----\n";
}

void Stack::pop()
{
        if (myTop >= 0)
                myTop--;
        else
                cout << "Stack is empty can't remove a value\n";
}

mcldev Feb 22nd, 2005 11:54 pm
Re: Mathematics In A Stack
 
Take a look at http://www.calculator.org/rpn.html, I think this will help you fill in the gaps. Good luck!

Khishin Feb 23rd, 2005 4:21 pm
Re: Mathematics In A Stack
 
Also, next time you post code use the [code] tag to make it easier to read and get rid of the smiles.

alsoagirl Feb 23rd, 2005 11:03 pm
Re: Mathematics In A Stack
 
okay so i tried the calculator i know how it works, push's pops etc don't know how to work in actually evaluating the expression. Please help :) Thanks for everything so far :)


All times are GMT -4. The time now is 8:09 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC