Java—分数四则运算
Description
编写程序,实现两个分数的加减法
Input
输入包含多行数据;
每行数据是一个字符串,格式是"a/boc/d",其中a, b, c, d为数字(每个数字保证为正数并且不存在正号)。o是运算符"+“或者”-","*",""。
数据以EOF结束,输入数据保证合法。
Output
直接输出结果,并且注意结果应符合书写习惯,没有多余的符号、分子、分母,并且化简至最简分数形式。
Sample
Input
1/100+3/100
1/4-1/2
1/3-1/3
1/2*2/1
1/2\1/2
Output
1/25
-1/4
0
1
1
import java.util.Scanner;
class Opertion {
int a, b, c, d, m, n;
public Opertion(int a, int b, int c, int d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
//分式约分
public void print(int m, int n) {
int x = Math.abs(m);
int y = Math.abs(n);
if (x < y) {
int t = x;
x = y;
y = t;
}
while (y != 0) {
int k = x % y;
x = y;
y = k;
}
m = m / x;
n = n / x;
if (n == 1)
System.out.println(m);
else
System.out.println(m + "/" + n);
}
//四则运算
public void sum() {
m = a * d + b * c;
n = b * d;
print(m, n);
}
public void jian() {
m = a * d - b * c;
n = b * d;
print(m, n);
}
public void cheng() {
m = a * c;
n = b * d;
print(m, n);
}
public void chu() {
m = a * d;
n = b * c;
print(m, n);
}
//判断四则运算类型
public void oper(String s) {
for (int i = 0; i < s.length(); i++) {
char ss = s.charAt(i);
if (ss == '+') {
sum();
break;
} else if (ss == '-') {
jian();
break;
} else if (ss == '*') {
cheng();
break;
} else if (ss == '\\') {
chu();
break;
}
}
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
while (reader.hasNext()) {
String str;
str = reader.nextLine();
String s1[] = str.split("\\+|\\-|\\*|\\\\");//分离出两个分式
String fir[] = s1[0].split("/");//将第一个分式的分子和分母分离并求出值
int a = Integer.parseInt(fir[0]);
int b = Integer.parseInt(fir[1]);
String sec[] = s1[1].split("/");//将第二个分式的分子和分母分离并求出值
int c = Integer.parseInt(sec[0]);
int d = Integer.parseInt(sec[1]);
Opertion opertion = new Opertion(a, b, c, d);
opertion.oper(str);
}
}
}