Results 1 to 12 of 12

Thread: Computing a String

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    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...

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Computing a String

    Yup! Priority is needed.
    To become a PROFESSIONAL,
    Start from SCRATCH...

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Computing a String

    Quote 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

  6. #6
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Computing a String

    Quote 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

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Computing a String

    Quote 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.

  8. #8
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Computing a String

    Quote Originally Posted by penagate
    Your signature is ironic.

    I almost said something about that in my post.

  9. #9
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: Computing a String

    you would need to use a for loop to run through it to prioritize...

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  11. #11
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    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...

  12. #12
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Computing a String

    Quote 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
  •  



Click Here to Expand Forum to Full Width