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,810 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: 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

Pascal calculator program

  #1  
Nov 15th, 2007
Hey guys, I am a new programmer just starting. The assignment my prof gave us has to deal with taking a file (the file is an equation, 5+2-3*2 for example) and calculating the result. The program also has to give appropriate errors when they arise. (missing operators, etc). I am a bit confused on how to do this, any help would be great thanks!
AddThis Social Bookmark Button
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

  #2  
Nov 16th, 2007
The best place to start is to get out a piece of paper and a pencil and figure out how you do it yourself. Once you know how to do it, step by step, you can tell the computer how to do it.

When you have thought it out enough to write some code, post it here and we'll be happy to help further.
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

  #3  
Nov 16th, 2007
Originally Posted by Duoas View Post
The best place to start is to get out a piece of paper and a pencil and figure out how you do it yourself. Once you know how to do it, step by step, you can tell the computer how to do it.

When you have thought it out enough to write some code, post it here and we'll be happy to help further.


Ok thanks. I looked at the other thread regarding the same type of program. Say i had the equation 1+2+3, I would want to look at the first number and save the value, then move to the next number and add it to the previous number. But I can't figure out how to write the code that moves from the first number, records it, then moves to the next number.
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

  #4  
Nov 16th, 2007
I think that is the part your professor is interested in.

A string is an array of characters. So, no matter how you get the equation (reading it from file or from user input) you still have a string:
var equation_string = '1+2+3';

What procedures and functions and techniques have you learned that can index and split a string? Turn a character digit into an integer?
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

  #5  
Nov 16th, 2007
Originally Posted by Duoas View Post
I think that is the part your professor is interested in.

A string is an array of characters. So, no matter how you get the equation (reading it from file or from user input) you still have a string:
var equation_string = '1+2+3';

What procedures and functions and techniques have you learned that can index and split a string? Turn a character digit into an integer?


Let me give some more information about this project. I need to read equations from an input file. For example, the file could look like this
5 + 3 * 5 -2
51 - 14/5 + 1 *5

Operator precedence must be taken into account as well as the bracketing of expressions. The first equation will be completely evaluated and the final answer determined before the next equation is read and evaluated. "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". This is my first project using arrays, so I will have to get use to it
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

  #6  
Nov 16th, 2007
So, according to your assignment description, you account for operator precedence by evaluating the same expression over and over until there is only a number (or error):
I start with
5 + 3 * 5 -2
The most precedent operator is *, so:
5 + 3 * 5 -2
5 + 15 -2
The remaining operators have equal precedence, so I'll just take them left to right:
5 + 15 -2
20 -2
and
20 -2
18
There are no more operators, so the answer is 18.

Now, you are supposed to store the expression in an array. So you could have an array of string, where each element is either a number or an operator:
'5', '+', '3', '*', '5', '-', '2'

Or you could have an array of some record type that tells you whether it is a number or an operator, say:
type tType = (number, add, subtract, multiply, ...);
type tElement = record element_type: tType; number: integer end;
This is how I would do it (if forced to use an array), but if it is beyond you just stick to an array of strings. I assume the latter in what follows, but the concepts work either way.

So, even a bracketed expression is easily stuck in an array:
5 * (2 + (3 + 1))
becomes
'5', '*', '(', '2', '+', '(', '3', '+', '1', ')', ')'
This makes it easy to find operators. The brackets are special, in that they work on a sub-array. Hence:
5 * (2 + 3)
has the () operator, which means to evaluate a subexpression. We'll do that first:
2 + 3
becomes
5

Now we can stick it back into the original expression and remove the ():
5 * 5
Evaluating further:
25

Your professor wants you to use an array, alas. You can move stuff around every time, or you can just transform unused parts (which have been "removed") into, say, the empty string, which means just to ignore it. For example, taking:
1 + 2 + 3
and transforming it into
3 + 3
can have two affects on your array. Starting with:
'1', '+', '2', '+', '3'
you can transform it to:
'3', '+', '3' (or '3', '+', '3', '', '')
or
'3', '', '', '+', '3'

Both ways have caveats you must be aware of. The first means you must shift elements downward. If you are allowed to use dynamic arrays you can get rid of extra elements at the end, otherwise you must set them to ''. The second way requires your code to skip '' when finding elements adjacent to an operator.

Well, that's enough to think about. Consider how you want to make it work and get started. Remember, it is perfectly OK to use functions and procedures to do something. For example, you might have a function to search your array for the operator with the highest precedence and return its index. Another might modify take the array and an index to an operator and do the operation.

Oh yeah, almost forgot. Convert between numbers and strings with strToInt() and intToStr().

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

  #7  
Nov 16th, 2007
Program Lesson9_Program(input, output);
Var file1 : Text;
eq1 : String;
eq2 : string;

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.

This is basically what I just have so far, it prints out both the equations that are in the file. Now, I need to somehow put those equations into an array I think.
Last edited by Gotovina7 : Nov 16th, 2007 at 10:59 pm.
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

  #8  
Nov 17th, 2007
How do I put a record into an Array? Pretty lost for this.
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

  #9  
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  
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

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

 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.
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:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC