Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.*;
- import java.util.*;
- public class FractionalCalculator
- {
- public static void main(String[] args)
- {
- Scanner console = new Scanner(System.in);
- initialization();
- fractionalTesting();
- boolean error = false;
- while (error == false)
- {
- String operation = console.nextLine();
- int operationType = -1;
- int inputType = -1;
- //first input type test
- char[] character_ = new char[1000];
- char[] character__ = new char[1000];
- int slashcount = 0;
- int[] slashlocation = new int[1000];
- int underscorecount = 0;
- int[] underscorelocation = new int[1000];
- int spacecount = 0;
- int[] spacelocation = new int[1000];
- int dividecount = 0;
- int[] dividelocation = new int[1000];
- int multiplycount = 0;
- int[] multiplylocation = new int[1000];
- int addcount = 0;
- int[] addlocation = new int[1000];
- int subtractcount = 0;
- int[] subtractlocation = new int[1000];
- int negativecount = 0;
- int[] negativelocation = new int[1000];
- negativelocation[1] = -1;
- //count and location
- for (int i = 0; i < operation.length(); i++)
- {
- character_[i] = operation.charAt(i);
- if (character_[i] == '*')
- {
- if (operation.charAt(i-1) == ' ' && operation.charAt(i+1) == ' ')
- {
- operationType = 1;
- multiplycount += 1;
- multiplylocation[multiplycount] = i;
- }
- }
- if (character_[i] == '+')
- {
- if (operation.charAt(i-1) == ' ' && operation.charAt(i+1) == ' ')
- {
- operationType = 3;
- addcount += 1;
- addlocation[addcount] = i;
- }
- }
- if (character_[i] == '-')
- {
- //potential for one bug here
- if (operation.charAt(i+1) == ' ')
- {
- operationType = 4;
- subtractcount += 1;
- subtractlocation[subtractcount] = i;
- }
- if (Character.isDigit(operation.charAt(i+1)) == true)
- {
- System.out.println("AT " + i + "THERE IS A -");
- negativecount += 1;
- negativelocation[negativecount-1] = i;
- }
- if (i > 0)
- {
- if (Character.isDigit(operation.charAt(i+1)) == true && operation.charAt(i-1) == ' ')
- {
- negativecount += 1;
- negativelocation[negativecount] = i;
- }
- }
- }
- if (character_[i] == ' ')
- {
- if (i == 0)
- {
- error = true;
- System.out.println("space before first number error");
- }
- if (i == (operation.length() - 1))
- {
- error = true;
- System.out.println("space after last number error");
- }
- if (operation.charAt(i-1) != ' ' && operation.charAt(i+1) == '-')
- {
- spacecount += 1;
- spacelocation[spacecount] = i;
- }
- if (Character.isDigit(operation.charAt(i-1)) == true && operation.charAt(i+1) != ' ')
- {
- spacecount += 1;
- spacelocation[spacecount] = i;
- }
- if (Character.isDigit(operation.charAt(i+1)) ==true && operation.charAt(i-1) != ' ')
- {
- spacecount += 1;
- spacelocation[spacecount] = i;
- }
- //FOR BUG TEST OVERHAUL (going to take like 20 minutes to sort the stupid extra spaces
- /*if (i == 0)
- {
- for (int j = 0; j < operation.length(); j++)
- {
- character__[i] = operation.charAt(i);
- }
- }*/
- }
- if (character_[i] == '/')
- {
- if (operation.charAt(i-1) == ' ' && operation.charAt(i+1) == ' ')
- {
- operationType = 2;
- dividecount += 1;
- dividelocation[dividecount] = i;
- }
- if (Character.isDigit(operation.charAt(i-1)) == true && Character.isDigit(operation.charAt(i+1)) == true)
- {
- slashcount += 1;
- slashlocation[slashcount] = i;
- System.out.println();
- }
- if (operation.charAt(i-1) != ' ' && operation.charAt(i+1) != ' ')
- {
- if (Character.isDigit(operation.charAt(i-1)) == false || Character.isDigit(operation.charAt(i+1)) == false)
- {
- System.out.println("no space or digit around / error");
- error = true;
- }
- }
- }
- if (character_[i] == '_')
- {
- underscorecount += 1;
- underscorelocation[underscorecount] = i;
- }
- }
- //System.out.println("fbbarcount: " + slashcount);
- //System.out.println("_count: " + underscorecount);
- //type sorter
- //whole number + whole number
- if (underscorecount == 0 && slashcount == 0)
- {
- inputType = 1;
- }
- //whole number + fraction
- else if (underscorecount == 0 & slashcount == 1 && slashlocation[1] > spacelocation[2])
- {
- inputType = 2;
- }
- //whole number + mixed number
- else if (underscorecount == 1 && underscorelocation[1] > spacelocation[2] && slashcount == 1 && slashlocation[1] > spacelocation[2])
- {
- inputType = 3;
- }
- //fraction + whole number
- else if (underscorecount == 0 && slashcount == 1 && slashlocation[1] < spacelocation[1])
- {
- inputType = 4;
- }
- //fraction + fraction
- else if (underscorecount == 0 && slashcount == 2 && slashlocation[1] < spacelocation[1] && slashlocation[2] > spacelocation[2])
- {
- inputType = 5;
- }
- ///fraction + mixed number
- else if (underscorecount == 1 & underscorelocation[1] > spacelocation[2] && slashcount == 2 && slashlocation[1] < spacelocation[1])
- {
- inputType = 6;
- }
- //mixed number + whole number
- else if (underscorecount == 1 && underscorelocation[1] < spacelocation[1] && slashcount == 1)
- {
- inputType = 7;
- }
- //mixed number + fraction
- else if (underscorecount == 1 & underscorelocation[1] < spacelocation[1] && slashcount == 2 && slashlocation[2] > spacelocation[2])
- {
- inputType = 8;
- }
- //mixed number + mixed number
- else if (underscorecount == 2 && slashcount == 2)
- {
- inputType = 9;
- }
- else
- {
- System.out.println("too many underscores error");
- error = true;
- }
- System.out.println("negative count = " + negativecount + "negative location =" + negativelocation[1]);
- //initializations
- char character = '0';
- int wholenumber1 = 0;
- int numerator1 = 0;
- int denominator1 = 0;
- int wholenumber2 = 0;
- int numerator2 = 0;
- int denominator2 = 0;
- //first error test, does the whole number have any alphabetic values? if so, error = true.
- for (int i = 0; i < operation.length(); i++)
- {
- character_[i] = operation.charAt(i);
- if (Character.isAlphabetic(character_[i]))
- {
- error = true;
- System.out.println("alphabet detected");
- }
- }
- //converts the whole number given in the string to an int value to be used for math! :)
- boolean negativewhole1 = false;
- int negoffset1 = 0;
- int negmultiplier1 = 1;
- int negoffset2 = 0;
- int negmultiplier2 = 1;
- boolean negativewhole2 = false;
- //first word test
- int firstword = 0;
- //second word test
- int secondword = 0;
- //negative offset boolean test
- if (negativelocation[1] == 0)
- {
- negoffset1 = 1;
- negmultiplier1 = -1;
- negativewhole1 = true;
- }
- //System.out.println("NEGLOCATION1: " + negativelocation[1]);
- if (negativelocation[2] == spacelocation[2] + 1)
- {
- negoffset2 = 1;
- negmultiplier2 = -1;
- negativewhole2 = true;
- }
- //wholenumber test for mixed numbers
- if (inputType == 7 || inputType == 8 || inputType == 9)
- {
- for (int i = 0 + negoffset1; i <= underscorelocation[1]; i++)
- {
- character = operation.charAt(i);
- int numcharacter = Character.getNumericValue(character);
- wholenumber1 += numcharacter * Math.pow(10, underscorelocation[1]-i-1);
- }
- wholenumber1 += 1;
- wholenumber1 *= negmultiplier1;
- firstword++;
- }
- //whole number test for whole numbers
- if (inputType == 1 || inputType == 2 || inputType == 3)
- {
- for (int i = 0 + negoffset1; i <= spacelocation[1]; i++)
- {
- character = operation.charAt(i);
- int numcharacter = Character.getNumericValue(character);
- wholenumber1 += numcharacter * Math.pow(10, spacelocation[1]-i-1);
- }
- if (operation.charAt(0) != '-')
- {
- wholenumber1 += 1;
- }
- wholenumber1 *= negmultiplier1;
- firstword++;
- if (operation.charAt(0) == '0')
- {
- wholenumber1 = 0;
- }
- if (operation.charAt(1) == '0' && operation.charAt(0) == '-')
- {
- wholenumber1 = 0;
- }
- }
- //converts first numerator of mixednumber to math
- if (inputType == 7 || inputType == 8 || inputType == 9)
- {
- for (int i = underscorelocation[1]+1; i < slashlocation[1]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- numerator1 += characternum * Math.pow(10,slashlocation[1] - i-1);
- }
- firstword++;
- }
- //converts first numerator of fraction to math
- if (inputType == 4 || inputType == 5 || inputType == 6)
- {
- for (int i = 0 + negoffset1; i < slashlocation[1]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- numerator1 += characternum * Math.pow(10,slashlocation[1] - i-1);
- }
- numerator1 *= negmultiplier1;
- firstword++;
- }
- //converts denominator to a number for math
- if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
- {
- for (int i = slashlocation[1]+1; i < spacelocation[1]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- denominator1 += characternum * Math.pow(10,spacelocation[1] - i-1);
- }
- firstword++;
- }
- //converts second whole number of mixednumber to a number for math
- if (inputType == 9)
- {
- if (negativelocation[2] != 0)
- {
- for (int i = negativelocation[2] + 1; i < underscorelocation[2]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- wholenumber2 += characternum * Math.pow(10,underscorelocation[2] - i-1);
- }
- if (operationType == 4)
- {
- wholenumber2 *= -1;
- }
- wholenumber2 *=negmultiplier2;
- System.out.println(negativelocation[2]);
- }
- else
- {
- for (int i = spacelocation[2] + 1; i < underscorelocation[2]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- wholenumber2 += characternum * Math.pow(10,underscorelocation[2] - i-1);
- }
- }
- secondword++;
- }
- if (inputType == 3 || inputType == 6)
- {
- if (negativelocation[2] != 0)
- {
- for (int i = negativelocation[2] + 1; i < underscorelocation[1]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- wholenumber2 += characternum * Math.pow(10,underscorelocation[1] - i-1);
- }
- if (operationType == 4)
- {
- wholenumber2 *= -1;
- }
- wholenumber2 *=negmultiplier2;
- System.out.println(negativelocation[2]);
- }
- else
- {
- System.out.println("ASDASDASD");
- for (int i = spacelocation[2] + 1; i < underscorelocation[1]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- wholenumber2 += characternum * Math.pow(10,underscorelocation[1] - i-1);
- }
- System.out.println("Spacelocation" + spacelocation[2] + " underscorelocation" + underscorelocation[1]);
- }
- secondword++;
- }
- /*{
- for (int i = spacelocation[2]+1 + negoffset2; i < underscorelocation[2]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- wholenumber2 += characternum * Math.pow(10,underscorelocation[2] - i-1);
- }
- wholenumber2 *= negmultiplier2;
- secondword++;
- }*/
- //converts second whole number to int value for math
- if (inputType == 1 || inputType == 4 || inputType == 7)
- {
- if (negativelocation[2] != 0)
- {
- for (int i = negativelocation[2] + 1; i < operation.length(); i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- wholenumber2 += characternum * Math.pow(10,operation.length() - i-1);
- }
- if (operationType == 4)
- {
- wholenumber2 *= -1;
- }
- wholenumber2 *=negmultiplier2;
- System.out.println(negativelocation[2]);
- }
- else
- {
- for (int i = spacelocation[2] + 1; i < operation.length(); i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- wholenumber2 += characternum * Math.pow(10,operation.length() - i-1);
- }
- }
- System.out.println((spacelocation[2]));
- System.out.println(operation.length());
- secondword++;
- }
- //converts second numerator of mixed number to a number for math
- if (inputType == 3 || inputType == 6)
- {
- System.out.println(underscorelocation[1] + " " + slashlocation[1]);
- for (int i = underscorelocation[1] + 1; i < slashlocation[1]; i++)
- {
- System.out.println("hi");
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- numerator2 += characternum * Math.pow(10,slashlocation[1] - i-1);
- System.out.print(numerator2);
- }
- secondword++;
- }
- if (inputType == 9)
- {
- for (int i = underscorelocation[2] + 1; i < slashlocation[2]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- numerator2 += characternum * Math.pow(10,slashlocation[2] - i-1);
- }
- secondword++;
- }
- int slashmodifier = 0;
- //bugfixing
- if (inputType == 2)
- {
- slashmodifier = 1;
- }
- if (inputType == 5 || inputType == 8)
- {
- slashmodifier = 2;
- }
- //converts second numerator of fraction to a number for math
- if (inputType == 2 || inputType == 5 || inputType == 8)
- {
- for (int i = spacelocation[2]+1 + negoffset2; i <slashlocation[slashmodifier]; i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- numerator2 += characternum * Math.pow(10,slashlocation[slashmodifier] - i-1);
- }
- numerator2 *= negmultiplier2;
- secondword++;
- }
- //converts second denominator to a number for math
- if (inputType == 2 || inputType == 3 || inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
- {
- System.out.println(slashlocation[1]+1);
- System.out.println(operation.length());
- for (int i = slashlocation[1]; i < operation.length(); i++)
- {
- character = operation.charAt(i);
- int characternum = Character.getNumericValue(character);
- denominator2 += characternum * Math.pow(10,operation.length() - i-1);
- }
- secondword++;
- }
- //fraction simplifier
- //fraction to mixed number
- if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
- {
- if (numerator1 > denominator1 && denominator1 != 0)
- {
- wholenumber1 += (numerator1 / denominator1);
- numerator1 = (numerator1 % denominator1);
- }
- }
- if (inputType == 2 || inputType == 3 || inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
- {
- if (numerator2 > denominator2 && denominator2 != 0)
- {
- wholenumber2 += (numerator2 / denominator2);
- numerator2 = (numerator2 % denominator2);
- }
- }
- int x1 = denominator1;
- int y1 = numerator1;
- int x2 = denominator2;
- int y2 = numerator2;
- if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
- {
- int z = 0;
- while (y1 != 0)
- {
- z = y1;
- y1 = x1 % y1;
- x1 = z;
- }
- }
- if (inputType == 2 || inputType == 3 || inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
- {
- int z = 0;
- while (y2 != 0)
- {
- z = y2;
- y2 = x2 % y2;
- x2 = z;
- }
- }
- if (inputType == 4 || inputType == 5 || inputType == 6 || inputType == 7 || inputType == 8 || inputType == 9)
- {
- numerator1 /= x1;
- denominator1 /= x1;
- }
- if (inputType == 2 || inputType == 3 || inputType == 5 || inputType == 6 || inputType == 8 || inputType == 9)
- {
- numerator2 /= x2;
- denominator2 /= x2;
- }
- String operationsorter = "";
- if (operationType == 1)
- {
- operationsorter = "multiplied by";
- }
- if (operationType == 2)
- {
- operationsorter = "divided by";
- }
- if (operationType == 3)
- {
- operationsorter = "added to";
- }
- if (operationType == 4)
- {
- operationsorter = "subtracted by";
- }
- String fword = " ";
- String sword = " ";
- if (firstword == 1)
- {
- fword = "whole number";
- }
- if (firstword == 2)
- {
- fword = "fraction";
- }
- if (firstword == 3)
- {
- fword = "mixed number";
- }
- if (secondword == 1)
- {
- sword = "whole number";
- }
- if (secondword == 2)
- {
- sword = "fraction";
- }
- if (secondword == 3)
- {
- sword = "mixed number";
- }
- System.out.println("You inputed a " + fword + " " + operationsorter + " a " + sword);
- System.out.println("Is this your input?");
- System.out.println("\n" + wholenumber1 + "_" + numerator1 + "/" + denominator1 + " " + operationsorter + " " + wholenumber2 + "_" + numerator2 + "/" + denominator2);
- if (operation == "quit")
- {
- error = true;
- }
- }
- }
- public static void initialization()
- {
- System.out.println("This is a fully functional fractional calculator with");
- System.out.println("four operations. The calculator accepts the number format");
- System.out.println("of a_b/c, so for example you'd write five and three tenths");
- System.out.println("as 5_3/10. Use + for addition, - for subtraction, * for");
- System.out.println("multiplication, and / for division.\n");
- System.out.println("EX.) 26_1/2 * 5_21/30");
- }
- public static void fractionalTesting()
- {
- }
- /*LIST OF THINGS TO DO
- *
- * TEST FOR BAD VALUES
- * FIX SUBTRACTION BUG
- *
- * MORE EXTENSIVE ERROR TESTING + MESSAGE SYSTEM
- *
- *
- *
- *
- *
- * operations
- *
- *
- * cleaner code
- * ??
- *
- *
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement