Announcement

Collapse
No announcement yet.

For you Pascal Programming wizards.

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

  • For you Pascal Programming wizards.

    Procedures, Arrays and File Input

    Proper use of procedures and parameters to create a well written modular program.

    The operators DIV and MOD are now not required.

    Again reals in the form of exponential notation need not be considered.

    Operator precedence must now be taken into account as well as the bracketing of expressions.

    Your program must use arrays as the main storage medium for the equation.
    The complete arithmetic expression will be read from a file.

    The file may contain a number of equations.

    An equation will not cover more than one line and may be assumed to be entirely valid.

    For example the file might contain the following:
    4 + 2 * 5 + 2
    56 - 12 / 3 + 1.1 * 5

    The first equation will be completely evaluated and the final answer determined before the next equation is read and evaluated.
    Everytime an intermediate result is calculated the corresponding components of that calculation are removed from the array(s) and replaced by the single answer. For example the second step for the first equation would have the equation stored as 4 + 10 + 2.


    How hard is this to do?
    Last edited by QuA; 11-04-2004, 02:19 AM.
    2008 BMW 135i | M Package | JB3 | DCI | BMW Performance Exhaust

  • #2
    Re: For you Pascal Programming wizards.

    Originally posted by QuA

    How hard is this to do?
    How good are you?

    It wouldn't be hard for me. However I haven't programmed using Pascal since 1995.
    KR
    Porsche 991 Carrera S

    Comment


    • #3
      Re: For you Pascal Programming wizards.

      Originally posted by Kor
      How good are you?

      It wouldn't be hard for me. However I haven't programmed using Pascal since 1995.
      Also, it may be useful to convert from infix to postfix notation before solving calculator-type problems.
      KR
      Porsche 991 Carrera S

      Comment


      • #4
        Re: For you Pascal Programming wizards.

        I think I had to do a similar assignment - wasn't too bad.
        Geoff
        Fear is the element that unites all losers.

        Comment


        • #5
          Re: For you Pascal Programming wizards.

          It is not worth it. Drop out of school and open a gun store in the middle of the ghetto.

          Comment


          • #6
            Re: For you Pascal Programming wizards.

            Originally posted by VWfolyfe
            It is not worth it. Drop out of school and open a gun store in the middle of the ghetto.
            That's hardly a fine example for you to present youth. While I agree that schooling is a tremendous waste of resources, guns and violence are never an answer to achievements. A more rewarding path would be networking and fund gathering to seek public office on the local government level where a difference can be made by your actions.

            Comment


            • #7
              Re: For you Pascal Programming wizards.

              Originally posted by Kor
              Also, it may be useful to convert from infix to postfix notation before solving calculator-type problems.
              Pascal.. RPN.. Flashback.. Brain hurt.

              Comment


              • #8
                Re: For you Pascal Programming wizards.

                Originally posted by Reverend_Kalunda
                That's hardly a fine example for you to present youth. While I agree that schooling is a tremendous waste of resources, guns and violence are never an answer to achievements. A more rewarding path would be networking and fund gathering to seek public office on the local government level where a difference can be made by your actions.
                Yeah, he could run for president. That way he could kill thousands of people before anyone gives a ****. Way to steer him away from guns and violence.

                Comment


                • #9
                  Re: For you Pascal Programming wizards.

                  Originally posted by VWfolyfe
                  Yeah, he could run for president. That way he could kill thousands of people before anyone gives a ****. Way to steer him away from guns and violence.

                  LOL

                  ps. Pascal? I'm suprised they still teach that. I took it in first year engg, but after that it was changed to a java course.
                  1.8T
                  Not quite stock anymore...

                  Comment


                  • #10
                    Re: For you Pascal Programming wizards.

                    Yeah i take Java next semester. Pascal is way too old to be taught =)
                    2008 BMW 135i | M Package | JB3 | DCI | BMW Performance Exhaust

                    Comment


                    • #11
                      Re: For you Pascal Programming wizards.

                      Originally posted by VWfolyfe
                      It is not worth it. Drop out of school and open a gun store in the middle of the ghetto.
                      We don't have gettos in Calgary, we just have the NorthEast
                      Name: Brent
                      His: '04 TDI Golf Mods: None If it's smoken it ain't broken
                      Family: '15 Jetta Sportwagon
                      Fun car: '92 Blue Karmann Crabby Cabby Mods: Coils, front and rear swaybars, LED interior lights and some other old things.

                      Comment


                      • #12
                        Re: For you Pascal Programming wizards.

                        Originally posted by Stonewall
                        We don't have gettos in Calgary, we just have the NorthEast
                        You could move to Detroit. All they have is ghettos there.

                        Comment


                        • #13
                          Re: For you Pascal Programming wizards.

                          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.
                          2008 BMW 135i | M Package | JB3 | DCI | BMW Performance Exhaust

                          Comment


                          • #14
                            Re: For you Pascal Programming wizards.

                            Damn characters just repeat.
                            2008 BMW 135i | M Package | JB3 | DCI | BMW Performance Exhaust

                            Comment


                            • #15
                              Re: For you Pascal Programming wizards.

                              right

                              Comment

                              Working...
                              X