Four arithmetic operations for fractions

Four arithmetic operations for fractions

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

Write a program to add and subtract two fractions

Input

input contains multiple lines of data;

Each line of data is a string in the format "a/boc/d", where a, b, c, d are numbers (each number is guaranteed to be positive and no positive sign exists). o is the operator "+" or "-", "*", "\".

The data ends with EOF, and the input data is guaranteed to be legal.

Output

Output the result directly, and note that the result should conform to writing habits, without redundant symbols, numerators, and denominators, and simplified to the simplest fractional form.

Sample Input

1/100+3/100
1/4-1/2
1/3-1/3
1/2*2/1
1/2\1/2

Sample Output

1/25
-1/4
0
1
1
 
  
package hello;

import java.util. *;

class Fenshu {

	int fenzi;
	int fenmu;

	public Fenshu(int fenzi, int fenmu) { // constructor
		this.fenzi = fenzi;
		this.fenmu = fenmu;
	}

	public Fenshu() {
	}
	public Fenshu add(Fenshu Fenshu) { // Define the addition method
		int newfenzi = fenzi * Fenshu.fenmu + fenmu * Fenshu.fenzi;
		int newfenmu = fenmu * Fenshu.fenmu;
		int gcd = gcd(newfenzi, newfenmu); // find the greatest common divisor for equal fractions
		return new Fenshu(newfenzi / gcd, newfenmu / gcd);
	}

	public Fenshu sub(Fenshu Fenshu) {
		int newfenzi = fenzi * Fenshu.fenmu - fenmu * Fenshu.fenzi;
		int newfenmu = fenmu * Fenshu.fenmu;
		int gcd = gcd(newfenzi, newfenmu);
		return new Fenshu(newfenzi / gcd, newfenmu / gcd);
	}
	public Fenshu mul(Fenshu Fenshu) {
		int newfenzi = fenzi*Fenshu.fenzi;
		int newfenmu = fenmu*Fenshu.fenmu;
		int gcd = gcd(newfenzi, newfenmu);
		return new Fenshu(newfenzi/gcd, newfenmu/gcd);
	}
	public Fenshu div(Fenshu Fenshu) {
		int newfenzi = fenzi*Fenshu.fenmu;
		int newfenmu = fenmu*Fenshu.fenzi;
		int gcd = gcd(newfenzi, newfenmu);
		return new Fenshu(newfenzi/gcd, newfenmu/gcd);
	}
	public int gcd(int a, int b) { // 辗转相除求最大公约数

		int m = Math.max(Math.abs(a), Math.abs(b));
		int n = Math.min(Math.abs(a), Math.abs(b));
		int r;
		while (n != 0) {
			r = m % n;
			m = n;
			n = r;
		}
		return m;
	}
	public String toString(){   //定义输出对象的格式
		if ((fenzi % fenmu) == 0)
			return fenzi/fenmu+"";
		else
			return fenzi + "/" + fenmu;
	}
}

public class Main {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {

			String str = in.next();
			char[] s = str.toCharArray();
			int center = 0;
			int flag = 0;
			int i;
			for (i = 0; i < str.length(); i++) {
				if (s[i] == '+') {
					center = i;
					flag = 1;
					break;
				} else if (s[i] == '-') {
					center = i;
					flag = 2;
					break;
				}else if(s[i] == '*') {
					center = i;
					flag = 3;
					break;
				}else if(s[i] == '\\') {
					center = i;
					flag = 4;
					break;
				}
			}

			String s1 = str.substring(0, center); // 截取字符串
			String s2 = str.substring(center + 1, str.length());
			String[] p = s1.split("\\/"); // 截取字符串 取得数字
			String[] q = s2.split("\\/");
			int a = Integer.parseInt(p[0]);
			int b = Integer.parseInt(p[1]);
			int c = Integer.parseInt(q[0]);
			int d = Integer.parseInt(q[1]);
			Fenshu Fenshu1 = new Fenshu(a, b); // 构造两个分数对象
			Fenshu Fenshu2 = new Fenshu(c, d);

			Fenshu result = new Fenshu();

			if (flag == 1)
				result = Fenshu1.add(Fenshu2);
			else if (flag == 2)
				result = Fenshu1.sub(Fenshu2);
			else if(flag == 3)
				result = Fenshu1.mul(Fenshu2);
			else if(flag == 4)
				result = Fenshu1.div(Fenshu2);
			System.out.println(result);
		}
		in.close();
	}
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735393&siteId=291194637