954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

urgent help postfix evaluation

Hi Experts!
i need some help now i have written a code but my output is not correct its reading everything from text file but not computing it correctly,,,pls pls help.

here's the problem statement..

evaluate a postfix expression containing some one-letter variables a-z (lower case) whose values are given in a data file called: c:\\temp\\575_prog2_data.txt
make sure to include this complete path for the data file.
there will not be more than 26 different variables (a-z) and there will not be more than 20 postfixes the operators will be: + - * / and ^
all computations are integer computations e.g., 7/2=3
use a stack of integers and the algorithm below to compute the value of the given postfix expressions.
scan a postfix expression left to right char by char:
if char is operand (i.e., variable ) find its value and push the value on the stack
if the value of the variable is not given, report "invalid postfix" and go to the next problem
if char is operator + - * / ^ get the top of the stack (say A) and pop the stack.
again get the top of the stack (say B) and pop the stack. compute "B operator A" and push the result on stack
if there is any stack problem AT ANY TIME "report invalid postfix" and go to the next problem
if there is 0 divide problem report "zero divide" and go to the next problem
if char is neither variable nor operator report "invalid postfix" and go to the next problem
at the end of scanning the postfix, if stack contains exactly one integer return this value as the answer
if stack is empty or has more than one integer report invalid postfix and go to the next problem
end of algorithm

input data format:
the first line gives how many variables there are (n)
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 need to evaluate.
================
5 //this is only a sample; your data may be different
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/
adx^+

your output should look like:
=============================
a 4
d 9
z 17
y 0
x 2
aD+ invalid postfix ERROR
ad+zy-x/* = 104
xz*yd+ad-*+ = -11
ab+ invalid postfix ERROR
ad*yz*+ad*yz*-* = 1296
adz*+yx/- = 157
zy/ zero divide ERROR
adx^+ = 85
program 575_prog2.cpp written by ===================================
===================================

here's my code....

#include
#include
#include
#include
#include
using namespace std;
using std::string;
using std::cout;
using std::cerr;
#define MAX 20

//global variables
string vars=""; //add the variables to this string as you read them
int vals[26]; //add the values of the variables to this array as you read them
int valA;
int valB;
string pf[MAX];
int num_vars; //number of variables
int num_pfs;
int ndx;
stacks;
int found;
int ops;
int lenops;
char token;
int i;
void process_pf(string pf);

int var2ndx(string var) { //given a variable it returns its index in vars
//so that its value may be found as vals[index]
ndx=vars.find(var);
if ( (ndx>=0) && (ndx> num_vars;
//loop and read this many vars and vals
for (int i=0; i> var >> val;
vars=vars+var;
vals[i]=val;
cout<> pf[i]) i++; //pf is the array of postfix strings (global variable)
num_pfs=i;
inf.close();
}

bool is_operand(string ch) {
found=vars.find(ch); int lenvars=vars.length();
if ( (found>=0) && (found=0) && (found

sarah24
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Before you start doin' the postfix calculations with the stack I would make sure you can read the file in and assign all the variables properly?

Example

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <fstream>
#include <limits>

using namespace std;

bool myCheck ( string );
bool isNumber ( char ch );
int getIndex ( string ch );

int main()
{
  ifstream read ( "c:\\temp\\575_prog2_data.txt" );
  string crap;

  int vals[26] = {0};

  int placeHolder = 0;
  while ( read >> crap )
  {
    if ( ( myCheck ( crap ) == false )
         && ( crap.length() > 1 ) )
    {
      cout << "Postfix Expression maybe?:" << crap << endl;
    }
    else if ( ( myCheck ( crap ) == false )
              && ( crap.length() == 1 ) )
    {
      cout << "Var:" << crap << endl;
      placeHolder = getIndex ( crap );
    }
    else
    {
      cout << "integer:" << crap << endl;
      istringstream myStream ( crap );
      int j;
      myStream >> j;
      vals[placeHolder] = j;
    }
  }
  read.close();

  string letterz = "abcdefghijklmnopqrstuvwxyz";

  cout << "\n";
  for ( int i = 0; i < 26; i++ )
  {
    cout << letterz[i] << "=" << vals[i] << " ";
  }

  cin.get();
}
bool myCheck ( string t )
{
  int length = t.length();

  int counter = 0;
  for ( int i = 0; i < length; i++ )
  {
    if ( isNumber ( t[i] ) == true )
    {
      counter++;
    }
  }
  if ( counter == length )
    return true;
  else
    return false;
}

bool isNumber ( char ch )
{
  if  ( ( ch >= '0' ) && ( ch <= '9' ) )
    return true;
  else
    return false;
}

int getIndex ( string ch )
{
  string letterz = "abcdefghijklmnopqrstuvwxyz";
  int t = letterz.length();

  for ( int i = 0; i < t; i++ )
  {
    if ( ch == letterz.substr ( i, 1 ) )
    {
      return i;
    }
  }
}


My output

Var:a
integer:4
Var:d
integer:9
Var:z
integer:17
Var:y
integer:0
Var:x
integer:2
Postfix Expression maybe?:aD+
Postfix Expression maybe?:ad+zy-x/*
Postfix Expression maybe?:xz*yd+ad-*+
Postfix Expression maybe?:ab+
Postfix Expression maybe?:ad*yz*+ad*yz*-*
Postfix Expression maybe?:adz*+yx/-
Postfix Expression maybe?:zy/
Postfix Expression maybe?:adx^+

a=4 b=0 c=0 d=9 e=0 f=0 g=0 h=0 i=0 j=0 k=0 l=0 m=0 n=0 o=0 p=0 q=0 r=0 s=0 t=0
u=0 v=0 w=0 x=2 y=0 z=17
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

thanks for looking into my problem, i think i can go on further now..
thanks again...

sarah24
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You