473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help I'm stuck on a reverse polish calculation

ron
have been stuck on this for several days now.
I am trying to create a reverse polish calculator and I'm stuck at an
intermediate stage.
This is what I know I have to do (just not sure how to do it to much of a
newbie :( )
I'm trying to call method, isOp, in the int method, evaluate. While in INT
evaluate method and in a while(hasNextToken) loop. Calling isOp on each
token, nextToken.

If it returns False, convert to (Integer) and push the token into a Stack.
True, you will pop the two tokens, convert them to (Integer) and store them
into x and y respectively. Do the Arithmetic operation and then push it back
to the stack.

I'm pretty sure the IsOp test is good just can't figure out the first part.
I am trying to get the following code to work:
----------------------
public class Evaluator {//tokList contains a list of tokens in postfix
notation.
//evaluate the expression and return the result
int x, y,resu;
int i,j=0;
public int evaluate (ArrayList tokList) {
while (tokList.hasNextToken()){
isOp (s);
(tokList.nextToken());

Stack eval =new Stack();

if (false)
eval.push(new Integer(s));
else
{ y=((Integer) eval.pop()).intValue();
x=((Integer) eval.pop()).intValue();
switch (s)
{
case '+':
resu=x+y;
break;
case '-':
resu=x-y;
break;
case '*':
resu=x*y;
break;
case '/':
resu=x/y;
break;
default:
resu=0;
}
eval.push(new Integer(resu));

}
}
resu= ((Integer) eval.pop()).intValue();
return resu;
}
}

// Check whether s represents an operator
private boolean isOp(String s) {
return( s.equals( " + " ) || s.equals( " -- " ) || s.equals( " / " ) ||
s.equals( " * " ) );

} //isOp
private boolean hprecedence(String a, String b) {
return !a.equals("(") && (b.equals("+") || b.equals("-")
|| a.equals("*") || a.equals("/"));
} //precedence

}

----------------------

Any help would be greatly appreciated, I'm told it's quite simple once you
know how but I don't know....

Jul 17 '05 #1
1 4223
ron wrote:

<snip>
I am trying to get the following code to work:

<snip>

Try getting it to compile first.

Also, please indent properly if you'd like us to be able to read the
code in order to help you. This will also greatly help you to find the
obvious mistake.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Philip Kofoed | last post by:
Greetings, I have a SQL server 2000 running on an english win2000 workstation. In a database I have a table where one varchar column is set to polish collation. Regional settings for the system...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
23
by: gregf | last post by:
I have a paragraph of text pasted into a word document, it's in Polish, complete with polish characters. They show up just fine in word, but the program I use for web page programming, HomeSite,...
27
by: Chuck Grimsby | last post by:
(Repost, due to lack of submissions...) The Microsoft Access Product Group (the people who build Microsoft Access) want your help! One of the main things we're working on for the near future...
11
by: Steve Lambert | last post by:
Hi Anyone know how to convert a bracketed boolean expression into reverse polish? Cheers
11
by: abico | last post by:
Write a program that reads in a sequence of words and prints them in reverse order.Using STACK.
4
by: kalikoi | last post by:
Hi All I need a regular expression that retrieves the string that appears after the "/" As a example consider the following Input Output ------ -------
7
by: tylerca | last post by:
I'm attempting to start some process control using Python. I've have quite a bit of literature on networking, and have made some tinkering servers and clients for different protocols HTTP, FTP,...
8
by: schaf | last post by:
Hi NG! I have a problem in my remote application. After calling a remote function the calculation will be done by the service. The calculation result will be sent to the caller (client) via...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.