Advertisement
Guest User

fraccalcarrayparse

a guest
Oct 25th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package parseToArray;
  2. import java.util.Scanner;
  3. // "\\d+/\\d+"
  4. public class ArrayParse {
  5.     public static void main(String[] args) {
  6.         Scanner console = new Scanner(System.in);
  7.         //HashSet<String> operator = new HashSet<String>(Arrays.asList("+","-","*","/"));
  8.         int foo=0;
  9.         String ans = new String();
  10.         while(true)
  11.         {
  12.             System.out.print("Enter your expression \nType \"quit\" to quit\n");
  13.             String input = console.nextLine();
  14.             if(input.equals("quit"))
  15.                 break;
  16.             else if(input.matches("( ?[+-/*] (([+-]?\\d+_)?[+-]?\\d+/[+-]?\\d+)|([+-]?\\d+))+") && foo > 0)
  17.             {
  18.                 if(input.charAt(0)!= ' ')
  19.                     input = " " + input;
  20.                 String mutie = (ans + " " + input);
  21.                 System.out.println(parse(mutie));
  22.             }
  23.             else if (input.matches("((([+-]?\\d+_)?[+-]?\\d+/[+-]?\\d+)|([+-]?\\d+))( [+-/*] (([+-]?\\d+_)?[+-]?\\d+/[+-]?\\d+)|([+-]?\\d+))+"))
  24.             {
  25.                 ans = parse(input);
  26.                 System.out.println(ans);
  27.                 foo++;
  28.             }
  29.             else
  30.                 System.out.println("INVALID INPUT");
  31.         }
  32.         System.out.println("TERMINATED");
  33.         console.close();
  34.  
  35.     }
  36.     public static String parse(String input)
  37.     {
  38.         int spacecount = input.length() - input.replace(" ", "").length();
  39.         int operandscount = 1 + spacecount/2;
  40.         String[] operand = new String[operandscount + 1];
  41.         String[] flubber = new String[operandscount + 1];
  42.         flubber[0] = input;
  43.         char[] operator = new char[operandscount];
  44.         int place = 1;
  45.         int space = 0;
  46.         int nustart = 0;
  47.         while(nustart < input.length())
  48.         {
  49.             flubber[place] = input.substring(nustart, input.length());
  50.             space = flubber[place].indexOf(" ");
  51.             if(place == operandscount)
  52.             {
  53.                 operand[place] = flubber[place];
  54.                 break;
  55.             }
  56.             operand[place] = flubber[place].substring(0,space);
  57.             operator[place] = flubber[place].charAt(space + 1);
  58.             nustart +=  (operand[place].length() + 3);
  59.             place++;
  60.         }
  61.         /*for(int baz = 1; baz<=operandscount; baz++)
  62.         {
  63.             if(!(operand[baz].matches("\\d+_\\d+/\\d+") || operand[baz].matches("\\d+/\\d+") || operand[baz].matches("\\d+")))
  64.                 return "INVALID OPERAND";
  65.         }
  66.         for(int xyzzy = 1; xyzzy<operandscount; xyzzy++)
  67.         {
  68.             if(!OPERATOR.contains("" + operator[xyzzy]))
  69.                 return "INVALID OPERATOR";
  70.         }*/
  71.         int[] num = new int[operandscount + 1];
  72.         int[] denom = new int[operandscount + 1];
  73.         for(int foo = 1; foo <= operandscount; foo++)
  74.         {
  75.             num[foo] = getUnredFracPart(operand[foo], true);
  76.             denom[foo] = getUnredFracPart(operand[foo], false);
  77.         }
  78.         int[] operatedNum = new int[operandscount + 1];
  79.         int[] operatedDenom = new int[operandscount + 1];
  80.         operatedNum[1] = num[1];
  81.         operatedDenom[1] = denom[1];
  82.         for(int bar = 2; bar <= operandscount; bar++)
  83.         {
  84.             if(operator[bar-1] == '+')
  85.             {
  86.                 operatedNum[bar] = (operatedNum[bar-1]*denom[bar] + num[bar]*operatedDenom[bar-1]);
  87.                 operatedDenom[bar] = (operatedDenom[bar-1] * denom[bar]);
  88.             }
  89.             else if(operator[bar-1] == '*')
  90.             {
  91.                 operatedNum[bar] = (operatedNum[bar-1] * num[bar]);
  92.                 operatedDenom[bar] = (operatedDenom[bar-1] * denom[bar]);
  93.             }
  94.             else if(operator[bar-1] == '/')
  95.             {
  96.                 operatedNum[bar] = (operatedNum[bar-1] * denom[bar]);
  97.                 operatedDenom[bar] = (operatedDenom[bar-1] * num[bar]);
  98.             }
  99.             else if(operator[bar-1] == '-')
  100.             {
  101.                 operatedNum[bar] = (operatedNum[bar-1]*denom[bar] - num[bar]*operatedDenom[bar-1]);
  102.                 operatedDenom[bar] = (operatedDenom[bar-1] * denom[bar]);
  103.             }
  104.             else
  105.                 return "INVALID OPERATOR";
  106.         }
  107.         return formatAnswer(operatedNum[operandscount], operatedDenom[operandscount]);
  108.     }
  109.     public static int getUnredFracPart(String operand, boolean wantNum)
  110.     {
  111.         int div = operand.indexOf("/");
  112.         int mix = operand.indexOf("_");
  113.         if(div!= -1)
  114.         {
  115.             if(wantNum == true)
  116.             {
  117.                 int num = Integer.parseInt(operand.substring(mix + 1,div));
  118.                 if(mix != -1)
  119.                     num += mix * Integer.parseInt(operand.substring(div+1));
  120.                 return num;
  121.             }
  122.             else
  123.             {
  124.                 String outstring = operand.substring(div+1);
  125.                 return Integer.parseInt(outstring);
  126.             }
  127.         }
  128.         else
  129.         {
  130.             if(wantNum == true)
  131.             {
  132.                 int num = Integer.parseInt(operand);
  133.                 return num;
  134.             }
  135.             else
  136.                 return 1;
  137.         }
  138.     }
  139.     public static String formatAnswer(int numfin, int denomfin)
  140.     {
  141.         int numprin = 0;
  142.         int denomprin = 0;
  143.         int whole = 0;
  144.         for(int foo = 1; foo<=Math.abs(numfin); foo++)
  145.         {
  146.             if(numfin % foo == 0 && denomfin % foo == 0)
  147.             {
  148.                 numprin = numfin/foo;
  149.                 denomprin = denomfin/foo;
  150.             }
  151.         }
  152.         boolean negative = (numprin<0 ^ denomprin<0);
  153.         if ((numprin>0 && denomprin>0) || (numprin < 0 && denomprin < 0))
  154.         {
  155.             while(Math.abs(numprin)>=Math.abs(denomprin))
  156.             {
  157.                 numprin-=denomprin;
  158.                 whole++;
  159.             }
  160.         }
  161.         else if ((numprin<0 && denomprin>0) || (numprin>0 && denomprin<0))
  162.         {
  163.             while(Math.abs(numprin)>=Math.abs(denomprin))
  164.             {
  165.                 numprin +=denomprin;
  166.                 whole--;
  167.             }
  168.         }
  169.         if (numprin<0 && denomprin<0)
  170.         {
  171.             numprin = Math.abs(numprin);
  172.             denomprin = Math.abs(denomprin);
  173.         }
  174.         else if (numprin > 0 && denomprin <0)
  175.         {
  176.             if (whole > 0)
  177.                 numprin *= -1;
  178.             denomprin *= -1;
  179.         }
  180.         else if (whole < 0 && numprin < 0)
  181.             numprin *=-1;
  182.         if(whole==0 && numprin != 0)
  183.         {
  184.             String ans = (numprin + "/" + denomprin);
  185.             return ans;
  186.         }
  187.         else if (whole !=0 && numprin == 0)
  188.             return (""+whole);
  189.         else if (whole == 0 && numprin == 0)
  190.             return ("0");
  191.         else
  192.         {
  193.             String ans = (whole + "_" + numprin + "/" + denomprin);
  194.             return ans;
  195.         }
  196.     }
  197.  
  198. }
  199. //  1/2 + 3/4 - 5/6 * 7/8 / 9/10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement