Cod sursa(job #1615563)

Utilizator iondodon1998Dodon Ion iondodon1998 Data 26 februarie 2016 17:54:35
Problema Evaluarea unei expresii Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 3.02 kb
Program evaluare;
type tabel=array[0..100000] of char;
var     s:tabel;
        f1,f2:text;
        i,r:longint;

Procedure citiresir;
begin
    i:=0;
    while not eoln(f1) do
        begin
          i:=i+1;
          read(f1,s[i]);
        end;

    s[0]:='(';
    s[i+1]:=')';
end;

Function eval():longint;
Forward;


Function numar():longint;
begin
numar:=0;
while (s[i]>='0') and (s[i]<='9') do
        begin
        numar:=numar*10+ord(s[i])-48;
        i:=i+1;
        end;
while (s[i]='*') or (s[i]='/') do
begin
        if s[i]='*' then
        begin
        i:=i+1;
        if s[i]='(' then
                numar:=numar*eval() else
                        numar:=numar*numar();
        end else
                if s[i]='/' then
                begin
                i:=i+1;
                if s[i]='(' then
                        numar:=numar div eval() else
                                numar:=numar div numar();
                end;
if s[i]=')' then
i:=i+1;
end;
end;


Function eval():longint;
begin
eval:=0;
i:=i+1;
if s[i]='(' then
        eval:=eval+eval() else
        begin
        eval:=eval+numar();
        while (s[i]='+') or (s[i]='-') or (s[i]='*') or (s[i]='/') do
        begin
                if s[i]='+' then
                        begin
                        i:=i+1;
                        if s[i]='(' then
                                eval:=eval+eval() else
                                        eval:=eval+numar();
                        end else
                                if s[i]='-' then
                                begin
                                i:=i+1;
                                if s[i]='(' then
                                        eval:=eval-numar() else
                                                eval:=eval-numar();
                                end else
                                        if s[i]='*' then
                                        begin
                                        i:=i+1;
                                        if s[i]='(' then
                                                eval:=eval*eval() else
                                                        eval:=eval*numar();
                                        end else
                                                if s[i]='/' then
                                                begin
                                                i:=i+1;
                                                if s[i]='(' then
                                                        eval:=eval div eval() else
                                                                eval:=eval div numar();
                                                end;
        if s[i]=')' then
                i:=i+1;
        end;
        end;
i:=i+1;
end;


begin
    assign(f1,'evaluare.in'); reset(f1);
    assign(f2,'evaluare.out'); rewrite(f2);


    citiresir;

    i:=0;
    write(f2,eval());


    close(f1);
    close(f2);
end.