RSS Forums RSS
Please support our Pascal and Delphi advertiser: Programming Forums

Pascal calculator program

Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,884
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 197
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #17  
Nov 18th, 2007
OK. A record is just a way to keep different kinds of data together.

Normally, you have simple types, like integer, char, real, etc.
You can have an array of things, but they are all the same kind (or type) of thing: array of integer, array of char, array of real.

Sometimes you need to store related things together. Say, a student. There's the student's name, current grade, etc.
  1. type tStudent = record
  2. last_name: shortstring;
  3. first_name: shortstring;
  4. grade: integer
  5. end;

Now, if you want to store information about a student, you have all that stuff grouped together under one name:
  1. var students: array[ 1..35 ] of tStudent;

You can access information about a particular student:
  1. writeln(
  2. 'Student 12: ',
  3. students[ 12 ].first_name,
  4. ' ',
  5. students[ 12 ].last_name,
  6. ' has a numeric grade of ',
  7. students[ 12 ].grade,
  8. '.'
  9. );

You can copy records just like simple data types.
  1. var a_student, another_student: tStudent;
  2. ...
  3. if a_student.grade <> another_student.grade
  4. then writeln( 'very possible...' );
  5.  
  6. a_student := another_student;
  7.  
  8. if a_student.grade <> another_student.grade
  9. then writeln( 'impossible!' );

You can use a with statement to get at all the elements of a record without having to repeat the record variable's name each time:
  1. with a_student do
  2. begin
  3. first_name := 'Jenny';
  4. last_name := 'O''Hara';
  5. grade := 95
  6. end;
  7. { Now a_student is Jenny O'Hara, who has an A+ }

An enumeration (which you should have learned already also) is just a bunch of names. It is stored as a number, but we don't usually care what those numbers are. The meaning is in the name. For example:
  1. type
  2. tCardSuite = (Spades, Hearts, Clubs, Diamonds);
  3. tCardRank = 1..12;
  4. tCard = record
  5. suite: tCardSuite;
  6. rank: tCardRank
  7. end;
Now, you can create a function that creates a card with rank and suite.
  1. function create_a_card( rank: tCardRank; suite: tCardSuite ): tCard;
  2. var r: tCard;
  3. begin
  4. r.rank := rank;
  5. r.suite := suite;
  6. create_a_card := r
  7. end;
And you can use it:
  1. var card: tCard;
  2. begin
  3. card := create_a_card( 1, Spades );
  4. end.

-
So, then, what I recommended is, since each type of thing in an equation is something different, you can't have an array of them. What you can have, though, is an array of the same thing, which stores different data depending on the type of thing it represents.
  1. type
  2. tEquationItemType = (
  3. number, addition, subtraction, multiplication, division,
  4. open_parentheses, close_parentheses
  5. );
  6. tEquationItem = record
  7. itemType: tEquationItemType; // number? +? -? *? /?
  8. value: real // if itemType = number, this is the number
  9. end;
  10. tEquation = array[ 1..100 ] of tEquationItem;
  11.  
  12. var
  13. equation: tEquation;

Now, you can convert a string like 5*(12+3) into an equation:
  1. with equation[ 1 ] do begin itemType := number; value := 5.0 end;
  2. equation[ 2 ].itemType := multiplication;
  3. equation[ 3 ].itemType := open_parentheses;
  4. with equation[ 4 ] do begin itemType := number; value := 12.0 end;
  5. { and so on }
Of course, you won't want to directly hardcode the equation like this, but you will read it from file and convert it in a similar manner. Use a loop to get lines from the file. In that loop, call a function to convert the line from the file into an equation. I named my version of this procedure string_to_equation:
procedure string_to_equation( s: string; var equ: tEquation );
The function itself will use a loop to get characters out of the string. For each character, if it is a number, set the next element of equ to think of itself as a number and store a value. If it is a plus sign, set the next element of equ to think of itself as a multiplication. Etc.

You will want to write a few other functions or procedures that act on the equation to test it, evaluate it (or part of it), etc.

Hope this helps.
Last edited by Duoas : Nov 18th, 2007 at 3:33 pm.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:42 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC