Advertisement
DNNdrago

3, Simple Expression

Jun 2nd, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.text.DecimalFormat;
  3. import java.util.Scanner;
  4.  
  5.  
  6.  
  7. public class _03_Problem3 {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.        
  12.         String expression = sc.nextLine().replaceAll(" ", "");
  13.        
  14.         double totalSum = 0;
  15.        
  16.         for(int i = 0; i < expression.length(); ) {
  17.        
  18.             int signPos = nextSignPossition(expression, 1);
  19.             int nextSignPost = nextSignPossition(expression, signPos + 1);
  20.             if(nextSignPost == -1) {
  21.                 double firstNumber = Double.parseDouble(expression.substring(0, signPos));
  22.                 String operation = expression.substring(signPos, signPos + 1);
  23.                 double secondNumber = Double.parseDouble(expression.substring(signPos + 1, expression.length()));
  24.                
  25.                 totalSum = calc(firstNumber, secondNumber, operation);
  26.                
  27.                 break;
  28.             }
  29.             double firstNumber = Double.parseDouble(expression.substring(0, signPos));
  30.             String operation = expression.substring(signPos, signPos + 1);
  31.             double secondNumber = Double.parseDouble(expression.substring(signPos + 1, nextSignPost));
  32.            
  33.             String sum = calc(firstNumber, secondNumber, operation) + "";
  34.        
  35.             expression = sum  + expression.substring(nextSignPost);
  36.        
  37.         }      
  38.        
  39.        
  40.        
  41.     }
  42.    
  43.     public static double calc(double firstValue, double secondValue, String sign) {
  44.         double sum = 0;
  45.         switch (sign) {
  46.         case "+":
  47.             sum = firstValue + secondValue;
  48.             break;
  49.         case "-":
  50.             sum = firstValue - secondValue;
  51.             break;
  52.         default:
  53.             break;
  54.         }
  55.        
  56.         return sum;
  57.     }
  58.    
  59.     public static int nextSignPossition (String expression, int start) {
  60.         int signPos;
  61.         int plusIndex = expression.indexOf("+", start);
  62.         int minusIndex = expression.indexOf("-", start);
  63.        
  64.         if (plusIndex == -1 && minusIndex == -1) {
  65.             signPos = -1;
  66.         }
  67.         else {
  68.             signPos = (plusIndex < minusIndex && plusIndex != -1 || minusIndex == -1) ? plusIndex : minusIndex;
  69.         }
  70.        
  71.         return signPos;
  72.        
  73.     }
  74.    
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement