in reply to Re^3: Reverse Polish Notation Question
in thread RPN Question
For a linear equation it could look like this (and yes, it took me more than 15 minutes...)
use strict; use warnings; my $equation = "(2*(4*x-8)+3*x)-(10*x+1-9)"; # " = 0" omitted $equation =~ s/x/\$_[0]/g; my $eval = sub { eval $equation }; my $val1 = $eval->(0); my $val2 = $eval->(1); die "No solution for this equation\n" unless $val1 - $val2; my $result = -$val1 / ($val2 - $val1); printf "Result is %.10f\n", $result;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Reverse Polish Notation Question
by Laurent_R (Canon) on May 02, 2015 at 22:13 UTC | |
|
Re^5: Reverse Polish Notation Question
by Laurent_R (Canon) on May 02, 2015 at 17:44 UTC |