Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Perl and maths

by sleepingsquirrel (Hermit)
on Feb 15, 2005 at 19:43 UTC ( #431307=note: print w/replies, xml ) Need Help??

in reply to Re: Perl and maths
in thread Perl and maths

Here's a modification to my code above which handles adding fractions and printing out the partial results. Finishing it off is left as an exercise to the reader...


#!/usr/bin/perl -w # # add fractions and display partial results # use re 'eval'; #use recursive regex for parsing use strict; our $num = qr{\d+(?:/\d+)?}; our $op = qr{[+*/\-]}; # an expression is a fraction or a pair of expressions separated by # an operator, enclosed in parens. our $exp; $exp = qr{$num|\(\s*(??{$exp})\s*$op\s*(??{$exp})\s*\)}s; # Something to test the evaluator with my @tests = ("(1/2+2/3)","(1/2+(2/3+4/5))","((5/3+7/13)+(5/2+3/7))"); for my $t (@tests) { print "$t\n"; print simplify($t,"",""); print "\n\n"; } sub simplify { my ($e, $pre, $post) = @_; if($e =~ /^($num)$/s) { return $1; } elsif ($e =~ /^\(($exp)\+($exp)\)$/s) { my ($l, $r) = ($1,$2); my $left = simplify($l,"",""); print "$pre($left+$r)$post\n" if $l!~m/^$num$/; my $right = simplify($r,"($left+",")"); print "$pre($left+$right)$post\n" if $r!~m/^$num$/; my $sum = add_frac($left, $right); return "$sum"; } elsif ($e =~ /^\(($exp)-($exp)\)$/s) { #subtraction } elsif ($e =~ /^\(($exp)\*($exp)\)$/s) { #multiplication } elsif ($e =~ /^\(($exp)\/($exp)\)$/s) { #division } else { die "Syntax error\n"; } } sub add_frac { my ($l, $r) = @_; my ($l_num, $l_denom) = $l =~ m{(\d+)/(\d+)}; my ($r_num, $r_denom) = $r =~ m{(\d+)/(\d+)}; my $denom = $l_denom * $r_denom; my $ans = $l_num * $r_denom + $r_num * $l_denom; return "$ans/$denom"; }

-- All code is 100% tested and functional unless otherwise noted.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://431307]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2016-10-21 20:19 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (289 votes). Check out past polls.