Hello,
I am looking for a Java source code for Polish Stack, please.
A Polish Stack - A Stack algoritm, that is usefull for developing a tool for
computing basic formulas, with priority.
i.e a formula :
2 + 3 x 5 = 17 (and not 25, because x is perior to +)
The function that evaluate this can be done by some recrusive statements
(which I don't preffer), or by polish-stack.
If there is no source code :
What is the concept of polish stack ?
Thanks :)
|
|
0
|
|
|
|
Reply
|
no_spam_please4 (116)
|
5/10/2008 11:46:26 AM |
|
On Sat, 10 May 2008 14:46:26 +0300, "Mr. X."
<no_spam_please@nospam_please.com> wrote, quoted or indirectly quoted
someone who said :
>What is the concept of polish stack ?
Google for "postfix notation" . Read up on how FORTH works.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
|
|
0
|
|
|
|
Reply
|
see_website (4855)
|
5/10/2008 12:01:39 PM
|
|
Mr. X. wrote:
> Hello,
> I am looking for a Java source code for Polish Stack, please.
> A Polish Stack - A Stack algoritm, that is usefull for developing a tool for
> computing basic formulas, with priority.
> i.e a formula :
> 2 + 3 x 5 = 17 (and not 25, because x is perior to +)
> The function that evaluate this can be done by some recrusive statements
> (which I don't preffer), or by polish-stack.
It is generally inadvisable to ask for exact source code in forums--many
people here would be willing to consult with you for the going rate of
$X/hour (or other relevant currency).
Google proves useful here:
<http://en.wikipedia.org/wiki/Shunting_yard_algorithm> tells you what
you need to do.
> If there is no source code :
> What is the concept of polish stack ?
Polish or reverse-polish notation (the latter tends to be more commonly
used) is better for computers to work with than infix notation, because
it doesn't requires parenthesis to operate, and because computation is a
simple stack-based algorithm. The "Polish Stack," I am inferring, is the
stack of numbers generated in such an algorithm.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
|
|
0
|
|
|
|
Reply
|
Pidgeot18 (1393)
|
5/10/2008 12:08:56 PM
|
|
Mr. X. wrote:
> Hello,
> I am looking for a Java source code for Polish Stack, please.
> A Polish Stack - A Stack algoritm, that is usefull for developing a tool
> for computing basic formulas, with priority.
Settle for a reverse polish stack?
<http://preview.tinyurl.com/648qo4>
> i.e a formula :
> 2 + 3 x 5 = 17 (and not 25, because x is perior to +)
> The function that evaluate this can be done by some recrusive statements
> (which I don't preffer), or by polish-stack.
<https://javacc.dev.java.net/> or <http://www.ssw.uni-linz.ac.at/coco/>
or <http://www.singularsys.com/jep/>
>
> If there is no source code :
> What is the concept of polish stack ?
<http://en.wikipedia.org/wiki/Stack_(data_structure)>
|
|
0
|
|
|
|
Reply
|
oohiggins (266)
|
5/10/2008 1:02:13 PM
|
|
On Sat, 10 May 2008 14:46:26 +0300, "Mr. X."
<no_spam_please@nospam_please.com> wrote, quoted or indirectly quoted
someone who said :
>2 + 3 x 5 = 17 (and not 25, because x is perior to +)
( 2 + 3 ) * 4 would be written in postfix as
2 3 + 4 *
imagine a stack at these various stages during execution
-
-------
2
------
3
2
-------
5
-------
4
5
-------
20
So your + operator looks at the top two elements of the stack adds
them and replaces the operands with the result.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
|
|
0
|
|
|
|
Reply
|
see_website (4855)
|
5/10/2008 1:19:10 PM
|
|
On Sat, 10 May 2008 12:08:56 +0000, Joshua Cranmer wrote:
> Polish or reverse-polish notation (the latter tends to be more commonly
> used) is better for computers to work with than infix notation, because
> it doesn't requires parenthesis to operate, and because computation is a
> simple stack-based algorithm.
[Snip]
Not just computers. I like my calculators to work this way. It really
is much easier once you get used to it.
--
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
|
|
0
|
|
|
|
Reply
|
kt-usenet (235)
|
5/10/2008 1:32:58 PM
|
|
On 10 May 2008 13:32:58 GMT, "Kenneth P. Turvey"
<kt-usenet@squeakydolphin.com> wrote, quoted or indirectly quoted
someone who said :
>Not just computers. I like my calculators to work this way. It really
>is much easier once you get used to it.
Quite right . Once you get used to it, postfix is so clear without 20
layers of precedence rules, and you can store intermediate results so
easily during a calculation for later use. Had we started out with
postfix in school, infix notation would seem baroque.
In a similar way, had you started out with metric in school, American
measure would have seemed impossibly quaint.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
|
|
0
|
|
|
|
Reply
|
see_website (4855)
|
5/10/2008 1:41:22 PM
|
|
On Sat, 10 May 2008 14:46:26 +0300, "Mr. X."
<no_spam_please@nospam_please.com> wrote:
>Hello,
>I am looking for a Java source code for Polish Stack, please.
>A Polish Stack - A Stack algoritm, that is usefull for developing a tool for
>computing basic formulas, with priority.
>i.e a formula :
>2 + 3 x 5 = 17 (and not 25, because x is perior to +)
>The function that evaluate this can be done by some recrusive statements
>(which I don't preffer), or by polish-stack.
>
>If there is no source code :
>What is the concept of polish stack ?
>
>Thanks :)
>
See http://en.wikipedia.org/wiki/Reverse_Polish_notation
I suspect that is what you are looking for.
rossum
|
|
0
|
|
|
|
Reply
|
rossum48 (643)
|
5/10/2008 1:45:46 PM
|
|
On Sat, 10 May 2008 13:41:22 +0000, Roedy Green wrote:
> In a similar way, had you started out with metric in school, American
> measure would have seemed impossibly quaint.
Still does..
One thing that has always given me an itch though is our measurement of
time. In everything else the metric system switches to a nice decimal
system, but time.. no such thing.
It is awfully convenient to have years and days line up correctly, but
months, hours and minutes?
Our system of time seems antiquated. Certainly there are better units
for the measurement of the passage of time.
--
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
|
|
0
|
|
|
|
Reply
|
kt-usenet (235)
|
5/10/2008 4:08:08 PM
|
|
Kenneth P. Turvey wrote:
> On Sat, 10 May 2008 13:41:22 +0000, Roedy Green wrote:
>> In a similar way, had you started out with metric in school, American
>> measure would have seemed impossibly quaint.
>
> Still does..
>
> One thing that has always given me an itch though is our measurement of
> time. In everything else the metric system switches to a nice decimal
> system, but time.. no such thing.
>
> It is awfully convenient to have years and days line up correctly, but
> months, hours and minutes?
>
> Our system of time seems antiquated. Certainly there are better units
> for the measurement of the passage of time.
Something in time could be changed, but other characteristics is
given. A day is the time it takes the earth to rotate around itself.
A year is the time it takes the earth to rotate around the sun. We
can't tell the earth to male that 1000 days per year instead of those
365.25 !
Arne
|
|
0
|
|
|
|
Reply
|
arne6 (9481)
|
5/10/2008 4:34:51 PM
|
|
....
How can I translate from 2 + (3 x 5) to something in stack as :
3,5,x,2,+ ?
.... with O(N) at maximum, where N is the number of elements,
with no recrusive algoritm ?
Thanks :)
|
|
0
|
|
|
|
Reply
|
no_spam_please4 (116)
|
5/10/2008 4:36:58 PM
|
|
Mr. X. wrote:
> ...
> How can I translate from 2 + (3 x 5) to something in stack as :
> 3,5,x,2,+ ?
>
> ... with O(N) at maximum, where N is the number of elements,
> with no recrusive algoritm ?
It should be:
2 3 5 * +
and the algorithm should be in most beginner books on
algorithms.
Arne
|
|
0
|
|
|
|
Reply
|
arne6 (9481)
|
5/10/2008 4:58:09 PM
|
|
"Kenneth P. Turvey" <kt-usenet@squeakydolphin.com> wrote in message
news:4825c868$0$25758$ec3e2dad@news.usenetmonster.com...
> On Sat, 10 May 2008 13:41:22 +0000, Roedy Green wrote:
>
>
>> In a similar way, had you started out with metric in school, American
>> measure would have seemed impossibly quaint.
>
> Still does..
>
> One thing that has always given me an itch though is our measurement of
> time. In everything else the metric system switches to a nice decimal
> system, but time.. no such thing.
>
> It is awfully convenient to have years and days line up correctly, but
> months, hours and minutes?
>
> Our system of time seems antiquated. Certainly there are better units
> for the measurement of the passage of time.
Calculating the number of milliseconds before or since Jan 01, 1970 GMT
seems quite workable. Then you only need to convert to and from local clock
time for input and display.
|
|
0
|
|
|
|
Reply
|
karl.uppiano (122)
|
5/10/2008 6:10:02 PM
|
|
"Kenneth P. Turvey" <kt-usenet@squeakydolphin.com> wrote in message
news:4825a40a$0$30073$ec3e2dad@news.usenetmonster.com...
> On Sat, 10 May 2008 12:08:56 +0000, Joshua Cranmer wrote:
>
>> Polish or reverse-polish notation (the latter tends to be more commonly
>> used) is better for computers to work with than infix notation, because
>> it doesn't requires parenthesis to operate, and because computation is a
>> simple stack-based algorithm.
> [Snip]
>
> Not just computers. I like my calculators to work this way. It really
> is much easier once you get used to it.
Yup. I never use the parenthesis on infix notation calculators because I
never can seem to match the open-parens with the close-parens. Whenever I
have serious mathematical calculation to do, I get out my trusty HP11, or my
HP32S II.
|
|
0
|
|
|
|
Reply
|
karl.uppiano (122)
|
5/10/2008 6:13:35 PM
|
|
On Sat, 10 May 2008 12:34:51 -0400, Arne Vajhøj wrote:
> Something in time could be changed, but other characteristics is given.
> A day is the time it takes the earth to rotate around itself. A year is
> the time it takes the earth to rotate around the sun. We can't tell the
> earth to male that 1000 days per year instead of those 365.25 !
That's certainly true, but does it really make sense for us to have 24
hour days, 60 minute hours and 60 minute seconds? Why not 10 hour days,
10 minute hours, and 10 second minutes or something similar?
Do we really need every day to start at the same time anymore? It is
certainly convenient, but is there another way to get that convenience
without screwing up our units of measure? Couldn't days just be
fractions of a year, which is what they really are anyway.
I don't know.
It just seems like people just decided to ignore time when they developed
the metric system.
--
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
|
|
0
|
|
|
|
Reply
|
kt-usenet (235)
|
5/10/2008 10:49:49 PM
|
|
Kenneth P. Turvey wrote:
> On Sat, 10 May 2008 12:34:51 -0400, Arne Vajhøj wrote:
>
>> Something in time could be changed, but other characteristics is given.
>> A day is the time it takes the earth to rotate around itself. A year is
>> the time it takes the earth to rotate around the sun. We can't tell the
>> earth to male that 1000 days per year instead of those 365.25 !
>
> That's certainly true, but does it really make sense for us to have 24
> hour days, 60 minute hours and 60 minute seconds? Why not 10 hour days,
> 10 minute hours, and 10 second minutes or something similar?
>
> Do we really need every day to start at the same time anymore? It is
> certainly convenient, but is there another way to get that convenience
> without screwing up our units of measure? Couldn't days just be
> fractions of a year, which is what they really are anyway.
>
> I don't know.
>
> It just seems like people just decided to ignore time when they developed
> the metric system.
Sixty is a better number base than ten anyway. We should use sexagintesimal
(exekontesimal?) arithmetic for everything.
--
Lew
|
|
0
|
|
|
|
Reply
|
lew (2143)
|
5/10/2008 11:07:42 PM
|
|
"Lew" <lew@lewscanon.com> wrote in message
news:Q4GdnREUWeojt7vVnZ2dnUVZ_uLinZ2d@comcast.com...
> Kenneth P. Turvey wrote:
>> On Sat, 10 May 2008 12:34:51 -0400, Arne Vajh�j wrote:
>>
>>> Something in time could be changed, but other characteristics is given.
>>> A day is the time it takes the earth to rotate around itself. A year is
>>> the time it takes the earth to rotate around the sun. We can't tell the
>>> earth to male that 1000 days per year instead of those 365.25 !
>>
>> That's certainly true, but does it really make sense for us to have 24
>> hour days, 60 minute hours and 60 minute seconds? Why not 10 hour days,
>> 10 minute hours, and 10 second minutes or something similar? Do we
>> really need every day to start at the same time anymore? It is certainly
>> convenient, but is there another way to get that convenience without
>> screwing up our units of measure? Couldn't days just be fractions of a
>> year, which is what they really are anyway. I don't know. It just seems
>> like people just decided to ignore time when they developed the metric
>> system.
>
> Sixty is a better number base than ten anyway. We should use
> sexagintesimal (exekontesimal?) arithmetic for everything.
That's a lot of symbols to keep track of. Even our alphabet has only 26,
give or take a few. While the Chinese alphabet has more discrete symbols,
but they aren't exactly distinct in the same way as Western symbols are.
|
|
0
|
|
|
|
Reply
|
karl.uppiano (122)
|
5/11/2008 9:15:49 PM
|
|
On May 10, 5:49=A0pm, "Kenneth P. Turvey" <kt-use...@squeakydolphin.com>
wrote:
> That's certainly true, but does it really make sense for us to have 24
> hour days, 60 minute hours and 60 minute seconds? =A0Why not 10 hour days,=
> 10 minute hours, and 10 second minutes or something similar? =A0
>
> Do we really need every day to start at the same time anymore? =A0It is
> certainly convenient, but is there another way to get that convenience
> without screwing up our units of measure? =A0Couldn't days just be
> fractions of a year, which is what they really are anyway. =A0
>
> I don't know. =A0
>
> It just seems like people just decided to ignore time when they developed
> the metric system.
At the time of the French revolution, when the metric system was
invented, France for a while (The last vestiges were abandoned in
1805.) went to 10 hours per day, 100 minutes per hour and 100 seconds
per minute. The resulting second, with 100,000 of them from noon to
noon, was a little shorter than the conventional second (86,400
seconds from noon to noon). There were also 12 metric months per year,
3 weeks per month, 10 days per week.
--Mike Amling
|
|
0
|
|
|
|
Reply
|
mamling
|
5/12/2008 7:57:42 PM
|
|
Please help me.
At least how do you get to (the rest is obvious)
> 2 3 5 * +
.... and I didn't find something on the internet.
I whould like whether you can lead me to some source, please.
Thanks :o)
|
|
0
|
|
|
|
Reply
|
no_spam_please4 (116)
|
5/18/2008 1:10:37 PM
|
|
Also,
I need explain what is the representation of (for example) ;
9+3+4*5-2 ?
Thanks :)
|
|
0
|
|
|
|
Reply
|
no_spam_please4 (116)
|
5/18/2008 2:43:18 PM
|
|
Mr. X. wrote:
> I need explain what is the representation of (for example) ;
> 9+3+4*5-2 ?
9 3 + 4 5 * + 2 -
Arne
|
|
0
|
|
|
|
Reply
|
arne6 (9481)
|
5/18/2008 3:40:33 PM
|
|
Mr. X. wrote:
> Please help me.
> At least how do you get to (the rest is obvious)
>> 2 3 5 * +
> ... and I didn't find something on the internet.
>
> I whould like whether you can lead me to some source, please.
I think your Google is broken.
Mine has 32000 hits on:
convert infix postfix
Arne
|
|
0
|
|
|
|
Reply
|
arne6 (9481)
|
5/18/2008 3:43:08 PM
|
|
Hi,
I don't have book of polish-stack, and I won't buy any,
If you don't want to answer - please, don't answer at all.
I just want to know the concept in details !
I have tried a lot searching google, but it seems that I found my own
questions,
or answers that are after the transform to 95+ ... (and not 9 + 5)
1. How do I get it from 9+3+4*5-2 to any kind of : 9 3 + 4 5 * + 2 - ?
--- > I suppose, the concept is to run from left to right arguments,
put values to stack, and put operator to stack, but if current operator's
priority is higher then previous,
then wait it to be put at the next step.
so for 9 + 3 + 4 * 5, the sequence is 9, 3, + (now 4 has * operator which
its priority is higher, so I wait ...),
ten 4,5,* ... (something like that ...).
I didn't see any clue of that algorithm, so I suppose that's right (Is that
true ?)
2. Any free java code somewhere of that algorithm, it will be fine.
Thanks :)
|
|
0
|
|
|
|
Reply
|
no_spam_please4 (116)
|
5/18/2008 9:51:57 PM
|
|
On Mon, 19 May 2008 00:51:57 +0300, Mr. X. wrote:
> 2. Any free java code somewhere of that algorithm, it will be fine.
This is homework isn't it? If so, why don't you do it yourself?
--
Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
|
|
0
|
|
|
|
Reply
|
kt-usenet (235)
|
5/18/2008 10:18:52 PM
|
|
....
Also, what I think (only guess) :
there are two stacks :
1 - stack of arguments (let call it : SA)
2 - stack of operators (let call it : SO).
We always push on stack.
We pop from stack, only at end of formula, or when we reach an operator at
the same priority or less then current priority.
The 1st 2nd arguments are operated by the 1st argument.
When pop from stack - we pop from the last argument of the stack (LIFO).
9+3+4*5-2
SA:9
SO:<empty>
SA:9
SO:+
SA:9,3
SO:+
(there is no another operand, so we keep pushing on stack).
SA:9,3
SO:+,+
(now we can pop the lasts : 9,3 with +, and push the result again into
stack)
SA:12
So:+
SA:12,4
SO:+
(there is no next argument, so we're keep trying).
SA:12,4
SO:+,*
SA:12,4,5
SO:+,*
SA:12,4,5,
SO:+,*,-
(now the priority is downed so we pop the last arguments : 4,5, with
corresponding operator : *, and push back to stack).
SA:12,20
SO:+,-
SA:32
SO:-
SA:32,2
SO:-
<end>
SA:30
SO:<empty>
Stack is empty, so we pop the result on argument : SA.
result = 30 !
The same thing can be for brakes "(" with a higher priority no. (I can mark
for each element it's priority, by some kind of method :
when priority get higher, I increment the priority counter, when it getting
lower I decrement the priority counter, and every time, and due that I know
how can I handle the stack as I have described above.
Is the above right thorey ?
If the above is right, I think I'll mange...
Thanks :)
|
|
0
|
|
|
|
Reply
|
no_spam_please4 (116)
|
5/18/2008 10:27:32 PM
|
|
No, No !!!
|
|
0
|
|
|
|
Reply
|
no_spam_please4 (116)
|
5/18/2008 10:29:00 PM
|
|
"Mr. X." <no_spam_please@nospam_please.com> writes:
>there are two stacks :
For this approach (I do not know whether this is the same as a
�polish stack�), you might read the paragraph starting with
�I gave each symbol in the syntax table a particular code�
at
http://www.woz.org/letters/general/03.html
. Also see the English text at the end of
http://groups.google.com/groups?selm=8zYj1kuMRGB@gmx.de
. The classical reference is
Sequential Formula Translation
K. Samelson and F. L. Bauer
Communications of the ACM
Volume 3, Issue 2 (February 1960)
Pages: 76 - 83
ISSN:0001-0782
. I have implemented such a stack pair once in Java.
The support code is rather large and unpublished yet.
But the two core methods for the stack pair are given below.
public static void acceptOperator
( final OperatorToken operatorToken,
final de.dclj.ram.notation.bauer.notation.PlacementHint placementHint,
final OperatorStack operatorStack,
final DesignationStack designationStack )
{ int rightPriority;
if( placementHint instanceof OperatorMustBePrefixPlacementHint )
{ rightPriority = getOperatorRightPriorityInPrefixContext( operatorToken ); }
else
{ rightPriority = getOperatorRightPriorityInInfixContext( operatorToken ); }
while( isReducible( operatorStack, rightPriority ))
{ OperatorToken operator = operatorStack.pop();
operator.reduce( placementHint, operatorStack, designationStack ); }
operatorStack.accept( operatorToken ); }
public static void acceptLiteral
( final LiteralToken literalToken,
final de.dclj.ram.notation.bauer.notation.PlacementHint placementHint,
final OperatorStack operatorStack,
final DesignationStack designationStack )
{ designationStack.accept( literalToken.value() ); }}
|
|
0
|
|
|
|
Reply
|
ram (2825)
|
5/18/2008 11:15:21 PM
|
|
Mr. X. wrote:
> I don't have book of polish-stack, and I won't buy any,
Do you want to get through life asking people to spend
time explaining thing sin detail for you instead of
you actually sit down, read the relevant books and learn something ?
> If you don't want to answer - please, don't answer at all.
That is our decision.
> I just want to know the concept in details !
Study !
Arne
|
|
0
|
|
|
|
Reply
|
arne6 (9481)
|
5/19/2008 1:03:43 AM
|
|
Mr. X. wrote:
>> If you don't want to answer - please, don't answer at all.
That's a weird response considering that Arne fully answered your question.
Why did you take umbrage at that?
Why the heck don't you look it up? It took about a minute to find
<http://en.wikipedia.org/wiki/Reverse_Polish_notation>
starting from the Wikipedia home page. Maybe that would be a better approach
than getting all snarky with people who answer your questions, and acting like
a prima donna about doing the work yourself.
Also, GIYF.
--
Lew
|
|
0
|
|
|
|
Reply
|
lew (2143)
|
5/20/2008 1:30:49 AM
|
|
Arne Vajhøj wrote:
> Mr. X. wrote:
>> Please help me.
>> At least how do you get to (the rest is obvious)
>>> 2 3 5 * +
>> ... and I didn't find something on the internet.
>>
>> I whould like whether you can lead me to some source, please.
>
> I think your Google is broken.
>
> Mine has 32000 hits on:
>
> convert infix postfix
Apparently his ability to read the answers given earlier is also broken:
Approximately twenty-two minutes after the OP posted the original question on
May 10, Joshua Cranmer posted:
> Google proves useful here:
> <http://en.wikipedia.org/wiki/Shunting_yard_algorithm>
> tells you what you need to do.
So he has had the answer for a week and a half and he's bitching the newsgroup
out for not providing the answer.
--
Lew
|
|
0
|
|
|
|
Reply
|
lew (2143)
|
5/20/2008 1:34:40 AM
|
|
|
29 Replies
19 Views
(page loaded in 0.349 seconds)
|