Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package parseToArray;
- import java.util.Scanner;
- // "\\d+/\\d+"
- public class ArrayParse {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- //HashSet<String> operator = new HashSet<String>(Arrays.asList("+","-","*","/"));
- int foo=0;
- String ans = new String();
- while(true)
- {
- System.out.print("Enter your expression \nType \"quit\" to quit\n");
- String input = console.nextLine();
- if(input.equals("quit"))
- break;
- else if(input.matches("( ?[+-/*] (([+-]?\\d+_)?[+-]?\\d+/[+-]?\\d+)|([+-]?\\d+))+") && foo > 0)
- {
- if(input.charAt(0)!= ' ')
- input = " " + input;
- String mutie = (ans + " " + input);
- System.out.println(parse(mutie));
- }
- else if (input.matches("((([+-]?\\d+_)?[+-]?\\d+/[+-]?\\d+)|([+-]?\\d+))( [+-/*] (([+-]?\\d+_)?[+-]?\\d+/[+-]?\\d+)|([+-]?\\d+))+"))
- {
- ans = parse(input);
- System.out.println(ans);
- foo++;
- }
- else
- System.out.println("INVALID INPUT");
- }
- System.out.println("TERMINATED");
- console.close();
- }
- public static String parse(String input)
- {
- int spacecount = input.length() - input.replace(" ", "").length();
- int operandscount = 1 + spacecount/2;
- String[] operand = new String[operandscount + 1];
- String[] flubber = new String[operandscount + 1];
- flubber[0] = input;
- char[] operator = new char[operandscount];
- int place = 1;
- int space = 0;
- int nustart = 0;
- while(nustart < input.length())
- {
- flubber[place] = input.substring(nustart, input.length());
- space = flubber[place].indexOf(" ");
- if(place == operandscount)
- {
- operand[place] = flubber[place];
- break;
- }
- operand[place] = flubber[place].substring(0,space);
- operator[place] = flubber[place].charAt(space + 1);
- nustart += (operand[place].length() + 3);
- place++;
- }
- /*for(int baz = 1; baz<=operandscount; baz++)
- {
- if(!(operand[baz].matches("\\d+_\\d+/\\d+") || operand[baz].matches("\\d+/\\d+") || operand[baz].matches("\\d+")))
- return "INVALID OPERAND";
- }
- for(int xyzzy = 1; xyzzy<operandscount; xyzzy++)
- {
- if(!OPERATOR.contains("" + operator[xyzzy]))
- return "INVALID OPERATOR";
- }*/
- int[] num = new int[operandscount + 1];
- int[] denom = new int[operandscount + 1];
- for(int foo = 1; foo <= operandscount; foo++)
- {
- num[foo] = getUnredFracPart(operand[foo], true);
- denom[foo] = getUnredFracPart(operand[foo], false);
- }
- int[] operatedNum = new int[operandscount + 1];
- int[] operatedDenom = new int[operandscount + 1];
- operatedNum[1] = num[1];
- operatedDenom[1] = denom[1];
- for(int bar = 2; bar <= operandscount; bar++)
- {
- if(operator[bar-1] == '+')
- {
- operatedNum[bar] = (operatedNum[bar-1]*denom[bar] + num[bar]*operatedDenom[bar-1]);
- operatedDenom[bar] = (operatedDenom[bar-1] * denom[bar]);
- }
- else if(operator[bar-1] == '*')
- {
- operatedNum[bar] = (operatedNum[bar-1] * num[bar]);
- operatedDenom[bar] = (operatedDenom[bar-1] * denom[bar]);
- }
- else if(operator[bar-1] == '/')
- {
- operatedNum[bar] = (operatedNum[bar-1] * denom[bar]);
- operatedDenom[bar] = (operatedDenom[bar-1] * num[bar]);
- }
- else if(operator[bar-1] == '-')
- {
- operatedNum[bar] = (operatedNum[bar-1]*denom[bar] - num[bar]*operatedDenom[bar-1]);
- operatedDenom[bar] = (operatedDenom[bar-1] * denom[bar]);
- }
- else
- return "INVALID OPERATOR";
- }
- return formatAnswer(operatedNum[operandscount], operatedDenom[operandscount]);
- }
- public static int getUnredFracPart(String operand, boolean wantNum)
- {
- int div = operand.indexOf("/");
- int mix = operand.indexOf("_");
- if(div!= -1)
- {
- if(wantNum == true)
- {
- int num = Integer.parseInt(operand.substring(mix + 1,div));
- if(mix != -1)
- num += mix * Integer.parseInt(operand.substring(div+1));
- return num;
- }
- else
- {
- String outstring = operand.substring(div+1);
- return Integer.parseInt(outstring);
- }
- }
- else
- {
- if(wantNum == true)
- {
- int num = Integer.parseInt(operand);
- return num;
- }
- else
- return 1;
- }
- }
- public static String formatAnswer(int numfin, int denomfin)
- {
- int numprin = 0;
- int denomprin = 0;
- int whole = 0;
- for(int foo = 1; foo<=Math.abs(numfin); foo++)
- {
- if(numfin % foo == 0 && denomfin % foo == 0)
- {
- numprin = numfin/foo;
- denomprin = denomfin/foo;
- }
- }
- boolean negative = (numprin<0 ^ denomprin<0);
- if ((numprin>0 && denomprin>0) || (numprin < 0 && denomprin < 0))
- {
- while(Math.abs(numprin)>=Math.abs(denomprin))
- {
- numprin-=denomprin;
- whole++;
- }
- }
- else if ((numprin<0 && denomprin>0) || (numprin>0 && denomprin<0))
- {
- while(Math.abs(numprin)>=Math.abs(denomprin))
- {
- numprin +=denomprin;
- whole--;
- }
- }
- if (numprin<0 && denomprin<0)
- {
- numprin = Math.abs(numprin);
- denomprin = Math.abs(denomprin);
- }
- else if (numprin > 0 && denomprin <0)
- {
- if (whole > 0)
- numprin *= -1;
- denomprin *= -1;
- }
- else if (whole < 0 && numprin < 0)
- numprin *=-1;
- if(whole==0 && numprin != 0)
- {
- String ans = (numprin + "/" + denomprin);
- return ans;
- }
- else if (whole !=0 && numprin == 0)
- return (""+whole);
- else if (whole == 0 && numprin == 0)
- return ("0");
- else
- {
- String ans = (whole + "_" + numprin + "/" + denomprin);
- return ans;
- }
- }
- }
- // 1/2 + 3/4 - 5/6 * 7/8 / 9/10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement