Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

polish calculator 1

Status
Not open for further replies.

desanti

Programmer
Jul 9, 2004
51
US
the following,as copied from page 143 of 'awk a programming language' by(AKW) doesn not wish to cooperate.i know it is me but i do not know why.


{ for (i =1; i <= NF; i++)

if ($i ~/^[+-]?([0-9] + [.]?[0-9]*|[.][0-9]+)$/){

stack[++top] = $i

} else if ($i == "+" && top > 1 ) {

stack[top-1] += stack[top];top--

} else if ($i == "-" && top > 1 ) {

stack[top-1] -= stack[top];top--

} else if ($i == "*" && top > 1 ) {

stack[top-1] *= stack[top];top--

} else if ($i == "/" && top > 1 ) {

stack[top-1] /= stack[top];top--

} else if ($i == "^" && top > 1 ) {

stack[top-1] ^= stack[top];top--

} else {

printf("error: cannot evaluate %s\n", $i)
top = 0
next
}

if (top ==1)
printf("\t%.8g\n",stack[top--1])
else if ( top > 1) {
printf("error: too many operands\n")
top = 0
}
}
by the way no syntactical error occurs.all i get is 'cannot evaluate 1'
input:as shown in book is 1 2 + 3 4 - * 5 /
spaces are required.any help.thank you.
P.S.i thought all { had to be paired with a }.
i count 10 { and only 8 }.am i missing something?
 
Something like this ?
{
for(i=1;i<=NF;++i)
if($i~/^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)$/){
stack[++top]=$i
} else if($i=="+" && top>1){
stack[top-1]+=stack[top];top--
} else if($i=="-" && top>1){
stack[top-1]-=stack[top];top--
} else if($i=="*" && top>1){
stack[top-1]*=stack[top];top--
} else if($i=="/" && top>1){
stack[top-1]/=stack[top];top--
} else if($i=="^" && top>1){
stack[top-1]^=stack[top];top--
} else {
printf "error: cannot evaluate %s\n",$i
top=0
next
}
if(top==1)
printf "\t%.8g\n",stack[top--]
else if(top>1){
printf "error: too many operands\n"
top=0
}
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

> P.S.i thought all { had to be paired with a }.
> i count 10 { and only 8 }.am i missing something?

There are 9 of each.

[tt]if ($i ~/^[+-]?([0-9] + [.]?[0-9]*|[.][0-9]+)$/){[/tt]
should be
[tt]if ( $i ~/^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)$/ ){[/tt]

[tt]printf("\t%.8g\n",stack[top--1])[/tt]
should be
[tt]printf("\t%.8g\n",stack[top--])[/tt]

 
Error is here:
Code:
    if (top ==1)
        printf("\t%.8g\n",stack[[b]top--1[/b]])
    else if ( top > 1) [b]{[/b]
        printf("error: too many operands\n")
    [b]top = 0
    }[/b]
What is top--1 supposed to mean? And top-1 doesn't make sense here either, since if top==1, top-1 is 0, and array indexing starts at 1 in awk.
This should be stack[top].

The bracketing on the else causes top to be reset to zero only if top > 1, so it will be off on every line of input after the first.

Should be like so:
Code:
    if (top == 1)
        printf("\t%.8g\n",stack[[b]top[/b]])
    else if ( top > 1) 
        printf("error: too many operands\n")
    
    [b]top = 0[/b]
or use brackets to remove ambiquity:
Code:
    if (top == 1) [b]{[/b]
        printf("\t%.8g\n",stack[[b]top[/b]])
    [b]}[/b] else [b]{[/b]
        if ( top > 1) [b]{[/b]
            printf("error: too many operands\n")
        [b]}[/b]
    [b]}[/b]
    [b]top = 0[/b]
Here you can clearly see that top=0 is outside the if.
Actually, I think top=0 works better at the beginning, before the for.

The code works okay with these changes. I get -0.6 (-3 / 5) as the result with input 1 2 + 3 4 - * 5 /.

By the way, this is reverse Polish (postfix operators).. Polish is prefix operators.


 
Oh, and yes, futurelet is right about taking out the space in the regexp. I forgot to mention that.
 
futurelet: thank you for the info.your answer was the correct one.i was unaware that spaces had such an impact.
you are also correct on the { } count.i am 73 and senility is setting in.again many thanx to you and all that responded.
 

Thanks, desanti. Counting fences, such as { and }, is hard even for younger programmers. A good programmer's text editor will find matching fences for you. In the editor I use, if you put the cursor on one of [tt](){}[][/tt] and press CTRL-], the cursor will jump to the matching fence; you can put the cursor on the last [tt]}[/tt], press CTRL-], and if your curly braces are balanced, the cursor will jump to the first [tt]{[/tt].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top