|
-
December 15th, 2010, 05:21 AM
#3
Re: #URGENT# Infix to postfix conversion and evaluation problem.
 Originally Posted by Kramarz
Greetings!
The program works fine as long as I dont put any '(' and ')' in the infix, and I can't find the reason why is it doing that. Also, I can't seem to make the program calculate numbers bigger than 9.
The issue is that your program was written initially to process "easy", one digit, with no parenthetical expressions, and it did it by linear scanning each character and determining what to do based on that character. In other words, it is an ad-hoc, informal approach to parsing the string expression with no real structure or design to it.
There is one big problem with this approach -- you can't easily maintain this code if you need to add things like parentheses, numbers more than one digit, etc. The code will become unwieldly, and almost impossible to get right all the time. We basically would have to scrap the code you have now and rewrite it to do things formally and structured.
What should have been done is to first write a grammar that parses the expression. Then you write a parser (recursive descent is the easiest) that goes through the expression, breaks it down into tokens, and does the action based on the token and grammar rules.
The algorithm in terms of going from infix to postfix remains the same using the stack, but the big issue is how do you parse the expression properly to determine what goes on the stack? Your code has "hard-coded" the rules -- one digit, no parentheses, etc. Once you hard-code these rules into your logic, you're basically stuck with those rules.
In terms of let's say 23+17. And after placing 23+17 in the infix, I can't seem to make postfix look like 23 17 +. Could you please help me?
How do you build the integer, when all you have is a bunch of characters? See my point above. If the string contains "10203 + 234 - (14 + 3232)", how are you going to process "10203" and interpret that as 10203? or "234" as 234? This piece of code is not going to work at all:
Code:
if(isdigit(*p)){
push(*p-48);
}
You need to know where the string version of the integer starts and know where it ends, and then you convert that string into an int and place it on the stack. Nowhere do you do this.
If this is a school assignment (which it sounds like it is), you should have been told from the beginning that coding a formal parser is the way to handle this issue, as most assignments that require parsing a mathematical expression gives the simple grammar in what makes up an expression.
Your program inherently can only handle single digits, to add handling multiple digit numbers and parentheses is not a quick thing to do. That in itself is a huge part of your assignment which unfortunately you didn't do, and frankly may not have time to do it. If you had the time, I would have suggested you scrap this code, write the grammar rules for an expression, and write the recursive descent parser.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 15th, 2010 at 09:53 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|