Pascal calculator program

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

 
0
  #11
Nov 17th, 2007
An array is just a list of things all having the same type. An array of integer is:
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

 
0
  #12
Nov 17th, 2007
Something still isn't right, this is what I have

Pascal and Delphi Syntax (Toggle Plain Text)
  1. Program Lesson9_Program(input, output);
  2.  
  3. Type
  4. Str25 = String[25];
  5. tEquation = Record
  6. num : Str25;
  7. add,subtract,multiply,divide : Str25;
  8. end;
  9.  
  10.  
  11. Var file1 : Text;
  12. eq1 : String[255];
  13. eq2 : string[255];
  14. myEquation : tEquation;
  15. myeqArray : array[1..50] of tEquation;
  16.  
  17. Begin
  18.  
  19. Assign(file1,'input.txt');
  20. Reset(file1);
  21. If (IOResult <> 0) then
  22. Begin
  23. Writeln('The file required to be opened is not found!');
  24. Readln;
  25. End Else
  26. Begin
  27. readln(file1, eq1);
  28. Writeln('The first line of the file reads: ',eq1);
  29. readln(file1, eq2);
  30. writeln('The second line of the file reads: ',eq2);
  31. End;
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

 
0
  #13
Nov 17th, 2007
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 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.
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.
Last edited by Duoas; Nov 17th, 2007 at 11:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

 
0
  #14
Nov 18th, 2007
Originally Posted by Duoas View Post
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
Pascal and Delphi Syntax (Toggle Plain Text)
  1. type tType = (number, add, subtract, multiply, ...);
  2. 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

 
0
  #15
Nov 18th, 2007
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 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).
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

 
0
  #16
Nov 18th, 2007
I appreciate all the help. My prof is very confusing, and I don't think he is a good teacher at all. We did records last class but he didn't explain them very well.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

 
0
  #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 Quick reply to this message  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

 
0
  #18
Nov 18th, 2007
Man, you are great at explaining. I wish you were my prof

Everything was clear until this part

Pascal and Delphi Syntax (Toggle Plain Text)
  1. 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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

 
0
  #19
Nov 19th, 2007
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:
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:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. var i: integer;
  2. begin
  3. i := str_to_int( '42' );
  4. writeln( 'Converted the string "42" to the integer ', i )
  5. end.

Once you have done that, modify your function to return real values:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. var x: real;
  2. begin
  3. x := str_to_real( '3.141592' );
  4. writeln( 'Converted the string "3.141592" to the floating point value ', x:0:6 )
  5. end.
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!)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

 
0
  #20
Nov 19th, 2007
I got some bad news. I talked to my prof today and he doesn't want us to use records for this assignment. The next assignment will involve records.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 11828 | Replies: 62
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC