944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2341
  • C++ RSS
Oct 3rd, 2007
0

urgent help postfix evaluation

Expand Post »
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 <your name here> ===================================
===================================

here's my code....






#include <iostream>
#include <iomanip>
#include <stack>
#include <fstream>
#include <string>
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;
stack<int>s;
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<vars.length()) ) return ndx;
cout << "illegal variable " << var << "\n";
return -1; //this signals that the variable is invalid
}


void read_vars_vals(){
string var; int val;
ifstream inf;
inf.open("c:\\temp\\575_prog2_data.txt");
if (!inf) { cout << "input data should be in c:\\temp\\575_prog2_data.txt\n"; exit(0); }
//read the number of variables (global)
inf >> num_vars;
//loop and read this many vars and vals
for (int i=0; i<num_vars; i++) {
inf >> var >> val;
vars=vars+var;
vals[i]=val;
cout<<vars[i]<<" "<<vals[i]<<endl;
}
//then read all the strings of postfx expressions into an array of strings
int i=0;
while (inf >> 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<lenvars) ) return true;
else return false;
}
bool is_operator(string ch) {
string ops="+-^*/"; found=ops.find(ch); lenops=ops.length();
if ( (found>=0) && (found<lenops) ) return true;
else return false;
}

void process_operand(string opnd,int i) {

if (is_operand(opnd))

s.push(vals[i]);

else
cout<<"invalid postfix error" <<endl;
// process_pf(opnd);
}

void process_operator(string op) {
if (is_operator(op))
{
valA = s.top();
s.pop();
valB = s.top();
s.pop();
}
}

void process_pf(string pf) { //pf=a single postfix

// i=0;
token = pf[i];

//while((i < pf.size()) && (token != '='))
//{
switch(token)
{
case '+': vals[i] = valA + valB;
break;
case '-': vals[i] = valB - valA;
break;
case '*': vals[i] = valA*valB;
break;

case '^':vals[i] = valA^valB;

case '/':
try
{
vals[i] = valA/valB;
}
catch(...)
{
cerr<<"divide by zero error";
}
break;
}
s.push(vals[i]);

//i++;
//token = pf[i];

cout << pf << " " << vals[i] << endl;

cout << endl;

}



void main() {

read_vars_vals();

for (i=0; i<num_pfs; i++) {
process_operand(pf[i],i);
process_operator(pf[i]);
process_pf(pf[i]);
}
//footer info like your name etci]
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarah24 is offline Offline
3 posts
since Sep 2007
Oct 3rd, 2007
0

Re: urgent help postfix evaluation

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
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stack>
  5. #include <fstream>
  6. #include <limits>
  7.  
  8. using namespace std;
  9.  
  10. bool myCheck ( string );
  11. bool isNumber ( char ch );
  12. int getIndex ( string ch );
  13.  
  14. int main()
  15. {
  16. ifstream read ( "c:\\temp\\575_prog2_data.txt" );
  17. string crap;
  18.  
  19. int vals[26] = {0};
  20.  
  21. int placeHolder = 0;
  22. while ( read >> crap )
  23. {
  24. if ( ( myCheck ( crap ) == false )
  25. && ( crap.length() > 1 ) )
  26. {
  27. cout << "Postfix Expression maybe?:" << crap << endl;
  28. }
  29. else if ( ( myCheck ( crap ) == false )
  30. && ( crap.length() == 1 ) )
  31. {
  32. cout << "Var:" << crap << endl;
  33. placeHolder = getIndex ( crap );
  34. }
  35. else
  36. {
  37. cout << "integer:" << crap << endl;
  38. istringstream myStream ( crap );
  39. int j;
  40. myStream >> j;
  41. vals[placeHolder] = j;
  42. }
  43. }
  44. read.close();
  45.  
  46. string letterz = "abcdefghijklmnopqrstuvwxyz";
  47.  
  48. cout << "\n";
  49. for ( int i = 0; i < 26; i++ )
  50. {
  51. cout << letterz[i] << "=" << vals[i] << " ";
  52. }
  53.  
  54. cin.get();
  55. }
  56. bool myCheck ( string t )
  57. {
  58. int length = t.length();
  59.  
  60. int counter = 0;
  61. for ( int i = 0; i < length; i++ )
  62. {
  63. if ( isNumber ( t[i] ) == true )
  64. {
  65. counter++;
  66. }
  67. }
  68. if ( counter == length )
  69. return true;
  70. else
  71. return false;
  72. }
  73.  
  74. bool isNumber ( char ch )
  75. {
  76. if ( ( ch >= '0' ) && ( ch <= '9' ) )
  77. return true;
  78. else
  79. return false;
  80. }
  81.  
  82. int getIndex ( string ch )
  83. {
  84. string letterz = "abcdefghijklmnopqrstuvwxyz";
  85. int t = letterz.length();
  86.  
  87. for ( int i = 0; i < t; i++ )
  88. {
  89. if ( ch == letterz.substr ( i, 1 ) )
  90. {
  91. return i;
  92. }
  93. }
  94. }

My output
C++ Syntax (Toggle Plain Text)
  1. Var:a
  2. integer:4
  3. Var:d
  4. integer:9
  5. Var:z
  6. integer:17
  7. Var:y
  8. integer:0
  9. Var:x
  10. integer:2
  11. Postfix Expression maybe?:aD+
  12. Postfix Expression maybe?:ad+zy-x/*
  13. Postfix Expression maybe?:xz*yd+ad-*+
  14. Postfix Expression maybe?:ab+
  15. Postfix Expression maybe?:ad*yz*+ad*yz*-*
  16. Postfix Expression maybe?:adz*+yx/-
  17. Postfix Expression maybe?:zy/
  18. Postfix Expression maybe?:adx^+
  19.  
  20. 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
  21. u=0 v=0 w=0 x=2 y=0 z=17
  22.  
Last edited by iamthwee; Oct 3rd, 2007 at 5:27 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 4th, 2007
0

Re: urgent help postfix evaluation

thanks for looking into my problem, i think i can go on further now..
thanks again...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sarah24 is offline Offline
3 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Searching for words in a file
Next Thread in C++ Forum Timeline: Confused about some C++ programs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC