I am trying to convert a improper fraction to a mixed fraction using my calculator that I have developed for a course that I am taking. I'm stuck on how to go about this. Here is my code. Any help would be greatly appreciated
package fractioncalc;
import java.util.Scanner;
public class fractioncalc2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please input your equation");
String input = scan.nextLine().replaceAll(" ", "");
System.out.println(input);
boolean add = input.contains("+");
boolean sub = input.contains("-");
boolean multi = input.contains("*");
//add
if (add) {
String Nume1 = input.substring(0,input.indexOf("/"));
String Demo1 = input.substring(input.indexOf("/") + 1, input.indexOf("+"));
System.out.println(Demo1);
String Nume2 = input.substring(input.indexOf("+") + 1,input.lastIndexOf("/"));
String Demo2 = input.substring(input.lastIndexOf("/") + 1, input.length());
int num1 = Integer.parseInt(Nume1);
int den1 = Integer.parseInt(Demo1);
int num2 = Integer.parseInt(Nume2);
int den2 = Integer.parseInt(Demo2);
int numans = (num1*den2+num2*den1);
int denans = (den1*den2);
System.out.println("your answer is" + numans + "/" + denans);
}
//subtract
else if (sub) {
String Nume1 = input.substring(0,input.indexOf("/"));
String Demo1 = input.substring(input.indexOf("/") + 1, input.indexOf("-"));
String Nume2 = input.substring(input.indexOf("-") + 1,input.lastIndexOf("/"));
String Demo2 = input.substring(input.lastIndexOf("/") + 1, input.length());
int num1 = Integer.parseInt(Nume1);
int den1 = Integer.parseInt(Demo1);
int num2 = Integer.parseInt(Nume2);
int den2 = Integer.parseInt(Demo2);
int numans = (num1*den2-num2*den1);
int denans = (den1*den2);
System.out.println("your answer is" + numans + "/" + denans);
}
//multiply
else if (multi) {
String Nume1 = input.substring(0,input.indexOf("/"));
String Demo1 = input.substring(input.indexOf("/") + 1, input.indexOf("*"));
String Nume2 = input.substring(input.indexOf("*") + 1,input.lastIndexOf("/"));
String Demo2 = input.substring(input.lastIndexOf("/") + 1, input.length());
int num1 = Integer.parseInt(Nume1);
int den1 = Integer.parseInt(Demo1);
int num2 = Integer.parseInt(Nume2);
int den2 = Integer.parseInt(Demo2);
int numans = (num1*den2*num2*den1);
int denans = (den1*den2);
System.out.println("your answer is" + numans + "/" + denans);
}
//divide
else {
String b = input.substring(0, input.lastIndexOf("/"));
String c = input.substring(input.indexOf("/")+1);
String Nume1 = b.substring(0,input.indexOf("/"));
String Demo1 = b.substring(input.indexOf("/") + 1, input.lastIndexOf("/")-2);
String Nume2 = c.substring(input.indexOf("/") + 1,input.lastIndexOf("/")-2);
String Demo2 = c.substring(input.lastIndexOf("/") - 1);
int num1 = Integer.parseInt(Nume1);
int den1 = Integer.parseInt(Demo1);
int num2 = Integer.parseInt(Nume2);
int den2 = Integer.parseInt(Demo2);
int numans = (num1*den2);
int denans = (den1*num2);
System.out.println("your answer is: " + numans + "/" + denans);
}
}
}
Consider that a mixed fraction has two parts: a whole number and a fraction. The whole number is the amount of times numerator
fits into denominator
wholly (which is the floored value of numerator / denominator
). The numerator to the fraction is the remainder of this division, and the denominator does not change.
Thus, the algorithm would be (pseudocode):
Whole = Floor(Numerator / Denominator)
Part = Numerator % Denominator
Print(Whole + " " + Numerator + "/" + Denominator)
Note the %
symbol used here. This is referred to as modulo, and represents the remainder of the division of the two numbers. Java uses the same symbol.
For an implementation of Floor
, look into the Java Math
class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.