•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
Rather than a bunch of "if foo = x ... " statements, use a "case foo of" statement:
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...)
Pascal Syntax (Toggle Plain Text)
case oper of '+': begin ... end; '-': begin ... end; 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...)
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
Still having alittle troubles getting that the second digits. I came up with this
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
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
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
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.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
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?
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?
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
Yea it makes sense, except whenever I try to find num2 (and all the digits after it) I just keep getting num1
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.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
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?
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?
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 1
Solved Threads: 0
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
. 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.
![]() |
•
•
•
•
•
•
•
•
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