Skip to content

Instantly share code, notes, and snippets.

@reubn
Created October 24, 2017 21:41
Show Gist options
  • Save reubn/b76aee3a182608c307388b00cc107878 to your computer and use it in GitHub Desktop.
Save reubn/b76aee3a182608c307388b00cc107878 to your computer and use it in GitHub Desktop.
Exercise04SPOC.java created by reu - https://repl.it/NKMw/20
import java.util.Scanner;
// YOU NEED TO CHANGE THIS LINE BACK TO WHAT IT WAS, BUT IT WOULD ONLY WORK ON THIS WEBSITE IF I CHANGED IT :)
// public class Exercise04SPOC {
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
int num1, den1, num2, den2, numRes = 0, denRes = 0, numSimplified = 0, denSimplified = 0, i;
System.out.println("Please enter the numerator of your fraction:");
num1 = input.nextInt();
System.out.println("Please enter the denominator:");
den1 = input.nextInt();
// For the operations we have to declare a String:
String op;
System.out.println("Choose the operation you want to perform: \n Addition (+) \n Subtraction (-) \n Multiplication (*) \n Division (:) \n Calculate the Opposite (O) \n Inverse (I) \n Quit (S)");
op = input2.nextLine();
// Exit Program
if (op.equalsIgnoreCase("S")) {
System.exit(-1);
}
// Opposite ??? Wtf is the opposite of a fraction
if (op.equalsIgnoreCase("O")) {
// This is a guess
numRes = -num1;
denRes = den1;
}
// Inverse
if (op.equalsIgnoreCase("I")) {
numRes = den1;
denRes = num1;
}
// Multiplication
if (op.equalsIgnoreCase("*")) {}
// Division
if (op.equalsIgnoreCase(":")) {}
// Addition
if (op.equals("+")) {
System.out.println("Please enter the second numerator:");
num2 = input.nextInt();
System.out.println("Please enter the second denominator:");
den2 = input.nextInt();
// We have to operate the fractions now, looking for a common denominator.
if (den1 == den2) {
numRes = num1 + num2;
denRes = den1;
} else {
numRes = (den2 * num1) + (den1 * num2);
denRes = den1 * den2;
}
}
// Subtraction
if (op.equals("-")) {
System.out.println("Please introduce the second numerator: ");
num2 = input.nextInt();
System.out.println("Please introduce the second denominator: ");
den2 = input.nextInt();
// We have to operate the fractions now
if (den1 == den2) {
numRes = num1 - num2;
denRes = den1;
} else {
numRes = (den2 * num1) - (den1 * num2);
denRes = den1 * den2;
}
}
// If numerator == denominator, then we should output 1
if(numRes == denRes) {
System.out.println("The result of your operation is: 1");
// This just skips the rest of the code
return;
}
// Simplification
int highestCommonFactor = 1;
// Math.min returns the smallest of the 2 numbers - as it's pointless trying when i is bigger than one of the number, bcs you'll get something < 1
// I think your problem was that if you hadn't found a common factor, you set the num/denSimplified to num/denRes
// So say 44/22, you find 11 is a common factor, then you try 12 and it doesn't work, so your code would have set num/denSimplified back to 44 and 11...which is wrong
// So in mine, if a number isn't a common factor, you do nothing, bcs it doesn't matter. However you need to continue looking, bcs there may be a higher one like 22 in the example ^^, but when you try 23, and it doesn't work, the code does nothing....which is correct
for (i = 1; i <= Math.min(numRes, denRes); i++) {
if((numRes % i) == 0 && (denRes % i) == 0) {
highestCommonFactor = i;
}
}
// Then here obviously you divide by the highest common factor to get the numbers
numSimplified = numRes / highestCommonFactor;
denSimplified = denRes / highestCommonFactor;
// If the denominator is < 0, then swap the signs (this also handles the case where both numbers are < 0)
if(denSimplified < 0) {
numSimplified = -numSimplified;
denSimplified = -denSimplified;
}
// Now we check for fractions where the denominator is 1, and to see if the numerator is 0
// If either are true, we should just print the numerator
if(denSimplified == 1 || numSimplified == 0) {
System.out.println("The result of your operation is: " + numSimplified);
} else {
System.out.println("The result of your operation is: " + numSimplified + "/" + denSimplified);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment