Program BitStringFlick;

 

{Tommy Luo}

{9 April 2010}

{Jr-5}

{Contest 4}

{Enloe High}

{Potter}

 

Uses crt;

 

Type

    Joey = array [1..2] of string;

 

Var

    Structure: string;

    Y: integer;

 

Procedure PopStack(var Structure: string; Many: integer);

    Begin

         Delete(Structure, Length(Structure) - Many + 1, Many);

    End;

 

Procedure PopQueue(var Structure: string; Many: integer);

    Begin

         Delete(Structure, 1, Many);

    End;

 

Procedure Push(var Structure: string; Cur: char);

    Begin

         Structure:= Structure + Cur;

    End;

 

Procedure Duplicate(var Structure: string; Many: integer);

    Var

         Hold1: string;

    Begin

         Hold1:= Copy(Structure, 1, Many);

         Structure:= Structure + Hold1;

    End;

 

Procedure SwapStack(var Structure: string; Many: integer);

    Var

         Hold2, Hold3: string;

    Begin

         Hold2:= Copy(Structure, Length(Structure) - Many + 1, Many);

         Hold3:= Copy(Structure, 1, Many);

         Delete(Structure, Length(Structure) - Many + 1, Many);

         Delete(Structure, 1, Many);

         Structure:= Hold2 + Structure + Hold3;

    End;

 

Procedure SwapQueue(var Structure: string; Many: integer);

    Var

         Hold4, Hold5: string;

    Begin

         Hold4:= Copy(Structure, Length(Structure) - Many + 1, Many);

         Hold5:= Copy(Structure, 1, Many);

         Delete(Structure, Length(Structure) - Many + 1, Many);

         Delete(Structure, 1, Many);

         Structure:= Hold5 + Structure + Hold4;

    End;

 

Procedure Outs(Structure: string; Many: integer; Kind: char);

    Var

         Display: string;

         X: integer;

    Begin

         Display:= Structure;

         WriteLN;

         If Kind = 'Q' then

              Begin

                   For X:= 1 to Many do

                        Begin

                             Write(Display[X], ', ');

                        End;

              End

         Else if Kind = 'S' then

              Begin

                   For X:= 1 to (Length(Display) - Many) do

                        Begin

                             Delete(Display, 1, 1);

                        End;

                   For X:= 1 to Many do

                        Begin

                             Write(Display[X], ', ');

                        End;

              End;

    End;

 

Procedure Choose(var Structure: string);

    Var

         Input, Choice, Hold: string;

         A, B, C, Many, Done: integer;

         Cur, Kind: char;

         Parts: Joey;

    Begin

         Done:= 0;

         Structure:= 'ABCDE';

         WriteLN('Is this is stack or a queue? S/Q');

         ReadLN(Kind);

         Kind:= Upcase(Kind);

         Many:= 0;

         Choice:= ' ';

         WriteLN('What to do?');

         WriteLN('POP / PSH / DUP / SWP / SWH / PRT');

         Repeat

              Begin

                   Input:= ' ';

                   Choice:= ' ';

                   Many:= 0;

                   ReadLN(Input);

                   Input:= Input + ' ';

                   B:= 0;

                   C:= 1;

                   For A:= 1 to Length(Input) do

                        Begin

                             If (Input[A] = ' ') then

                                  Begin

                                       B:= B+1;

                                       Parts[B]:= Copy(Input, C, A-C);

                                       C:= A+1;

                                  End;

                        End;

                   Choice:= Parts[1];

                   If Choice = 'POP' then

                        Begin

                             Val(Parts[2], Many, Many);

                             If Kind = 'S' then

                                  PopStack(Structure, Many)

                             Else if Kind = 'Q' then

                                  PopQueue(Structure, Many);

                        End

                   Else if Choice = 'PSH' then

                        Begin

                             Hold:= Parts[2];

                             Cur:= Hold[1];

                             Cur:= Upcase(Cur);

                             Push(Structure, Cur);

                        End

                   Else if Choice = 'DUP' then

                        Begin

                             Val(Parts[2], Many, Many);

                             Duplicate(Structure, Many);

                        End

                   Else if Choice = 'SWP' then

                        Begin

                             Val(Parts[2], Many, Many);

                             If Kind = 'S' then

                                  SwapStack(Structure, Many)

                             Else if Kind = 'Q' then

                                  SwapQueue(Structure, Many);

                        End

                   Else if Choice = 'SWH' then

                        Begin

                             If Kind = 'S' then

                                  Kind:= 'Q'

                             Else if Kind = 'Q' then

                                  Kind:= 'S';

                        End

                   Else if Choice = 'PRT' then

                        Begin

                             Val(Parts[2], Many, Many);

                             Outs(Structure, Many, Kind);

                             WriteLN;

                        End;

              End;

         Until Choice = 'PRT';

    End;

 

Begin

    Clrscr;

    For Y:= 1 to 5 do

         Choose(Structure);

    ReadLN;

End.