Java语言实现分数的加减乘除
Java:
import java.util.Scanner;
class Fraction{ // 分数类
public int molecule, denominator; // 分子, 分母
public Fraction() {
molecule = 1;
denominator = 1;
}
}
public class TestFive {
private static Fraction a, b; // 两个分数
static Scanner sc = new Scanner(System.in);
// 最大公约数方法
public static int greatestCommonDivisor(int a, int b) {
return (a % b == 0) ? b : greatestCommonDivisor(b, a%b);
}
// 最小公倍数
public static int leastCommonMultiple(int a, int b) {
return a*b/greatestCommonDivisor(a, b);
}
// 加法
public static Fraction add(Fraction a, Fraction b) {
Fraction result = new Fraction(); // 计算结果
// 通分并计算
int denleacommul = leastCommonMultiple(a.denominator, b.denominator); // 两个分数的分母的最小公倍数
int temp1 = denleacommul/a.denominator;
int temp2 = denleacommul/b.denominator;
result.molecule = a.molecule*temp1 + b.molecule*temp2; // 分子相加
result.denominator = denleacommul; // 分母不变
// 约分
int moldengrecomdiv = greatestCommonDivisor(Math.abs(result.molecule), Math.abs(result.denominator));// 不考虑符号
result.denominator /= moldengrecomdiv;
result.molecule /= moldengrecomdiv;
// 返回结果
return result;
}
// 减法
public static Fraction subtraction(Fraction a, Fraction b) {
Fraction result = new Fraction(); // 计算结果
// 通分并计算
int denleacommul = leastCommonMultiple(a.denominator, b.denominator); // 两个分数的分母的最小公倍数
int temp1 = denleacommul/a.denominator;
int temp2 = denleacommul/b.denominator;
result.molecule = a.molecule*temp1 - b.molecule*temp2; // 分子相减
result.denominator = denleacommul; // 分母不变
// 约分
int moldengrecomdiv = greatestCommonDivisor(Math.abs(result.molecule), Math.abs(result.denominator));// 不考虑符号
result.denominator /= moldengrecomdiv;
result.molecule /= moldengrecomdiv;
// 返回结果
return result;
}
// 乘法
public static Fraction multiplication(Fraction a, Fraction b) {
Fraction result = new Fraction(); // 计算结果
// 计算
result.molecule = a.molecule*b.molecule;
result.denominator = a.denominator*b.denominator;
// 约分
int moldengrecomdiv = greatestCommonDivisor(Math.abs(result.molecule), Math.abs(result.denominator));// 不考虑符号
result.denominator /= moldengrecomdiv;
result.molecule /= moldengrecomdiv;
// 返回结果
return result;
}
// 除法
public static Fraction division(Fraction a, Fraction b) {
Fraction result = new Fraction(); // 计算结果
// 计算
result.molecule = a.molecule*b.denominator;
result.denominator = a.denominator*b.molecule;
if(result.denominator < 0) { // 分母不为负的
result.molecule = -result.molecule;
result.denominator = -result.denominator;
}
// 约分
int moldengrecomdiv = greatestCommonDivisor(Math.abs(result.molecule), Math.abs(result.denominator));// 不考虑符号
result.denominator /= moldengrecomdiv;
result.molecule /= moldengrecomdiv;
// 返回结果
return result;
}
// 读取分子和分母
public static void inputMolDen(Fraction a) {
a.molecule = 0;
a.denominator = 0;
String inputfraction = sc.next();
/*
* boolean flag = true; // 标记符号
* int count = -1; // count为负是读分子,count为正时读分母
* for(int i = 0;i < inputfraction.length();i++) {
* if(inputfraction.charAt(i) == '-') // 如果分数是负的, flag 标记为 false
* flag = false;
* else if(Character.isDigit(inputfraction.charAt(i)) && count < 0){
* if(count != -1)
* a.molecule *= 10;
* count--;
* a.molecule += Integer.valueOf(String.valueOf(inputfraction.charAt(i)));
* }else if(inputfraction.charAt(i) == '/') { // 分子读完, 变 count 为正, 读分母
* count = 1;
* }else if(count > 0){
* if(count != 1)
* a.denominator *= 10;
* count++;
* a.denominator += Integer.valueOf(String.valueOf(inputfraction.charAt(i)));
* }
* }
* // 处理符号
* if(flag == false)
* a.molecule = -a.molecule;
*
* 以上注释代码也可以实现下面功能
*/
boolean flag = true; // 标记符号
int molstart = 0, molend = 0, i = 0;
while(i < inputfraction.length()) { // 找分子的开头
if(Character.isDigit(inputfraction.charAt(i))){
molstart = i;
break;
}
i++;
}
for(i =0;i < inputfraction.length();i++) {
if(inputfraction.charAt(i) == '-') // 如果分数是负的, flag 标记为 false
flag = false;
if(inputfraction.charAt(i) == '/') {
molend = i; // 分子的末尾 + 1
break;
}
}
a.molecule = Integer.parseInt(inputfraction.substring(molstart, molend));
a.denominator = Integer.parseInt(inputfraction.substring(molend+1, inputfraction.length()));
// 处理符号
if(flag == false)
a.molecule = -a.molecule;
}
// 分数字符串
public static String stringFraction(Fraction a) {
if(a.denominator == 1) // 分母为 1
return String.valueOf((a.molecule));
else
return (a.molecule + "/" + a.denominator);
}
public static void main(String[] args){
try {
a = new Fraction();
System.out.print("a = ");
inputMolDen(a); // 输入 a
b = new Fraction();
System.out.print("b = ");
inputMolDen(b); // 输入 b
System.out.println("a+b = " + stringFraction(add(a, b)));
System.out.println("a-b = " + stringFraction(subtraction(a, b)));
System.out.println("a*b = " + stringFraction(multiplication(a, b)));
System.out.println("a/b = " + stringFraction(division(a, b)));
}catch(ArithmeticException e) {
System.out.println("分母出现零");
}
}
}
/* Code Running Results
a = 1/3
b = -1/2
a+b = -1/6
a-b = 5/6
a*b = -1/6
a/b = -2/3
*/