User Name Password Register
DaniWeb IT Discussion Community
All
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,754 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
Reply
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
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: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #51  
Nov 28th, 2007
It is hard for a beginner, but not quite "way too hard".

Let me think a bit and post back later. My brain hurts and I'm tired of going around in circles with you.

Some quick things first:
1. Your last thoughts seem like a good plan.
2. Please get out some paper and draw how to accomplish each thing.
3. Please go talk to your prof.
4. See my post #7 for some big help. Just ignore the stuff about using records. Everything else applies.
5. temp_result := num1 causes them to both have the same value, which is not what I said. You don't need two variables with the same value. You need one variable that does what you are currently doing with two.

I'll post again in a day or after I've gotten some sleep.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
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: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #52  
Nov 29th, 2007
OK, I've had time to think it over.

Your post #50 is a good plan. You haven't "wasted" any time. You've learned a good deal and what you have learned directly applies to your assignment.

1. read from input file
2. Put the equation from input file into an array
These things can be done together. You have already developed a loop that gets numbers and operators from a file. The only thing you haven't done is considered the possibility that two operators may be next to each other (legally). My post #38 gives you both valid and invalid examples.

As a hint, consider what would happen if you called "find_digits" when c was not a digit. Your routines to read operators and skip spaces and the like should do the same thing.

3. use a procedure to get rid of spaces in the equation (if any)
Don't bother. Spaces can be filtered out when reading from file into an array.

4. another procedure to evaluate brackets
Brilliant! I suggest that the procedure that evaluates brackets be the same procedure that evaluates an equation.
procedure evaluate( equ: tEquationArray; len: cardinal; start, stop: cardinal );
This procedure takes an equation array, its length, and what part of the array is to be evaluated. So, given an equation like
0 1 2 3 4 5 6
5 * ( 2 + 3 )
you can first call with:
evaluate( ..., 7, 0, 6 );
Since the equation has a sub-equation (due to parentheses), your procedure can find the sub-equation and evaluate it with:
evaluate( ..., 7, 2, 6 ) (which includes the parentheses to remove)
or
evaluate( ..., 7, 3, 5 ) (which excludes the parentheses to remove).
You must decide where you want to remove parentheses, in recursion or after recursion.

It might be handy to have a procedure which simply finds the indices of the leftmost, outermost pair of matching parentheses.

5. Evaluate the equation
Yes. This should be done as part of the "equation" routine, but after the parentheses are handled. What this means is all you need to worry about is numbers and normal operators like +, -, *, etc. See my post #7 for more.


Don't get discouraged. This is a big assignment (and I think, a poorly planned one...). But you can do it if you take the time to think, and keep separate things separate. When reading numbers, only worry about reading numbers. When reading operators, only worry about reading operators. When removing elements in the equation array and replacing them with a single element (a number), worry only about modifying the array. Etc.

Hope this helps.
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #53  
Nov 29th, 2007
Originally Posted by Duoas View Post
Brilliant! I suggest that the procedure that evaluates brackets be the same procedure that evaluates an equation.
procedure evaluate( equ: tEquationArray; len: cardinal; start, stop: cardinal );
This procedure takes an equation array, its length, and what part of the array is to be evaluated. So, given an equation like
0 1 2 3 4 5 6
5 * ( 2 + 3 )
you can first call with:
evaluate( ..., 7, 0, 6 );
Since the equation has a sub-equation (due to parentheses), your procedure can find the sub-equation and evaluate it with:
evaluate( ..., 7, 2, 6 ) (which includes the parentheses to remove)
or
evaluate( ..., 7, 3, 5 ) (which excludes the parentheses to remove).
You must decide where you want to remove parentheses, in recursion or after recursion.

It might be handy to have a procedure which simply finds the indices of the leftmost, outermost pair of matching parentheses.


I like the idea of that procedure, but I am not sure how to exactly write it up. When you declared equ : tEquationArray , do I need to make a type for this? Do I need to change the way I am reading the file for my entire program? Instead of reading a character do I read as a string and put it into the array?
Last edited by Gotovina7 : Nov 29th, 2007 at 6:37 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
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: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #54  
Nov 29th, 2007
How did your professor say you should define your array?
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #55  
Nov 29th, 2007
HMmm, it never said we had to. Another thing, the equation in the input file is assumed to be valid. So I don't have to worry about all the errors.
Last edited by Gotovina7 : Nov 29th, 2007 at 9:24 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
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: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #56  
Nov 29th, 2007
Hold on. The entire length of this thread you've maintained that your professor wants you to use an array.
"Every time an intermediate result is calculated the corresponding components of that calculation are removed from the array(s) and replaced with the correct answer".

Did he or did he not? Which is it?
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #57  
Nov 29th, 2007
Originally Posted by Duoas View Post
Hold on. The entire length of this thread you've maintained that your professor wants you to use an array.

Every time an intermediate result is calculated the corresponding components of that calculation are removed from the array and replaced with the correct answer.

Did he or did he not? Which is it?


Yes that is correct, " Every time an intermediate result is calculated the corresponding components of that calculation are removed from the array and replaced with the correct answer." is something is we need to do.

4 + 2 * 5 +2 = 4 + 10 + 2
Last edited by Gotovina7 : Nov 29th, 2007 at 9:34 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
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: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #58  
Nov 29th, 2007
Alright. Since your equation is to be stored in an array, and you are not permitted to store it in an array of records, then how are you supposed to store it? What does he want you to use for each element of the array?
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #59  
Nov 29th, 2007
" Your program MUST use an array as the main storage medium for the equation " . So, I guess you need to use an array for the main storage but doesn't have to use an array all the time?
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
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: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #60  
Nov 29th, 2007
That's too vague. Is it an array of integers? Is it an array of strings? Is it an array of reals? Chars? Enumerations? Sets?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 5:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC