分数加减法

import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()) {
			String str=sc.next();
			char a[]=str.toCharArray();
			int fm=(a[2]-'0')*(a[6]-'0');
			int fz=a[3]=='+'?((a[0]-'0')*(a[6]-'0')+(a[4]-'0')*(a[2]-'0')):((a[0]-'0')*(a[6]-'0')-(a[4]-'0')*(a[2]-'0'));
			int x,y;
			if(fz<0) {
				x=-fz;
				y=-fm;
			}
			else {
				x=fz;
				y=fm;
			}
			int t;
			if(fz>fm) {
				t=fz;fz=fm;fm=t;
			}
			while(fz!=0) {
				int r=fm%fz;
				fm=fz;
				fz=r;
			}
			if((x/fm)%(y/fm)==0) {
				System.out.println((x/fm)/(y/fm));
			}
			else {
				System.out.println(x/fm+"/"+y/fm);
			}
		}
        sc.close();
	}
}

相关文章:

  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-06-04
  • 2021-06-26
  • 2022-12-23
猜你喜欢
  • 2021-09-25
  • 2021-09-25
  • 2022-12-23
  • 2021-07-31
  • 2021-11-29
相关资源
相似解决方案