whose values are given in a data file called: 375_stack_prog.txt.
there will not be more than 26 variables (a-z) and
there will not be more than 20 postfixes
use a stack of integers and the algorithm below.
algorithm for a given postfix expression
scan the postfix left to right char by char
if char is operand (var)
find its value and push on stack
if char is operator (+ - / *)
get the value from the top of the stack (say Value A) and pop the stack
again get the top of the stack (say Value
compute "B operator A" and push the result on stack
if there is a stack problem during any of the above operations
report "invalid postfix" and return
if there is 0 divide problem
report "0 divide problem" and return
if char is neither operand nor operator
report invalid variable and return
at the end of scanning the postfix,
if stack contains exactly one entry return this value
else report invalid postfix and return
end of algorithm
sample data file: (c:\temp\375_stack_prog3_data.txt)
the first line gives how many variables there are (say n variables)
the next n lines are the variables and their values separated by a space
the rest of the lines are the postfix expressions that you will evaluate.
I'm having some compiler errors in my code: I put //compiler error...by each line of code that is causing a compiler error.
And I attached the data.txt that is needed to test my program.
In 375StackProg3Data.txt:
5
a 4
d 9
z 17
y 0
x 2
ad+
ad+zy-x/*
xz*yd+ad-*+
ab+++
ad*yz*+ad*yz*-*
adz*+yx/-
zy/
abx^+
And the expected output for my program should look like this:
a =4
d =9
z =17
y =0
x =2
ad+ =13
ad+zy-x/* =104
xz*yd+ad-*+ =-11
ab+++ is invalid postfix ERROR
ad*yz*+ad*yz*-* = 1296
adz*+yx/- =157
zy/ zero divide ERROR
adx^+ is invalid postfix ERROR
#include <iostream>
#include <iomanip>
#include <stack>
#include <fstream>
#include <string>
using namespace std;
#define MAX 26 // Max Number of Variables and Postfixes
string Variables = "abcedfghijklmnopqrstuvwxyz"; //Possible Variable Names
int Values[26] = {0};
string Postfix;
int NumberofVariables;
int NumberofPostfixes;
int Index;
int ValueA;
int ValueB;
stack <int> STK;
int ReadFile();
int VariableToIndex(string Variables);
bool CheckForOperand(string Variables);
bool CheckForOperator(string Variables);
int ProcessThePostfix(string Postfix, int NumberofPostfixes);
int main ()
{
int CheckOperand;
int CheckOperator;
CheckOperand = CheckForOperand(Variables);
CheckOperator = CheckForOperator(Variables);
NumberofPostfixes = ReadFile();
for(int i = 0; i < NumberofPostfixes ; i++)
{
cout << NumberofPostfixes; //display the output of the postfixes.
if(CheckOperator = true)
{
ValueA = STK.top();
STK.pop();
ValueB = STK.top();
STK.pop();
}
if(CheckOperand = true)
{
STK.push(Values[i]);
ProcessThePostfix(Postfix, NumberofPostfixes);
}
cout << " = " << STK.top();
}
cin.ignore();
cin.get();
}
int ReadFile()
{
int Count = 0;
int i = 0;
int Temp;
ifstream Data;
Data.open("C:375StackProg3Data.txt");
Data >> Temp;
for(int j = 0; j < Temp ; i++)
{
Data >> Variables >> Values; //compiler error
Variables = Variables + Values;//compiler error
Values[j] = Values;//compiler error
cout << Variables[j]<< " = " << Values[j] << endl;
Index = VariableToIndex(Variables);
Values[Temp] = Values;//compiler error
}
while(Data >> Postfix[i])
{
i++;
}
Count = i;
Data.close();
return Count;
if(!Data)
{
cout << "ERROR: Can't find 375StackProg3Data.txt" << endl;
exit(0);
}
}
int VariableToIndex(string Variables)
{
if((Index >= 0) && (Index < Variables.length()))
{
return Index;
}
else
{
cout << "ILLEGAL Variable" << endl;
return -1;
}
}
int IndexToVariable(int Index)
{
Index = Variables.find(Variables);
if((Index >= 0) && (Index < Variables.substr(Index, 1)) )//compiler error
{
return Index;
}
else
{
cout << "ILLEGAL Variable" << endl;
return -1;
}
}
bool CheckForOperand(string Variables)
{
if((Variables >="a") && (Variables <="z"))
{
return true;
}
else
{
cout << " INVALID Postfix Error" << endl;
return false;
}
}
bool CheckForOperator(string Variables)
{
if(Variables = "+" || Variables = "-" || Variables = "*" || Variables = "/" )//compiler error
{
return true;
}
else
{
return false;
}
}
int ProcessThePostfix(string Postfix, int NumberofPostfixes)
{
int Math;
for(int i = 0; i < NumberofPostfixes ; i++)
{
Math = Postfix[i];
switch(Math)
{
case '+': Values[i] = ValueA + ValueB;
return Values[i];
break;
case '-': Values[i] = ValueB - ValueA;
return Values[i];
break;
case '*': Values[i] = ValueA * ValueB;
return Values[i];
break;
case '/': if(ValueB != 0)
{
Values[i] = ValueA / ValueB;
return Values[i];
break;
}
if(ValueB = 0)
{
cout << "ERROR: 0 Divide Problem" << endl;
return -1;
break;
}
default: cout << "Invaild Postfix" << endl;
}
}
}
Attached File(s)
-
375StackProg3Data.txt (105bytes)
Number of downloads: 197

New Topic/Question
Reply



MultiQuote




|