Java语⾔实现分数的加减乘除
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.denominator = denleacommul;// 分母不变
// 约分
int moldengrecomdiv =greatestCommonDivisor(Math.lecule), Math.abs(result.denominator));// 不考虑符号 result.denominator /= 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.denominator = denleacommul;// 分母不变
// 约分
int moldengrecomdiv =greatestCommonDivisor(Math.lecule), Math.abs(result.denominator));// 不考虑符号 result.denominator /= moldengrecomdiv;
// 返回结果
return result;
}
// 乘法
public static Fraction multiplication(Fraction a, Fraction b){
Fraction result =new Fraction();// 计算结果
// 计算
// 计算
result.denominator = a.denominator*b.denominator;
// 约分
int moldengrecomdiv =greatestCommonDivisor(Math.lecule), Math.abs(result.denominator));// 不考虑符号 result.denominator /= moldengrecomdiv;
// 返回结果
return result;
}
// 除法
public static Fraction division(Fraction a, Fraction b){
Fraction result =new Fraction();// 计算结果
// 计算
result.denominator = a.lecule;
if(result.denominator <0){// 分母不为负的
result.denominator =-result.denominator;
}
/
/ 约分
int moldengrecomdiv =greatestCommonDivisor(Math.lecule), Math.abs(result.denominator));// 不考虑符号 result.denominator /= moldengrecomdiv;
// 返回结果
return result;
}
// 读取分⼦和分母
public static void inputMolDen(Fraction a){
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++;
}
}
java valueoffor(i =0;i < inputfraction.length();i++){
if(inputfraction.charAt(i)=='-')// 如果分数是负的, flag 标记为 false
flag =false;
if(inputfraction.charAt(i)=='/'){
molend = i;// 分⼦的末尾 + 1
break;
}
}
a.denominator = Integer.parseInt(inputfraction.substring(molend+1, inputfraction.length())); // 处理符号
if(flag ==false)
}
// 分数字符串
public static String stringFraction(Fraction a){
if(a.denominator ==1)// 分母为 1
return String.valueOf((a.molecule));
else
lecule +"/"+ 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
*/
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论