Algebraic to RPN - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP Prime Software Library (/forum-15.html) +--- Thread: Algebraic to RPN (/thread-7824.html) |
Algebraic to RPN - Han - 02-24-2017 09:27 PM The code below takes an algebraic expression and converts it to an RPN string. The program itself is not all that useful (in my opinion) but it hopefully provides to those interested in programming expressions a bit of insight into how expressions are built. For example, in the CAS view, type: CRPN(x^2-4*x) to obtain "x 2 ^ 4 x * - +" EDIT: Joe Horn pointed out to me that the "+" appeared extraneous -- which was also my initial reaction. As it turns out, the CAS treats subtraction as a sequence of negation followed by addition. So the "-" operator is actually the negation operator, which can be verified by typing f:=neg(x) in the CAS view, and extracting the operation using f[1]. I never really gave this much thought, but it is neat to see how expressions are handled on a lower level. Code: #pragma mode( separator(.,;) integer(h32) ) RE: Algebraic to RPN - ggauny@live.fr - 02-25-2017 12:19 PM Hello Han, I appriciate many your program. I have the same from Wiiliam C WICKES from HP 48 INSIGHTS (part II page 534) from 1992. I was failure to translate to HP PRIME and you give me ! Thank again. RE: Algebraic to RPN - compsystems - 02-25-2017 02:05 PM extract parts and operators with [] acts as the PART command, please create a second version with PART cmd PHP Code: (x^2-4*x)[1] → '+' RE: Algebraic to RPN - Han - 02-25-2017 03:15 PM (02-25-2017 02:05 PM)compsystems Wrote: extract parts and operators with [] acts as the PART command, please create a second version with PART cmd I am not sure why you would want a version using the part command, but here: Code: #pragma mode( separator(.,;) integer(h32) ) RE: Algebraic to RPN - compsystems - 02-26-2017 02:59 PM userrpl PHP Code: « 0 0 { } -> EXPRESSION XPART NPARTS OPERATOR OBJECTS Hi. The code with PART CMD, is simplified to the following, also rename some variables to auto-document the code. PHP Code: #pragma mode( separator(.,;) integer(h32) ) convToRPN2(x^3); returns "x 3 ^" /////////////// Running step by step convToRPN2(x^3);, for those who want to see the conversion of x^3 to RPN more info on RPN https://en.wikipedia.org/wiki/Reverse_Polish_notation PHP Code: expr1:=x^3; Or in a list of steps, without comments to copy and paste them in the history view PHP Code: expr1:=x^3; rpnStr:=""; oper:=""; partn:=""; oper:=part(expr1,0); parts:=part(expr1); partJ:=1; expr2:=part(expr1,partJ); partn:=string(expr2); rpnStr:=rpnStr + partn; partJ:=2; expr2:=part(expr1,partJ); partn:=string(expr2); rpnStr:=rpnStr + " ";rpnStr:=rpnStr + partn; rpnStr:=rpnStr + " " + oper; returns PHP Code: [ x^3,"","","","^",2,1,x,"x","x",2,3,"3","x ","x 3","x 3 ^"] RE: Algebraic to RPN - Komanguy - 07-28-2018 08:33 PM Thanks for this useful program. I’m learning lots of things. RE: Algebraic to RPN - tcab - 07-29-2018 07:46 AM @compsystems - nice skin on your virtual Prime - is that available for download anywhere? RE: Algebraic to RPN - Joe Horn - 07-29-2018 12:10 PM (07-29-2018 07:46 AM)tcab Wrote: @compsystems - nice skin on your virtual Prime - is that available for download anywhere? It's here: http://www.hpmuseum.org/forum/thread-7622-post-66998.html#pid66998 |