Here are the requirements of the program.
Computer Science 245 - Programming Assignment #3
Due Date: Mar 22, 2009 (11:55 PM)
75 pts.
Reverse Polish Notation
Topic: Stack implemented as a linked list, Evaluating Postfix Expressions
Input file: postfix.dat
Output file: postfix.out
Application File: Postfix.cpp
Header File : Stack.h
Implementation File: Stack.cpp
The C++ application (postfix.cpp) will simply evaluate the postfix expressions found in postfix.dat and will write the answer to each to postfix.out. The application must use a stack class in the solution process. The stack class must be implemented using a linked list with dynamic memory allocation and pointer variables.
Within the file postfix.dat, a postfix expression will be on every line. The file will terminate with the dollar sign ($). A postfix expression will not exceed one line of text (80 characters). Tokens (operand or operator) in the expression will be separated by one space. There will be no leading spaces in front of an expression. The last token in an expression will terminate with a carriage return (\n).
You must be careful when you evaluate each postfix expression in postfix.dat. Not every expression is a correctly formed postfix expression. Also, insure that a mathematical operation can be performed correctly (e.g. dont allow divison by zero).
Below is given a sample input and sample output. You may assume that there will not be more than 15 postfix expressions to be evaluated in the actual test file.
Please turn in all three files using EASEL.
Sample I/O
postfix.dat
15 -33 +
7 2 + 3 15 * 5 / - 7 - 8 +
-8 4 + 24 13 2 * *
15 4 4 - /
$
postfix.out
The answers to the problems in postfix.dat are...
1) -18
2) 1
3) incorrect postfix expression
4) error, tried to divide by zero
Here are my three files
Stack.h
typedef double StackType;
struct node
{
StackType data;
node *next;
};
class Stack
{
public:
Stack();
~Stack();
bool Push(StackType operand);
bool Pop(StackType &operand);
bool Empty();
bool Full();
private:
node *top;
};
Stack.cpp
#include "Stack.h"
#include<iostream>
Stack::Stack()
{
top = NULL;
}
bool Stack::Full()
{
node *temp;
temp = new node;
if(temp == NULL)
return true;
else
return false;
}
bool Stack::Push(StackType operand)
{
if( !Full() )
{
node *temp;
temp=new node;
if(temp==NULL)
return false;
else
{
temp->data = operand;
temp->next = top;
top=temp;
return true;
}
}
return false;
}
bool Stack::Empty()
{
if(top == NULL)
return true;
else
return false;
}
bool Stack::Pop(StackType &operand)
{
if( !Empty() )
{
node *temp;
temp = top;
operand = temp->data;
top = top->next;
delete temp;
return true;
}
return false;
}
Stack::~Stack()
{
if(top==NULL)
return;
node *tmp;
while(top!=NULL)
{
tmp=top;
top=top->next;
delete tmp;
}
}
Postfix.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<cctype>
#include"Stack.h"
using namespace std;
const string EXIT_CHAR = "$";
Stack operandStack;
void ParseToken(string &postfix, string & token)
{
token.assign(postfix,0, postfix.find(' '));
postfix.erase(0, postfix.find(' ')+1);
}
void PushOrPop(StackType &num1, StackType &num2, string token)
{
StackType num_token = atoi(token.c_str());
if(num_token != 0)
{
operandStack.Push(num_token);
}
else
{
operandStack.Pop(num1);
operandStack.Pop(num2);
}
}
void PerformOperation(StackType num1, StackType num2, string token)
{
StackType result;
if(token =="+")
{
result = num2 + num1;
operandStack.Push(result);
}
else if(token =="-")
{
result = num2 - num1;
operandStack.Push(result);
}
else if(token =="/")
{
result = num2/num1;
operandStack.Push(result);
}
else if(token == "*")
{
result = num2 * num1;
operandStack.Push(result);
}
}
void WriteToFile(ofstream & fout )
{
StackType result;
operandStack.Pop(result);
fout << result <<endl;
}
void EvaluateExpression(string postfix)
{
string token;
StackType num1, num2;
while( !postfix.empty() )
{
ParseToken(postfix, token);
PushOrPop(num1, num2, token);
PerformOperation(num1, num2, token);
if(postfix.size()== 1 && postfix == token) //Erase postfix when last character is equal to token
postfix.erase(0, postfix.size());
}
}
void main()
{
string postfix;
ifstream fin;
ofstream fout;
fin.open("postfix.dat");
fout.open("postfix.out");
do
{
getline(fin, postfix);
EvaluateExpression(postfix);
if(postfix != EXIT_CHAR) //Only write to file when postfix = EXIT_CHAR
WriteToFile(fout);
}while(postfix != EXIT_CHAR);
fin.close();
fout.close();
}

New Topic/Question
Reply




MultiQuote





|