473,783 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(hasNextTo ken) 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.hasNex tToken()){
isOp (s);
(tokList.nextTo ken());

Stack eval =new Stack();

if (false)
eval.push(new Integer(s));
else
{ y=((Integer) eval.pop()).int Value();
x=((Integer) eval.pop()).int Value();
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()).int Value();
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(Str ing 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 4259
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
8802
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 is polish. Data entered in a client application looks fine until they are posted. When reading the data with the client application, the special polish characters are incorrect, they appears as e.g. '1' and '3'. The strange thing is that when I...
5
3808
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 complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
23
3156
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, won't translate it. When I paste the text into the code, the special characters are missing. If they would show up there I could use the Replace Special Characters feature to change it to the proper code, but it won't even paste into it...
27
3645
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 is a conversion tool to take Microsoft Access 97 databases (primarily, but also Microsoft Access 2000 or 2002/XP databases) up to Microsoft Office Access 2003. For us to do this, we need sample databases from *you*, our customers, to ensure a...
11
2285
by: Steve Lambert | last post by:
Hi Anyone know how to convert a bracketed boolean expression into reverse polish? Cheers
11
1906
by: abico | last post by:
Write a program that reads in a sequence of words and prints them in reverse order.Using STACK.
4
1097
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
1533
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, etc... But now it's time for the murky web of industrial protocol. I'm looking to start with IO and servo controls via Ethernet. Questions: Is there an existing forum on this already? What protocols are the most python friendly? i.e. are...
8
5027
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 remote event. The following behavior can be observed: 1.) Right after the start of the server the first response via remote event will take a long time.
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.