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";
}

Also, next time you post code use code tags to make it easier to read.

Edited 3 Years Ago by pyTony: out of date fixed

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 :)

This article has been dead for over six months. Start a new discussion instead.