Need help again 
Author Message
 Need help again

Hi ! First let me thank to all of you who answer my last questions ,
my problem now is with this little program wich evaluates postfix
expressions , what I`ve done so far does the job but I can`t figure
out how to make it work with more than 2 integers for input , for
example if my input is 4 5 + , it gives me 9 , but I need it to be
able to evaluate for example 4 5 7 + -. The unit I`m using, which is
an array implementation of the stack , I think is ok because it was
sent to me by the school , so I don`t think it is necessary to posted
it too.
The code follows:

program stacks (input,output);

        uses stackops;
type    thearray=array[1..30] of char;
var     theinput:char;
        op: char;
        item,item1,item2,item3,theresult,code:integer;
        S:stacktype;
        success,string1f,string2f,string3f:boolean;
        string1,string2,string3:string[8];
        arrayvar:thearray;
        i,j:integer;

begin

        createstack(S);
        writeln( ' Type your postfix expression ' );
              while not eoln do begin {puts string into the array}
                         read(theinput);
                         i:=i+1;
                         arrayvar[i]:=theinput;
              end;

        i:=1;{we advance to the first character of the string}
while (arrayvar[i]<>#0)  and not ( arrayvar[i] in ['+','-','*','/'] )
do begin {while there are characters}

                         if arrayvar[i]=' ' then begin
                                  while arrayvar[i]=' ' do begin
                                      i:=i+1;
                                  end;
                         end;{advances until the char is not a space}

   if not ( arrayvar[i] in ['+','-','*','/'] )   then begin
    string1f:=false;
          while arrayvar[i]<>' ' do begin{it will stop when it finds a
space}
                 string1:=string1+arrayvar[i];
                 i:=i+1;
                 if string1<>'' then string1f:=true;
          end;

          val(string1,item1,code); {now item1 is our first integer}

          if arrayvar[i]=' ' then begin

            string2f:=false;
                      while arrayvar[i]=' ' do begin
                                  i:=i+1;{until we don`t have more
spaces}
                      end;

                      while arrayvar[i]<>' ' do begin
                                    string2:=string2+arrayvar[i];
                                    i:=i+1;
                                    if string2<>'' then string2f:=true
;
                     end;
             val(string2,item2,code);
          end;

   end;{if}
end;{while}
  if string1f=true then
  push(S,item1,success) ;
  if string2f=true then
  push(S,item2,success);

    if (arrayvar[i] in ['+','-','/','*']) then begin
       op := theinput;

       getstacktop(S,item1,success);
       pop(S,success);
       getstacktop(S,item2,success);
       pop(S,success);

                  case op of
                            '-': theresult:= (item1-item2);
                            '+': theresult:=(item1+item2);
                            '/': theresult:=(item1 div item2);
                            '*': theresult:=(item1*item2);
                  end ;
        push (S,theresult,success);

    end; {if}

    pop(S,success);
    write(theresult);
    writeln;

end.{main}

Thanks again in advance: LB



Wed, 18 Jun 1902 08:00:00 GMT  
 Need help again

----=_334fc8e47110008201cb1e99.MFSBCHJLHS
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Quote:

>Hi ! First let me thank to all of you who answer my last questions ,
>my problem now is with this little program wich evaluates postfix
>expressions , what I`ve done so far does the job but I can`t figure
>out how to make it work with more than 2 integers for input , for
>example if my input is 4 5 + , it gives me 9 , but I need it to be
>able to evaluate for example 4 5 7 + -.

It may not have been good form to proceed your punctuation
with an extra space, but at least that didn't make it
anywhere near as unreadable as your code.  A little white
space and consistency with your indentation would do wonders
toward making it more legible.

I wouldn't be surprised to learn that you haven't received
replies simply because no one wants to be bothered with the
tedious chore of formatting your code so that it can be
understood.

Rather than perform this chore myself, I'm going to present
a different, and somewhat more structured solution.  There
is enough difference between my stack and the one you must
use to prevent you from using the code "as-is".  However,
I'm sure you'll have no problem incorporating key portions
into your program.

    ...red

----=_334fc8e47110008201cb1e99.MFSBCHJLHS
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

PROGRAM Infix(input, output);

{ ----------------------------------------------------------- }
PROCEDURE Abort(const Msg: String);
BEGIN
    Writeln;
    Writeln('*** ERROR: ', Msg, ' ***');
    Halt(1);
END;

{ ----------------------------------------------------------- }
TYPE  tToken = (Error,Stop,Number,Add,Subt,Mult,Divide);

PROCEDURE SkipSpaces(VAR S: String);
BEGIN
    While (s <> '') and (s[1] = ' ') Do
       Delete(s, 1, 1);
END;

FUNCTION NextToken(VAR S: String; VAR Value: Integer): tToken;
VAR i,j: Integer;
BEGIN
    NextToken := Error;   { assume failure }
    SkipSpaces(S);
    i := 1;
    If s = '' Then
       NextToken := Stop
    Else
    Case s[1] of
       '+': NextToken := Add;
       '-': NextToken := Subt;
       '*': NextToken := Mult;
       '/': NextToken := Divide;
       '0'..'9': Begin
           While (i < Length(s)) and (s[Succ(i)] in ['0'..'9']) Do
              Inc(i);
           Val(Copy(s, 1, i), Value, j);
           If j = 0 Then
              NextToken := Number
       End;
    End{Case};
    Delete(s, 1, i);
END;

{ ----------------------------------------------------------- }
CONST SP: Integer = 0;
      MxSTACK = 100;
VAR   Stack: Array[1..MxSTACK] of Integer;

FUNCTION IsEmptyStack: Boolean;
BEGIN
    IsEmptyStack := (SP = 0);
END;

PROCEDURE Push(Value: Integer);
BEGIN
    If SP < MxSTACK Then Begin
       Inc(SP);
       Stack[SP] := Value;
    End Else Abort('Stack overflow');
END;

FUNCTION Pop: Integer;
BEGIN
    If SP > 0 Then Begin
       Pop := Stack[SP];
       Dec(SP);
    End Else Abort('Stack underflow');
END;

{ ----------------------------------------------------------- }
PROCEDURE Calc;
VAR Value: Integer;
    Inp  : String;

BEGIN
    Writeln('Type your postfix expression: ');
    Readln(Inp);
    REPEAT
       Case NextToken(Inp, Value) Of
          Number: Push(Value);
          Add   : Push(  Pop + Pop );
          Subt  : Begin Value := Pop; Push(Pop - Value); End;
          Mult  : Push(Pop * Pop);
          Divide: Begin Value := Pop; Push(Pop div Value); End;
          Stop  : Begin Writeln('ANS: ', Pop);
                        If IsEmptyStack Then Exit;
                        Abort('Stack not cleared');
                  End;
          Error  : Abort('Invalid input');
       End{Case};
    UNTIL FALSE;
END;

{ ----------------------------------------------------------- }
BEGIN
    Calc;
END.

----=_334fc8e47110008201cb1e99.MFSBCHJLHS--



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Need Help again <* urgent *>

2. Need Help again <* urgent *>

3. I NEED HELP AGAIN!!

4. Basic help needed again.

5. Basic help needed again.

6. Hello...Me again...I need help with pascal

7. Help (again) needed on calculating SYSTEM DATE

8. Need bibliography again

9. please help again on BLOB.

10. help again

11. Help : deleting indexes and rebuilding again - problem

12. Help!!mastermind again!!!

 

 
Powered by phpBB® Forum Software