•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 456,234 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,785 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 6916 | Replies: 62
![]() |
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
An array is just a list of things all having the same type. An array of integer is:
Likewise, an array of char is:
So if your record's type is
You could have read this in your text book.
Good luck.
var a: array[ 1..100 ] of integer; (fixed-size array)var a: array of integer; (dynamic array)Likewise, an array of char is:
var str: array[ 1..100 ] of char; (fixed-size array)So if your record's type is
tElement you create an array as:var equation: array of tElement; (dynamic array)You could have read this in your text book.
Good luck.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
Something still isn't right, this is what I have
Do I have an error somewhere in my record or Array? I can't seem to get my equation into an array. Sorry I am new to programming.
Program Lesson9_Program(input, output);
Type
Str25 = String[25];
tEquation = Record
num : Str25;
add,subtract,multiply,divide : Str25;
end;
Var file1 : Text;
eq1 : String[255];
eq2 : string[255];
myEquation : tEquation;
myeqArray : array[1..50] of tEquation;
Begin
Assign(file1,'input.txt');
Reset(file1);
If (IOResult <> 0) then
Begin
Writeln('The file required to be opened is not found!');
Readln;
End Else
Begin
readln(file1, eq1);
Writeln('The first line of the file reads: ',eq1);
readln(file1, eq2);
writeln('The second line of the file reads: ',eq2);
End;
end.Do I have an error somewhere in my record or Array? I can't seem to get my equation into an array. Sorry I am new to programming.
Last edited by Gotovina7 : Nov 17th, 2007 at 6:43 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
You have no errors. However, I would be inclined to make a single enumeration type to tell me what the element is, as I suggested above.
Obviously, you can't just say
After reading it, all you have to do is convert it into your array.
If you can think out the process like this, then you can also draw it on a piece of paper. Think about the kinds of information you must have to do it: indices into your string, an index into your array, a way to tell numbers and various operators apart, etc. This is the reason I suggested a record type: because it can have a value that tells you what kind of thing it represents and another value to tell you what that thing is.
Also, you have no idea how many lines your file might have. It could be two, or two-hundred. You can't possibly do all those equations at once. Instead, do them one at a time: Read a line, convert it to your equation array, solve, print the answer. Read the next line, convert, solve, print. Rinse and repeat.
Hope this helps.
Obviously, you can't just say
readln( myEquation ); since the program has no idea how to read such a thing. You've already read a string that looks something like '1+20*3'.After reading it, all you have to do is convert it into your array.
•
•
•
•
The first character is a digit, and there is only one of them. So I take the first character and convert it into a number: 1. The first element of my array is a number = 1.
The second character is a plus sign. So it is an operator. The second element of my array is a plus operator.
The third and fourth characters are digits, so I convert them into a number: 20. The third element of my array is a number = 20.
The fifth character is an operator, so the fourth element of my array is the multiplication operator.
Etc.
Also, you have no idea how many lines your file might have. It could be two, or two-hundred. You can't possibly do all those equations at once. Instead, do them one at a time: Read a line, convert it to your equation array, solve, print the answer. Read the next line, convert, solve, print. Rinse and repeat.
Hope this helps.
Last edited by Duoas : Nov 17th, 2007 at 11:16 pm.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
You have no errors. However, I would be inclined to make a single enumeration type to tell me what the element is, as I suggested above.
What do you mean? I dont really understand what you are doing here
type tType = (number, add, subtract, multiply, ...);
type tElement = record element_type: tType; number: integer end; That second line confuses me. Records are a bit confusing. I read my textbook on them, but they are still confusing me. I am not really sure what my program is doing. Like, I can't understand the record part of it. Does my equation from the input file go into the record? How would I assign numbers from my file into "num" in the record, and operators from my file into the appropriate places in the record?
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
If you haven't studied records yet (sigh, halfway through the semester and your teacher's asleep), then just use an array of strings.
Take a single string like
and convert it into an array of strings
The operator with highest precedence is (), so you find the sub-array containing the bracketed expression (elements 3..5) and evaluate it in recursion. Replace elements 2..6 with the result of the last evaluation, so you would have
The drawback is that you must check to see what kind of thing each element of the array is each time you look at it, and you must continually convert between numbers and strings.
I think you should go talk to your teacher and see if he can't give you a better idea of how he wants you to do this.
Your assignment got me interested so I did it myself in about 300 lines. About 100 of that is lots of error checking and extra functions I threw in (like a power function).
Take a single string like
5+(12*3)and convert it into an array of strings
5, +, (, 12, *, 3, )The operator with highest precedence is (), so you find the sub-array containing the bracketed expression (elements 3..5) and evaluate it in recursion. Replace elements 2..6 with the result of the last evaluation, so you would have
5, +, 36.The drawback is that you must check to see what kind of thing each element of the array is each time you look at it, and you must continually convert between numbers and strings.
I think you should go talk to your teacher and see if he can't give you a better idea of how he wants you to do this.
Your assignment got me interested so I did it myself in about 300 lines. About 100 of that is lots of error checking and extra functions I threw in (like a power function).
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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.
Now, if you want to store information about a student, you have all that stuff grouped together under one name:
You can access information about a particular student:
You can copy records just like simple data types.
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:
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:
Now, you can create a function that creates a card with rank and suite.
And you can use it:
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.
Now, you can convert a string like
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:
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.
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.
Pascal Syntax (Toggle Plain Text)
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:
Pascal Syntax (Toggle Plain Text)
var students: array[ 1..35 ] of tStudent;
You can access information about a particular student:
Pascal Syntax (Toggle Plain Text)
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.
Pascal Syntax (Toggle Plain Text)
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:
Pascal Syntax (Toggle Plain Text)
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:
Pascal Syntax (Toggle Plain Text)
type tCardSuite = (Spades, Hearts, Clubs, Diamonds); tCardRank = 1..12; tCard = record suite: tCardSuite; rank: tCardRank end;
Pascal Syntax (Toggle Plain Text)
function create_a_card( rank: tCardRank; suite: tCardSuite ): tCard; var r: tCard; begin r.rank := rank; r.suite := suite; create_a_card := r end;
Pascal Syntax (Toggle Plain Text)
var card: tCard; begin card := create_a_card( 1, Spades ); end.
•
•
•
•
-
Pascal Syntax (Toggle Plain Text)
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: Pascal Syntax (Toggle Plain Text)
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 }
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.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
Man, you are great at explaining. I wish you were my prof 
Everything was clear until this part
Ok, I understand I need to get characters out of the string. This is the part that confused me a bit.
" 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. "
Thanks for all the help!

Everything was clear until this part
procedure string_to_equation( s: string; var equ: tEquation );
Ok, I understand I need to get characters out of the string. This is the part that confused me a bit.
" 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. "
Thanks for all the help!
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
I'm sorry, but you'll need to think on that one yourself for a bit. I can help once you write some code... for a hint, how would you do something like the following:
If you can break your code up into simple problems then all you have to do is solve a bunch of simple problems instead of one big problem. You'll have to do that to complete this assignment.
Once you have some code going, post here with specific problems for more help. Good luck. (And have fun!)
•
•
•
•
Write a function that takes a string (an array of characters) composed of the digits '0'..'9', turns it into an integer, and returns that integer value.
For example:
var i: integer; begin i := str_to_int( '42' ); writeln( 'Converted the string "42" to the integer ', i ) end.
Once you have done that, modify your function to return real values:
var x: real; begin x := str_to_real( '3.141592' ); writeln( 'Converted the string "3.141592" to the floating point value ', x:0:6 ) end.
Once you have some code going, post here with specific problems for more help. Good luck. (And have fun!)
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- C++ Calculator Program (C++)
- Really need help with the interactive Calculator program, or will be dead (C++)
- pascal triangle program (C++)
- starting a calculator program in C (C)
- Java Swing Calculator program not running. It has 0 errors (Java)
- Wierd error messages with calculator program (C++)
- need help with calculator program in C (C)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: RES file creaton under Vista
- Next Thread: Deleting components in D2007



Linear Mode