🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #1511

Re: expresion regular para comprobar Expresion matematicas

#1511
From: F
Date: Mon, 10 Oct 2005 08:43
109 lines
3713 bytes

"F" <esnewsfQuItAeStO@EsToCaMbIeNyahoo.es> escribi� en el mensaje 
news:ccN1f.4441850$I96.7525036@telenews.teleline.es...
> Hola, necesito comprobar que una expresion matematica este bien formada.
>
> por ejemplo 1+2+3 esta bien 1*/3 no
>
> Para ello, cuando no hay parentesis es bastante facil

...

Muchas gracias a todos por las contestaciones, me ha habeis dichos cosas muy 
interesantes, que mirare con mas detenimiento.  (ma�ana voy a intenar 
hacerlo con perlredescent).

finalmente me decidi por implementarlo manualmente, como un especie de 
automata a pila, lo dejo por si a alguien le sirve, falta implementar las 
funciones (sin, cos, tan), pero es algo bastante facil, similar a los 
parentesis.

Un saludo y gracias




$num   = "-?\\d*\\.?\\d+";
$op    = '\*|\/|\%|\+|\-';
$TRUE  = 1;
$FALSE = 0;

sub verificar {
    $expr = shift();

    #Inicializo varables
    $expr2    = $expr;
    $correcta = $TRUE;
    $q        = 0;
    @pila     = ("x");

    # 0--> S
    # 1--> N
    # 3 --> final
    # S::= num$|numN|(S
    # N::= $opS|)$|)N
    #
    #

    print "$expr2: ";
    while ( $q != 3 ) {
        print "q$q ";
        if ( ( $q == 0 ) && ( $expr2 =~ s/^\s*$num\s*$//i ) ) {
            $q = 3;
            last;
        }
        elsif ( ( $q == 0 ) && ( $expr2 =~ s/^\s*$num\s*//i ) ) {
            $q = 1;
        }
        elsif ( ( $q == 0 ) && ( $expr2 =~ s/^\s*\(\s*// ) ) {
            $q = 1;
           unshift @pila, "(";
        }
        elsif ( ( $q == 1 ) && ( $expr2 =~ s/^\s*$op\s*// ) ) {
            $q = 0;
        }
        elsif ( ( $q == 1 ) && ( $expr2 =~ s/^\s*\)\s*$// ) ) {
            $p = shift(@pila);
            $q = 3;
            if ( $p ne "(" ) {
              print "$p ";
                $correcta = $FALSE;
                last;
            }
        }
        elsif ( ( $q == 1 ) && ( $expr2 =~ s/^\s*\)\s*// ) ) {
            $p = shift(@pila);
            $q = 1;
            if ( $p ne "(" ) {
                print "$p ";
                $correcta = $FALSE;
                $q        = 3;
                last;
            }
        }
        else {
            $q        = 3;
            $correcta = $FALSE;
            last;
        }

    }

    $p=shift(@pila);
    print "$p \n";
    if ($correcta eq $FALSE || $p ne "x"){
      return 0;
    }else {
      return 1;
    }

}

print "introduce la expresion aritmetica: ";
$expr=<STDIN>;
chomp($expr);

$compro = verificar($expr);

print "La comprobacion es $compro\n";
Technical Details
Message-ID: <yQp2f.4477026$I96.7806198@telenews.teleline.es>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!nsnmpen1-lo.nuria.telefonica-data.net!telenews.teleline.es!not-for-mail
References: <ccN1f.4441850$I96.7525036@telenews.teleline.es>

Show Full Headers
Complete Article Headers
X-Google-Thread: f86c8,9615da75e63f6370
X-Google-Attributes: gidf86c8,public
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!nsnmpen1-lo.nuria.telefonica-data.net!telenews.teleline.es!not-for-mail
X-Trace-PostClient-IP: 82.159.102.130
From: "F" <esnewsfQuItAeStO@EsToCaMbIeNyahoo.es>
Newsgroups: es.comp.os.linux.programacion
References: <ccN1f.4441850$I96.7525036@telenews.teleline.es>
Subject: Re: expresion regular para comprobar Expresion matematicas
Lines: 111
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
Message-ID: <yQp2f.4477026$I96.7806198@telenews.teleline.es>
Date: Mon, 10 Oct 2005 08:43:10 GMT
NNTP-Posting-Host: 10.20.31.4
X-Complaints-To: usenet@teleline.es
X-Trace: telenews.teleline.es 1128933790 10.20.31.4 (Mon, 10 Oct 2005 10:43:10 MET DST)
NNTP-Posting-Date: Mon, 10 Oct 2005 10:43:10 MET DST
Organization: Terra Networks