|
-
Oct 1st, 2006, 08:33 PM
#1
Thread Starter
Lively Member
Computing a String
Need Help...
How can I compute a string data type?
For example, the value of variable strInput is "1+2+3-4*5", and the answer must be -14.
Need reply ASAP! I know its very simple for you.
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Oct 1st, 2006, 09:32 PM
#2
Re: Computing a String
Code:
import java.util.* ;
public class MyApp
{
public static void main ( String args[] )
{
Scanner s = new Scanner( System.in ) ;
System.out.println( "Please enter the mathematical expression" ) ;
String expression = s.nextLine() ;
Stack<Character> stNumbers = new Stack() ;
Stack<Character> stOps = new Stack() ;
for ( char c : expression.toCharArray() )
{
if ( Character.isDigit( c ) )
{
stNumbers.push( c ) ;
}
else
{
stOps.push( c ) ;
}
}
double value = Double.parseDouble( stNumbers.pop().toString() ) ;
while ( !stOps.isEmpty() )
{
double op2 = Double.parseDouble( stNumbers.pop().toString() ) ;
switch ( stOps.pop().charValue() )
{
case '+':
value += op2 ;
break ;
case '-':
op2 -= value ;
value = op2 ;
break ;
case '*':
value *= op2 ;
break ;
case '/':
op2 /= value ;
value = op2 ;
break ;
}
}
System.out.println( "The value of the expression is " + value ) ;
}
}
Last edited by ComputerJy; Oct 1st, 2006 at 09:37 PM.
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 1st, 2006, 09:34 PM
#3
Re: Computing a String
That was a very simple algorithm I rapidly created. It doesn't prioritize division and multiplication over summation and subtraction, you need to work on it a little bit
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 1st, 2006, 09:45 PM
#4
Thread Starter
Lively Member
To become a PROFESSIONAL,
Start from SCRATCH... 
-
Oct 1st, 2006, 10:02 PM
#5
Re: Computing a String
 Originally Posted by everard
Yup! Priority is needed.
Guess who's job is that
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 2nd, 2006, 05:30 AM
#6
Frenzied Member
Re: Computing a String
 Originally Posted by everard
Need Help...
How can I compute a string data type?
For example, the value of variable strInput is "1+2+3-4*5", and the answer must be -14.
Need reply ASAP! I know its very simple for you.
Do your own homework
-
Oct 3rd, 2006, 04:32 AM
#7
Re: Computing a String
 Originally Posted by everard
Need Help...
How can I compute a string data type?
For example, the value of variable strInput is "1+2+3-4*5", and the answer must be -14.
Need reply ASAP! I know its very simple for you.
Your signature is ironic.
-
Oct 3rd, 2006, 05:17 AM
#8
Frenzied Member
Re: Computing a String
 Originally Posted by penagate
Your signature is ironic.
I almost said something about that in my post.
-
Oct 9th, 2006, 08:15 AM
#9
Lively Member
Re: Computing a String
you would need to use a for loop to run through it to prioritize...
-
Oct 9th, 2006, 10:39 AM
#10
Re: Computing a String
Huh? What's a for-loop got to do with an abstract parsing problem?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 10th, 2006, 09:24 AM
#11
Lively Member
Re: Computing a String
I was stating that you would use a for loop to check for the order of operations
outer loop for addition
1 inner subtraction
2 inner for mulitiplication
3 inner for division
4th for exponets
There's a way to do the parathesises but it isn't hitting right now, another for loop that leads to a method to deal with them...
-
Oct 10th, 2006, 10:29 AM
#12
Re: Computing a String
 Originally Posted by vagabon
I was stating that you would use a for loop to check for the order of operations
outer loop for addition
1 inner subtraction
2 inner for mulitiplication
3 inner for division
4th for exponets
There's a way to do the parathesises but it isn't hitting right now, another for loop that leads to a method to deal with them...
Yeah you are right, and I think it's a third Stack for parenthesis
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
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
|