Advertisement
alefhidalgo

TLang

Jun 20th, 2011
1,546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. /**
  9.  * Tuenti Programming Contest
  10.  * Challenge 2: TLang
  11.  * @author alefhidalgo [at] gmail [dot] com
  12.  */
  13. public class TLang {
  14.     /* ADD OPERATION TOKEN */
  15.     private String OP_ADD = "^=";
  16.     /* MULTIPLICATION OPERATION TOKEN */
  17.     private String OP_MULT = "^#";
  18.     /* SUBTRACTION OPERATION TOKEN */
  19.     private String OP_SUB = "^@";
  20.  
  21.     /**
  22.      * Solve expression
  23.      * @param expression
  24.      * @return
  25.      */
  26.     public String solveExpression(String expression) { 
  27.         /* Pattern to recognise simple expressions */
  28.         Pattern p = Pattern.compile("[[\\^#][\\^=][\\^@]]+(\\s+[-]?\\d+)?\\s+[-]?\\d+\\$+");
  29.         Matcher m = p.matcher(expression);
  30.         //recursive until expression will be fully simplified
  31.         if (m.find()) {
  32.             String simpleExpression = m.group();
  33.             String simpleExpressionSolved = solveSimpleExpression(simpleExpression);
  34.             String simplyfiedExpression = expression.replace(simpleExpression, simpleExpressionSolved);        
  35.             return solveExpression(simplyfiedExpression);
  36.         } else {           
  37.             //at this point the expression is solved
  38.             return expression;
  39.         }
  40.     }
  41.    
  42.     /**
  43.      * Solve Simple Expression
  44.      * @param simpleExpression
  45.      *            ^# xxx yyy$..$ or ^@ yyy$..$
  46.      * @return
  47.      */
  48.     private String solveSimpleExpression(String simpleExpression) {
  49.         BigDecimal number1 = null;
  50.         BigDecimal number2 = null;
  51.         BigDecimal result = null;
  52.         String mod = "";
  53.         Scanner exprScn = new Scanner(simpleExpression);       
  54.         List<String> arrExpr = new ArrayList<String>();
  55.         while (exprScn.hasNext()) {
  56.             arrExpr.add(exprScn.next());
  57.         }      
  58.         //Type1 ^@ yyy$..$
  59.         if (arrExpr.size() == 2) {
  60.             mod = arrExpr.get(1).replaceFirst("[-]?\\d+\\$", "");
  61.             number1 = new BigDecimal(arrExpr.get(1).replaceAll("\\$", ""));
  62.             if (arrExpr.get(0).equals(OP_SUB)) {
  63.                 result = number1.negate();
  64.             }else {
  65.                 result = number1;
  66.             }
  67.         //Type2 ^# xxx yyy$..$
  68.         } else {
  69.             number1 = new BigDecimal(arrExpr.get(1));
  70.             number2 = new BigDecimal(arrExpr.get(2).replaceAll("\\$", ""));
  71.             mod = arrExpr.get(2).replaceFirst("[-]?\\d+\\$", "");
  72.             if (arrExpr.get(0).equals(OP_ADD)) {
  73.                 result = number1.add(number2);
  74.             } else if (arrExpr.get(0).equals(OP_MULT)) {
  75.                 result = number1.multiply(number2);
  76.             } else if (arrExpr.get(0).equals(OP_SUB)) {
  77.                 result = number1.subtract(number2);
  78.             }
  79.         }
  80.         return result.toPlainString() + mod;
  81.     }
  82.    
  83.     public static void main(String args[]) {
  84.         TLang tLang = new TLang();
  85.         Scanner in = new Scanner(System.in);
  86.         while (in.hasNextLine()) {
  87.             System.out.println(tLang.solveExpression(in.nextLine()));
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement