postfix evaluation problem

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 2
Reputation: Aric69 is an unknown quantity at this point 
Solved Threads: 0
Aric69 Aric69 is offline Offline
Newbie Poster

postfix evaluation problem

 
0
  #1
Oct 13th, 2008
evaluate a given postfix expression containing some one-letter
integer variables a-z (in lower case) whose values are given in a data file.

the name of the data file will be : c:\\temp\\375_prog4_stack_data.txt
and you can hard code this name in your program.

there will not be more than 26 different variables (a-z) and
there will not be more than 20 postfixes for you to process.

the only operators in the post fixes will be : + - * /
all computations are integer computations and all postfixes will be legal.

use a stack of integers and the algorithm below to compute the value of a postfix expression.

algorithm:
scan the given postfix expression left to right char by char:
if char is operand (i.e., a variable a-z)
find its value and push the value on the stack
if the value of the variable is not given, report "invalid variable"
and go to the next problem
if char is an 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 in the above "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 (anything other than a-z) nor
operator (anything other than + - * /) report "invalid variable"
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 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/
abx^+

code below
  1. =====================================
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <cstdlib>
  7. #include <istream>
  8. #include <fstream>
  9. #include <stack>
  10. #include <cmath>
  11. #include <math.h>
  12. #include <iomanip>
  13.  
  14. using namespace std;
  15.  
  16. // FUNCTION DECLARATIONS
  17.  
  18. // returns the index of a given character
  19. int get_index(char ch);
  20. // checks to see if a given char is a valid var or not
  21. bool is_alpha(char ch);
  22. // checks to see if a given char is a plus sign
  23. bool is_plus(char ch);
  24. // checks to see if a given char is a minus sign
  25. bool is_minus(char ch);
  26. // checks to see if a given char is a multiplication sign
  27. bool is_mult(char ch);
  28. // checks to see if a given char is a power sign
  29. bool is_power(char ch);
  30. // checks to see if a given char is a division sign
  31. bool is_divide(char ch);
  32. // terminates the program due to invalid postfix
  33. void invalid_postfix_hault( );
  34. void invalid_postfix_hault( string err ); // to give more detail to error message
  35.  
  36. // GLOBAL VARIABLES
  37. char var[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  38. int value[26] = { 0 }; // initializes to zero.
  39.  
  40. int _tmain(int argc, _TCHAR* argv[])
  41. {
  42. typedef stack<int> STACK;
  43. int index; // hold the current index
  44. char next; // to use in reading the data file.
  45. int line = 1; // this will keep track of what line in the file we're on.
  46. int numvars; // the number of variables we will need to read in.
  47.  
  48. // open the 375_prog4_stack_data.txt file for input
  49. ifstream inStream;
  50. inStream.open( "375_prog4_stack_data.txt" );
  51. if ( inStream.fail( ) )
  52. {
  53. cout << "Input file opening failed." << endl;
  54. exit( 1 );
  55. }
  56.  
  57. // get the number of variables...
  58. inStream.get( next );
  59. numvars = (next - '0');
  60. inStream.get( next ); line++; // skip newline, increment line
  61. // set up the variables...
  62. while (line <= numvars+1)
  63. {
  64. inStream.get( next );
  65. next = tolower(next);
  66. if ( is_alpha( next ) )
  67. {
  68. cout << next;
  69. index = get_index( next );
  70. inStream.get( next ); cout << next; // skip equal sign
  71. inStream.get( next ); cout << next; // first digit
  72. // use a little stack to read in the variable, so it can handle
  73. // numbers > 9
  74. STACK x;
  75. int num = 0; // the value we will eventually end up with
  76. int holder = 0; // temporary holder
  77. int digit = 1; // keeps track of which digits column we are on
  78. while ( next != '\n' )
  79. {
  80. x.push( (next - '0') );
  81. inStream.get( next ); cout << next;
  82. }
  83. while (!x.empty())
  84. {
  85. holder = x.top();
  86. num = num+(holder*digit);
  87. x.pop(); digit = digit*10;
  88. }
  89. value[index] = num;
  90. }
  91. line++;
  92. }
  93.  
  94. // Now that the numbers are all set up and assigned,
  95. // we'll get to the heart of the program
  96. // This is the main algorithm to evaluate postfix
  97. // expressions.
  98. STACK postfix;
  99. int a = 0; // these will act as temporary holders
  100. int b = 0; //
  101. char test;
  102. while ( !inStream.eof( ) )
  103. {
  104. inStream.get( next );
  105.  
  106. // if end of line
  107. if ( next == '\n' )
  108. {
  109. // if the stack is empty the postfix must be invalid
  110. if (postfix.empty())
  111. {
  112. inStream.get( next );
  113. if (!inStream.eof())
  114. invalid_postfix_hault( "NO RESULT FOUND" );
  115. inStream.unget();
  116. }
  117. // if the stack has more than one value, the postfix must be invalid
  118. else if (postfix.size() > 1)
  119. {
  120. invalid_postfix_hault( "TOO MANY OPERANDS 1" );
  121. }
  122. else
  123. {
  124. cout << "\t\t= " << postfix.top();
  125. postfix.pop();
  126. }
  127. }
  128. // if it is an operand
  129. else if ( is_alpha( next ) )
  130. {
  131. postfix.push(value[get_index(next)]);
  132. }
  133. // if it is a + operator
  134. else if ( is_plus( next ) )
  135. {
  136. if (postfix.size() > 1)
  137. {
  138. b = postfix.top(); postfix.pop();
  139. a = postfix.top(); postfix.pop();
  140. postfix.push(a+b);
  141. }
  142. else
  143. { postfix.pop();
  144. invalid_postfix_hault( );
  145. }
  146. }
  147. // if it is a - operator
  148. else if ( is_minus( next ) )
  149. {
  150. if (postfix.size() > 1)
  151. {
  152. b = postfix.top(); postfix.pop();
  153. a = postfix.top(); postfix.pop();
  154. postfix.push(a-b);
  155. }
  156. else
  157. { postfix.pop();
  158. cout << "\t invalid postfx ERROR " ;
  159. }
  160.  
  161. }
  162. // if it is a * operator
  163. else if ( is_mult( next ) )
  164. {
  165. if (postfix.size() > 1)
  166. {
  167. b = postfix.top(); postfix.pop();
  168. a = postfix.top(); postfix.pop();
  169. postfix.push(a*b);
  170. }
  171. else
  172. { postfix.pop();
  173. cout << "\t invalid postfx ERROR " ;
  174. }
  175. }
  176. // if it is a ^ operator
  177. else if ( is_power( next ) )
  178. {
  179. if (postfix.size() > 1)
  180. {
  181. b = postfix.top(); postfix.pop();
  182. a = postfix.top(); postfix.pop();
  183. int temp= (int)pow((double)a,b);
  184. postfix.push(temp);
  185. }
  186. else
  187. { postfix.pop();
  188. cout << "\t invalid postfx ERROR ";
  189. }
  190. }
  191. // if it is a / operator
  192. else if ( is_divide( next ) )
  193. {
  194. if (postfix.size() > 1)
  195. {
  196. b = postfix.top(); postfix.pop();
  197. a = postfix.top(); postfix.pop();
  198. // watch for divide by zero
  199. if (b != 0)
  200. {
  201. postfix.push(a/b);
  202. }
  203. else
  204. {
  205. cout << "\t DIVIDE BY ZERO";
  206. }
  207. }
  208. else
  209. invalid_postfix_hault( "NOT ENOUGH OPERANDS" );
  210. }
  211. cout << next;
  212. }
  213.  
  214. // finalize program
  215.  
  216.  
  217. return 0;
  218. }
  219.  
  220. int get_index(char ch)
  221. {
  222. for (int i = 0; i < 26; i++)
  223. {
  224. // check the given character against each variable character
  225. if (var[i] == ch)
  226. return i;
  227. }
  228. return 27;
  229. }
  230.  
  231. bool is_alpha(char ch)
  232. {
  233. for (int i = 0; i < 26; i++)
  234. {
  235. if ( var[i] == ch )
  236. return true;
  237. }
  238. return false;
  239. }
  240.  
  241. bool is_plus(char ch)
  242. {
  243. if (ch == '+')
  244. return true;
  245. else
  246. return false;
  247. }
  248.  
  249. bool is_minus(char ch)
  250. {
  251. if (ch == '-')
  252. return true;
  253. else
  254. return false;
  255. }
  256.  
  257. bool is_mult(char ch)
  258. {
  259. if (ch == '*')
  260. return true;
  261. else
  262. return false;
  263. }
  264.  
  265. bool is_power(char ch)
  266. {
  267. if (ch == '^')
  268. return true;
  269. else
  270. return false;
  271. }
  272.  
  273. bool is_divide(char ch)
  274. {
  275. if (ch == '/')
  276. return true;
  277. else
  278. return false;
  279. }
  280.  
  281. void invalid_postfix_hault( )
  282. {
  283. // cout << "\t invalid postfx ERROR " << endl;
  284. }
  285.  
  286. void invalid_postfix_hault( string err )
  287. {
  288. cout << "\t invalid postfx ERROR " << endl;
  289. }

http://i132.photobucket.com/albums/q...1Oct132158.gif

seems to work on the first one that is invalid but not on the rest that are not in the file. not sure what to check for now.
Last edited by cscgal; Oct 14th, 2008 at 3:16 am. Reason: take out name / Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: postfix evaluation problem

 
0
  #2
Oct 14th, 2008
If you'd be so kind to narrow down your problem to a small piece of readable code, please
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: Aric69 is an unknown quantity at this point 
Solved Threads: 0
Aric69 Aric69 is offline Offline
Newbie Poster

Re: postfix evaluation problem

 
0
  #3
Oct 14th, 2008
I can not get the power function working it keeps giving me a problem and error.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: postfix evaluation problem

 
0
  #4
Oct 14th, 2008
What problem/error is involved?

I'd start out by creating an input that I know the results of. Then I'd either use my debugger to monitor the variables as the code is being processed or put in code to output the value of each variable to the screen as it is obtained and visually monitor that it is correct each time I get a new value for each variable.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 501 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC