Announcement

Collapse
No announcement yet.

For you Pascal Programming wizards.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Re: For you Pascal Programming wizards.

    Wow, is that hard to follow without indentation.

    All I have to say is that recursion is your friend. Use it. (Maybe you did and I just missed it.)
    Pat
    Driver Found: Camber Wanted

    Comment


    • #17
      Re: For you Pascal Programming wizards.

      Originally posted by QuA
      Ok here is the code. Not working yet. Any tips?


      Program Calculate (input, test,output);
      {************************************************* **************************
      Input (keyboard) The equations obtained through the file 'test'.

      Purpose The purpose of this program is to accept equations from
      a file outside of the program and output the results.

      Output (Screen) Prompts the user, shows the result of the equation obtained
      by the file 'test' and outputs the equation simplified into a
      single term.
      ************************************************** *************************}

      CONST
      stringlength = 255;

      TYPE
      string1 = PACKED ARRAY[1..stringlength] of char;
      string2 = ARRAY[1..stringlength] of real;
      VAR
      x1 : real; {Variable used for operations}
      x2 : real; {Variable used for operations}
      ans : real; {Temporary slot that contains a simplied area of
      the equation}
      equation : string1; {Defines equation as a packed array}
      number : string2; {Stores the numerical values into an array}
      op : string1; {Stores the operator into an array}
      simeq : string2; {The equation after simplification}
      counter : integer; {The cell number within the array}
      counter2 : integer;
      test : text;


      procedure Add (x1, x2 : real ); {Procedure used for addition}

      BEGIN
      ans := x1 + x2
      END; { Add }

      procedure Sub (x1, x2 : real ); {Procedure used for subtraction}

      BEGIN
      ans := x1-x2
      END; { Sub }

      procedure Mul (x1, x2 : real ); {Procedure used for multiplication}

      BEGIN
      ans := x1*x2
      END; { Mul }

      procedure Divi (x1, x2 : real ); {Procedure used for division, if the divisor
      is 0, the procedure will repeatedly ask for a
      new possible divisor that isn't 0}

      BEGIN
      ans:= x1/x2
      end; { Divi }

      BEGIN
      reset(test);
      while not EOF(test) do
      BEGIN
      read(test, equation); {Copies the lines/equations from the textfile
      into the equation array}
      counter := 0;
      for counter := 0 to stringlength do {begins a loop that checks each cell
      to
      confirm if it is either a number or operator}
      BEGIN
      case equation[counter] of {Isolates the numbers into a separate array}
      '0'..'9','.':
      BEGIN {The case if the cell is a number}
      number[counter] := ord(equation[counter]); {Stores numbers in its ASCII
      form}
      if ((equation[counter-1] >= char('0')) and (equation[counter-1] <=
      char('9'))) then
      BEGIN
      number[counter] := (number[counter] + (number[counter-1] * 10));
      {If the value of a number is greater than 9, it will add the
      values together}
      number[counter-1] := 0;
      {Clears out cell before to ensure that no number is counted
      twice}
      END;
      END;
      '+','-','/','*' :
      BEGIN {Case if the cell is an operator sign}
      op[counter] := equation[counter];
      END;
      END; { End of the case }
      END;

      counter := 0; {Restarts the counter to 0}
      for counter:= 1 to stringlength do {Searching for brackets}
      BEGIN
      if op[counter] = '(' then
      counter2:= counter;
      repeat {Once a open bracket is found, the program looks
      for the}
      counter2:= counter2 + 1; {Closest operator}
      until op[counter2] <> ' ';

      repeat {Searches for the closest value to the left
      of
      the operator}
      counter2:= counter2 - 1;
      x1 := number[counter2];
      until (number[counter2] <> 0 ) or (counter2 = 0) ;
      number[counter2] := 0; {Resets the cell that contains the value to
      zero}

      counter2:= counter; {Sets counter2 back to where the operator
      is}

      repeat {Algorithm used to find the closest value
      after
      the operator}
      counter2:= counter2 + 1;
      x2:= number[counter2];
      until (number[counter2] <> 0 ) or (counter2 = stringlength);
      number[counter2] := 0; {Resets the cell that contains the value to
      zero}

      case op[counter] of {Performs an operation based on the
      operator}
      '+' : Add(x1,x2);
      '-' : Sub(x1,x2);
      '*' : Mul(x1,x2);
      '/' : Divi(x1,x2);
      END; { case }

      simeq[counter] := ans; {Saves the simplified value and stores it
      into another
      separate array}
      END; {Ends the search for open brackets
      because there are
      none left}

      for counter := 1 to stringlength do {This counter searches for the next
      highest prioritized
      operator}
      if (op[counter] = '*') or (op[counter] = '/') then
      BEGIN
      counter2 := counter; {Sets counter2 to the point of a '*' or
      '/'}

      repeat {Searches for the closest value to the
      left of
      the operator, limiting the search from that point
      to the very begining}
      counter2:= (counter2 - 1);
      x1 := number[counter2];
      until ((number[counter2] <> 0 ) or (counter2 = 1));
      number[counter2] := 0;

      counter2:= counter;

      repeat {Searches for the closest value to the
      right of
      the operator, limiting the search from that point
      to the very end}
      counter2:= counter2 + 1;
      x2:= number[counter2];
      until ((number[counter2] <> 0 ) or (counter2 = stringlength));
      number[counter2] := 0;

      case op[counter] of {Performs the operation of either
      multiply or divide}
      '*' : Mul(x1,x2);
      '/' : Divi(x1,x2);
      End; { case }

      simeq[counter] := ans; {Stores the simplified answer into 4th
      array for simple
      addition in the end}
      END; {End of the multiply and divide}


      for counter := 1 to stringlength do {Searches the remaining operators and
      performs them}
      if (op[counter] = '+') or (op[counter] = '-') then
      BEGIN
      counter2 := counter;

      repeat
      counter2:= counter2 - 1;
      x1:= number[counter2];
      until ((number[counter2] <> 0 ) or (counter2 = 1));
      number[counter2] := 0;

      counter2:= counter;

      repeat
      counter2:= counter2 + 1;
      x2:= number[counter2];
      until (number[counter2] <> 0) or (counter2 = stringlength);
      number[counter2] := 0;

      case op[counter] of
      '+' : Add(x1,x2);
      '-' : Sub(x1,x2);
      End; { case }

      simeq[counter]:= ans;
      END;



      writeln('solution is', ans:7:2);


      for counter:= 1 to stringlength do
      BEGIN
      x1 := simeq[counter];
      counter2:=counter;
      repeat
      counter2 := counter2 + 1;
      x2 := simeq[counter2];
      until (number[counter2] <> 0 ) or (counter2 = stringlength);
      ans := (x1 + x2);
      x1 := ans;

      END;




      writeln('The solution to the equation is:', ans :7:2);
      END;
      END.

      The solution here is obvious...follow VWfolyfe's advice...seriously.

      Comment

      Working...
      X