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 427,773 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,736 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: 6360 | Replies: 62
Reply
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #41  
Nov 25th, 2007
Rather than a bunch of "if foo = x ... " statements, use a "case foo of" statement:
  1. case oper of
  2. '+': begin
  3. ...
  4. end;
  5. '-': begin
  6. ...
  7. end;
  8. end;

You've already got a function that will read a number. (You used it to get num1.) Use it to get num2.

While you are moving along quite logically... your current line of thought is taking you toward that recursive descent algorithm that ExplainThat mentioned in joygqy's thread... there are no arrays in sight. If you are not to use records I'm at a loss as to how your professor expects you to use an array. You should take what you've got so far and go see him and ask how he wants you to put your equation into an array.

(Frankly, I think the assignment has a few points that are a little over the top. While simple, it is not trivial, and it is definitely not a beginner-level thing to properly handle mathematical expressions...)
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #42  
Nov 25th, 2007
Still having alittle troubles getting that the second digits. I came up with this

   while (my_char <> '=') and (not eoln(my_file)) do
   begin
      ignorespaces(my_file, my_char);
      num2 := find_digits(my_char, my_file);
      read(my_file, my_char);
      write(num2);
   end;

This gives me all the digits that I have in my file. My file consists of 2 equations:
5+4-3*2=
3+4/2*3=

This while loop gives me the output of 54323423
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #43  
Nov 26th, 2007
Ok, I messing around some more with my code and made it to give an output of 5 4 3 2 (where "5" is num1, and "4 3 2" are num2). I was trying to think of a way to put the numbers in an array but nothing worked.

The logic I am trying to use to solve this problem is as follows:
1. Num1 would be a "temporary result"
2. Read the next number, perform the operation needed. save this new number as "temporary result"
3. and repeat this

The problem is I don't know how to make "num2" be a single number that is the next number in my equation. So, I would want num2 to be "4" and so on, until the problem is solved.
Last edited by Gotovina7 : Nov 26th, 2007 at 2:12 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #44  
Nov 26th, 2007
You are on the cusp of getting it.

Num2 can never be more than one number at once. It can be different numbers at different times... it is a different number each time through the loop (you get a new num2, print it, then go through the loop again to repeat).

So your logic steps are good and correct. The problem is there is no array anywhere in there. Hence why you need to go bug your prof.
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #45  
Nov 26th, 2007
I am lost trying to get num2 to equal the second digit, then the next time the loop is ran it equals the third digit?
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #46  
Nov 27th, 2007
I don't understand why you are confused.
1. You use find_digits to read num1 (the first number).
2. You read a character (one of '+' or '-' or something).
3. You use find_digits to read num2 (the second number).
4. You read a character (one of '+' or '-' or something).
5. You use find_digits to read num2 (the third number).
6. You read a character (one of '+' or '-' or something).
7. You use find_digits to read num2 (the fourth number).

Make sense?
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #47  
Nov 28th, 2007
Yea it makes sense, except whenever I try to find num2 (and all the digits after it) I just keep getting num1

        case oper of
         '+' : begin
            num2 := find_digits(my_char, my_file);
            temp_result := num1 + num2;
         end;
         '-' : begin
            num2 := find_digits(my_char, my_file);
            temp_result := num1 - num2;
         end;
         '*' : begin
            num2 := find_digits(my_char, my_file);
            temp_result := num1 * num2;
         end;
         '/' : begin
            num2 := find_digits(my_char, my_file);
            temp_result := num1 div num2;
         end;
       end; { case }

Now, the problem I am having trouble with is the trying to keep reading the next number. Since my equations are
5+4-3*2=
3+4/2*3=
This case loop will give me output of 9 8 7 (not sure where the 8 is coming from). There is still something wrong, the loop is read both lines at once.
Last edited by Gotovina7 : Nov 28th, 2007 at 9:51 am.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
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: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pascal calculator program

  #48  
Nov 28th, 2007
Well, you know that the loop is reading the numbers correctly (because you before tested it by just reading and printing the numbers, and they printed the correct numbers).

But now that you are doing something to the numbers something is wrong.

May I suggest that num1 and temp_result should be the same variable?
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #49  
Nov 28th, 2007
Originally Posted by Duoas View Post
Well, you know that the loop is reading the numbers correctly (because you before tested it by just reading and printing the numbers, and they printed the correct numbers).

But now that you are doing something to the numbers something is wrong.

May I suggest that num1 and temp_result should be the same variable?

Not sure what you mean here, I have temp_result := num1; ?

I am having enough trouble as it is trying to get this too work. And I also need to make my program do operator presedence and evaluate brackets first. And somehow tie arrays into it all. Here is an example of how arrays are used in this assignment.

procedure analyze(a : integer; b : integer);

begin
   var oper               : char;
      num1, num2, i, j, k : integer

   For i := (a+1) to (b-1) do
   begin
      If arr[i] := ('*' or '/') then
      begin
         num2 := 0;
         j := i + 1;

         repeat
            if (ord(arr[i]) >= ord('0')) and (ord(arr[i]) <= ord('9')) then
            begin
               num2 := num2 * 10 + ord(array[i]) - ord('0');
            end;
            j := j + 1;

         until (ord(arr[i]) < ord('0')) or (ord(arr[i] > ord('9'))

 end;

Even though this code doesn't really accomplish much, it is an example of what I am suppose to do.
Last edited by Gotovina7 : Nov 28th, 2007 at 8:13 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 44
Reputation: Gotovina7 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Gotovina7 Gotovina7 is offline Offline
Light Poster

Re: Pascal calculator program

  #50  
Nov 28th, 2007
Yea man, I think I need to start all over using arrays . All that work for nothing (i think).

So this is what I am thinking:

1. read from input file
2. Put the equation from input file into an array
3. use a procedure to get rid of spaces in the equation (if any)
4. another procedure to evaluate brackets
5. Evaluate the equation

What do you think?


This assignment seems way to hard for a beginner
Last edited by Gotovina7 : Nov 28th, 2007 at 9:00 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 1:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC