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.
type tStudent = record
last_name: shortstring;
first_name: shortstring;
grade: integer
end;
Now, if you want to store information about a student, you have all that stuff grouped together under one name:
var students: array[ 1..35 ] of tStudent;
You can access information about a particular student:
writeln(
'Student 12: ',
students[ 12 ].first_name,
' ',
students[ 12 ].last_name,
' has a numeric grade of ',
students[ 12 ].grade,
'.'
);
You can copy records just like simple data types.
var a_student, another_student: tStudent;
...
if a_student.grade <> another_student.grade
then writeln( 'very possible...' );
a_student := another_student;
if a_student.grade <> another_student.grade
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:
with a_student do
begin
first_name := 'Jenny';
last_name := 'O''Hara';
grade := 95
end;
{ 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:
type
tCardSuite = (Spades, Hearts, Clubs, Diamonds);
tCardRank = 1..12;
tCard = record
suite: tCardSuite;
rank: tCardRank
end;
Now, you can create a function that creates a card with rank and suite.
function create_a_card( rank: tCardRank; suite: tCardSuite ): tCard;
var r: tCard;
begin
r.rank := rank;
r.suite := suite;
create_a_card := r
end;
And you can use it:
var card: tCard;
begin
card := create_a_card( 1, Spades );
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.
type
tEquationItemType = (
number, addition, subtraction, multiplication, division,
open_parentheses, close_parentheses
);
tEquationItem = record
itemType: tEquationItemType; // number? +? -? *? /?
value: real // if itemType = number, this is the number
end;
tEquation = array[ 1..100 ] of tEquationItem;
var
equation: tEquation;
Now, you can convert a string like
5*(12+3) into an equation:
with equation[ 1 ] do begin itemType := number; value := 5.0 end;
equation[ 2 ].itemType := multiplication;
equation[ 3 ].itemType := open_parentheses;
with equation[ 4 ] do begin itemType := number; value := 12.0 end;
{ 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.