Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

infix, postfix expression

843789Oct 29 2009 — edited Nov 1 2009
Hi all!
I have some questions, I wanna ask you.
About changing infix expression to postfix expression
If I have 4 class
interface Expression {
	
	public String toInfixString();
	
	public String toSuffixString();
	
	public double compute();
}

public enum typeOfExp {
	Infix, Suffix
}

class Connect {
	
	public static Expression createExp (String exp, typeOfExp type)
	{
		return null;
	}
}

public class ExpRunner {
	
	public static void main(String[] args)
	{
		//
		Expression exp =
 connect.createExp ("6.8+ 3/4-(9*2-1+5) +1.0)", typeOfExp.Infix);
		System.out.println(exp.compute());
		System.out.println(exp.toSuffixString());
	}
}
- I don?t understand Connect class. CreateExp is the method of that class, and Expression in this case is the data type of this method? Is this right? So we can take place it of building the constructor of Expression class?
- Another question is how to get the String exp in class Connect to compute in the class implement Expression interface? :?> That is a String so have I to read every item in that string and put it into an interger array to compute by using stack? And if item of the array is a function as pow, sin, cos => how to traverse? I mean if the item is an Interger or operator, I can write is if(operator(item)) ( operator is the function I create)? but with pow, sin, cose how can I write?
I?m very bad at programming so if these questions are so stupid. Please don?t fret me.
Regard,

Comments

796262 Oct 29 2009 — edited on Oct 29 2009
sun26 wrote:
I don't understand Connect class.
Isn't it your class? If this code was provided by a teacher, I would ask that person. We can't really tell you what your classes are supposed to be doing. We can only guess.
CreateExp is the method of that class, and Expression in this case is the data type of this method? Is this right?
The method createExp (I'm assuming that's short for createExpression, but why shorten it and force people to assume?) returns an Object of type Expression.. Er, as of now it returns null. I assume your assignment is to implement that method?
So we can take place it of building the constructor of Expression class?
I have no idea what this means. What does "take place" mean? What is "it"? And how do you "build the constructor of Expression class"?
Another question is how to get the String exp in class Connect to compute in the class implement Expression interface?
Again, I assume that's the point of your homework assignment. So basically you want us to do your homework for you?
That is a String so have I to read every item in that string and put it into an interger array to compute by using stack?
Like I said, we have no idea what that String is supposed to contain. Maybe you should give some examples. Where are you getting the idea of using a Stack? Is that what your teacher told you to do? If so, what happened when you tried it?
And if item of the array is a function as pow, sin, cos => how to traverse? I mean if the item is an Interger or operator, I can write is if(operator(item)) ( operator is the function I create)? but with pow, sin, cose how can I write?
The equals function comes to mind. But what have you tried?
843789 Oct 29 2009
Thanks kevinaworkman, you minght not understand my idea. But now i understand those classes and methods more clearly but i still have some troubles.
Example :
Expression : 2*3-4+9/pow(3,4)-sin(5);
That is an expression produced as String: S = "2*3-4+9/pow(3,4)-sin(5)"
I use charAt method to traverse each item like: char a = S.charAt(i);
So i only get an character, with pow or sin function i only single p,o,w ? Is this right?
843789 Oct 29 2009
sun26 wrote:
I use charAt method to traverse each item like: char a = S.charAt(i);
So i only get an character, with pow or sin function i only single p,o,w ? Is this right?
The charAt() will indeed return a single character. If that fulfills your requirements, then it is right.
843789 Oct 30 2009
So how can i trarvese that String if it has pow, sin, cos function?
843789 Oct 30 2009 — edited on Oct 30 2009
sun26 wrote:
So how can i trarvese that String if it has pow, sin, cos function?
[http://en.wikipedia.org/wiki/Lexical_analysis]

I would have expected that previous instructional material would have covered this before you were given the assignment. As it is, you'll need write logic to break up the string into its expression parts.

~
JosAH Oct 30 2009
yawmark wrote:
sun26 wrote:
So how can i trarvese that String if it has pow, sin, cos function?
[http://en.wikipedia.org/wiki/Lexical_analysis]

I would have expected that previous instructional material would have covered this before you were given the assignment. As it is, you'll need write logic to break up the string into its expression parts.
Don't forget syntax analysis and code generation and possibly interpretation. Lexical analysis loves Strings like ")sin2+" although those Strings don't make sense syntactically. Even Strings such as "sin(2)" don't make much sense if the code generator or interpreter doesn't understand that "sin" is supposed to be a monary function.

kind regards,

Jos (<--- has a new wireless printer! <happy dance/>)
843789 Oct 31 2009
I have trouble with minus '-' and negative number for example : 2-3+5 => suffix: 23-5+ , not 235+- . So is it right to pop the '-' out off the stack if the top of the stack is '-' but it's wrong if the expression has '*' or '/' like: 2-3*5 + 7 => suffix 235*-7+ ? So what is the algorithm in this case?
Is it right that when encounting '-', i could put '0' before it.

With an infix expression like this: -pow(2,2)+--++-----6*--5
How to calculate and change it to suffix easily and simplely.
And i don't know how to change suffix expression to infix expression. Can you show me? Should i use stack or tree?
Thanks!
JosAH Oct 31 2009
sun26 wrote:
I have trouble with minus '-' and negative number for example : 2-3+5 => suffix: 23-5+ , not 235+- . So is it right to pop the '-' out off the stack if the top of the stack is '-' but it's wrong if the expression has '*' or '/' like: 2-3*5 + 7 => suffix 235*-7+ ? So what is the algorithm in this case?
Is it right that when encounting '-', i could put '0' before it.

With an infix expression like this: -pow(2,2)+--++-----6*--5
How to calculate and change it to suffix easily and simplely.
And i don't know how to change suffix expression to infix expression. Can you show me? Should i use stack or tree?
Thanks!
If you're using a simple stack you push operators on on the fly you need different symbols for the binary minus (e.g. 3-5) and unary minus (e.g. -5) because you can't distinguish the two after you've pushed one of them on that stack. Better use a simple recursive descent parser; it can predict the unary minus versus a binary minus and you don't need two different symbols for the two operators; the same applies for a unary plus symbol. Have a look at this simple grammar:
E: T Et
Et: +- T Et |
T: F Tt
Tt: */ F Tt |
F: + F | - F | U
U: constant | identifier | ( E )
(this was from the top of my head, so be gentle with me ;-)

kind regards,

Jos
843789 Nov 1 2009 — edited on Nov 1 2009
JosAH wrote:
Don't forget syntax analysis and code generation and possibly interpretation.
Of course! Lexical analysis is just the first step. That's what I interpreted the focus to be, given the question of how to "traverse" the string in a fashion other than just reading one character at a time.

P.S. Congratulations on your new printer!

~

Edited by: yawmark on Nov 1, 2009 9:49 AM
JosAH Nov 1 2009
yawmark wrote:
P.S. Congratulations on your new printer!
Thanks; it's a colour printer and it can scan paper documents and it's a copier too and it's fast! And it's wireless! And it can send and receive faxes too. It also has a cute lcd colour display. Now only if I can find something sensible to print or fax or scan or copy ... ;-)

kind regards,

Jos (<--- almost never prints anything)
1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 29 2009
Added on Oct 29 2009
10 comments
324 views