分数四则运算

分数四则运算

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description

编写程序,实现两个分数的加减法

Input

输入包含多行数据;

每行数据是一个字符串,格式是"a/boc/d",其中a, b, c, d为数字(每个数字保证为正数并且不存在正号)。o是运算符"+"或者"-","*","\"。

数据以EOF结束,输入数据保证合法。

Output

直接输出结果,并且注意结果应符合书写习惯,没有多余的符号、分子、分母,并且化简至最简分数形式。

Example Input



import java.util.Scanner;


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 fk = 0; 
int fh = 0;
int i;
for( i=0; i<str.length(); i++ ){
    if( s[i]=='+' ){
    fk = i;
    fh = 1;
    break;
        }
    else if( s[i]=='-' ){
    fk = i;
    fh = 2;
    break;
    }
    else if( s[i]=='*' ){
    fk = i;
    fh = 3;
    break;
    }
    else if( s[i]=='\\' ){
    fk = i;
    fh = 4;
    break;
    }  
   }

String s1 = str.substring(0, fk);
   String s2 = str.substring(fk+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]);
Fs fs1 = new Fs(a, b);
Fs fs2 = new Fs(c, d);

Fs result = new Fs();
   
if( fh==1 )
result = fs1.add(fs2);
if( fh==2 )
result = fs1.sub(fs2);
if( fh==3 )
result = fs1.multiply(fs2);
if( fh==4 )
result = fs1.divide(fs2);
if( (result.fz%result.fm) == 0 )
System.out.println(result.fz/result.fm);
else
System.out.println(result.fz+"/"+result.fm);
}
in.close();
}
}
class Fs {

int fz;
int fm;

public Fs( int fz, int fm ){
this.fz = fz;
this.fm = fm;
}

public Fs(){}

public Fs add( Fs fs ){
int newFz = fz*fs.fm + fm*fs.fz;
int newFm = fm*fs.fm;
int gys = gys(newFz,newFm);
return new Fs(newFz/gys, newFm/gys);
}


public Fs sub( Fs fs ){
int newFz = fz*fs.fm - fm*fs.fz;
int newFm = fm*fs.fm;
int gys = gys(newFz,newFm); 
return new Fs(newFz/gys, newFm/gys);
}

public Fs multiply(Fs fs){
int newFz = fz*fs.fz;
int newFm = fm*fs.fm;
int gys = gys(newFz,newFm); 
return new Fs(newFz/gys, newFm/gys);


}

public Fs divide(Fs fs){
int newFz = fz*fs.fm;
int newFm = fm*fs.fz;
int gys = gys(newFz,newFm); 
return new Fs(newFz/gys, newFm/gys);


}
public int gys( 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;
}
}
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.Scanner;

	class cal{
		int fenzi;
		int fenmu;
		public cal(){
			
		}
		public cal(int fz,int fm){
			fenzi = fz;
			fenmu = fm;
		}
		public cal add(cal result){
			int newfenzi = fenzi * result.fenmu + fenmu * result.fenzi;
			int newfenmu = fenmu * result.fenmu;
			int gay = gay(newfenzi,newfenmu);
			return new cal(newfenzi/gay,newfenmu/gay);
		}
		public cal sub(cal result){
			int newfenzi = fenzi * result.fenmu - fenmu * result.fenzi;
			int newfenmu = fenmu * result.fenmu;
			int gay = gay(newfenzi,newfenmu);
			return new cal(newfenzi/gay,newfenmu/gay);
		}
		public cal mul(cal result){
			int newfenzi = fenzi * result.fenzi;
			int newfenmu = fenmu * result.fenmu;
			int gay = gay(newfenzi,newfenmu);
			return new cal(newfenzi/gay,newfenmu/gay);
		}
		public cal div(cal result){
			int newfenzi = fenzi * result.fenmu;
			int newfenmu = fenmu * result.fenzi;
			int gay = gay(newfenzi,newfenmu);
			return new cal(newfenzi/gay,newfenmu/gay);
		}
		public int gay(int newfenzi, int newfenmu) {
		
			int m = Math.max(Math.abs(newfenzi), Math.abs(newfenmu));
					int n = Math.min(Math.abs(newfenzi), Math.abs(newfenmu));
					while(n != 0){
						int r = m % n;
						m = n;
						n = r;
					}
			return m;
		}
	}
	
public class Main{
	 public static void main(String args[]){
	Scanner reader = new Scanner(System.in);
    while(reader.hasNext()){
    	String s = reader.next();
    	if(s.contains("+")){
    		int a = s.indexOf("+");
    		String s1 = s.substring(0, a);
    		String s2 = s.substring(a+1,s.length());
    		String p[] = s1.split("\\/");
    		String q[] = s2.split("\\/");
    		int b = Integer.parseInt(p[0]);
    		int c = Integer.parseInt(p[1]);
    		int d = Integer.parseInt(q[0]);
    		int e = Integer.parseInt(q[1]);
    		cal cal1 = new cal(b,c);
    		cal cal2 = new cal(d,e);
    		cal result  = cal1.add(cal2);
    		if((result.fenzi%result.fenmu) == 0)
    			System.out.println(result.fenzi/result.fenmu);
    		else System.out.println(result.fenzi+"/"+result.fenmu);
    	}
    	if(s.contains("-")){
    		int a = s.indexOf("-");
    		String s1 = s.substring(0, a);
    		String s2 = s.substring(a+1,s.length());
    		String p[] = s1.split("\\/");
    		String q[] = s2.split("\\/");
    		int b = Integer.parseInt(p[0]);
    		int c = Integer.parseInt(p[1]);
    		int d = Integer.parseInt(q[0]);
    		int e = Integer.parseInt(q[1]);
    		cal cal1 = new cal(b,c);
    		cal cal2 = new cal(d,e);
    		cal result  = cal1.sub(cal2);
    		if((result.fenzi%result.fenmu) == 0)
    			System.out.println(result.fenzi/result.fenmu);
    		else System.out.println(result.fenzi+"/"+result.fenmu);
    	}
    	if(s.contains("*")){
    		int a = s.indexOf("*");
    		String s1 = s.substring(0, a);
    		String s2 = s.substring(a+1,s.length());
    		String p[] = s1.split("\\/");
    		String q[] = s2.split("\\/");
    		int b = Integer.parseInt(p[0]);
    		int c = Integer.parseInt(p[1]);
    		int d = Integer.parseInt(q[0]);
    		int e = Integer.parseInt(q[1]);
    		cal cal1 = new cal(b,c);
    		cal cal2 = new cal(d,e);
    		cal result  = cal1.mul(cal2);
    		if((result.fenzi%result.fenmu) == 0)
    			System.out.println(result.fenzi/result.fenmu);
    		else System.out.println(result.fenzi+"/"+result.fenmu);
    	}
    	if(s.contains("\\")){
    		int a = s.indexOf("\\");
    		String s1 = s.substring(0, a);
    		String s2 = s.substring(a+1,s.length());
    		String p[] = s1.split("\\/");
    		String q[] = s2.split("\\/");
    		int b = Integer.parseInt(p[0]);
    		int c = Integer.parseInt(p[1]);
    		int d = Integer.parseInt(q[0]);
    		int e = Integer.parseInt(q[1]);
    		cal cal1 = new cal(b,c);
    		cal cal2 = new cal(d,e);
    		cal result  = cal1.div(cal2);
    		if((result.fenzi%result.fenmu) == 0)
    			System.out.println(result.fenzi/result.fenmu);
    		else System.out.println(result.fenzi+"/"+result.fenmu);
    	}
    	
    	
    }
		
	 }	
	}



	

	
	


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值