분수 가감 승제

import javax.swing.JOptionPane;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		fracAdd(1,5,7,20);//   :11/20
		fracSub(1,5,7,20);//    
		fracMul(1,5,7,20);//    
		fracDiv(1,5,7,20);//    

	}
	static void fracAdd(int first_numerator,int first_denominator,int second_numrator,int second_denominator){
		//               
		//             
		int a=lcm( first_denominator,second_denominator);
		int b=first_numerator*a/first_denominator;
		int c=second_numrator*a/second_denominator;
		String str1="    :"+(b+c)+"/"+a;
		String str=JOptionPane.showInputDialog(null, str1);
		//             
	}
	static void fracSub(int first_numerator,int first_denominator,int second_numrator,int second_denominator){
		//               
		//             
		int a=lcm( first_denominator,second_denominator);
		int b=first_numerator*a/first_denominator;
		int c=second_numrator*a/second_denominator;
		String str1="    :"+(b-c)+"/"+a;
		String str=JOptionPane.showInputDialog(null, str1);
		//             
	}
	static void fracMul(int first_numerator,int first_denominator,int second_numrator,int second_denominator){
		//               
		//             
		int numerator=first_numerator*second_numrator;
		int denominator=first_denominator*second_denominator;
		int c=gcd(numerator,denominator);
		String str1="    :"+(numerator/c)+"/"+(denominator/c);
		String str=JOptionPane.showInputDialog(null, str1);
	}
	static void fracDiv(int first_numerator,int first_denominator,int second_numrator,int second_denominator){
		//               
		//             
		int numerator=first_numerator*second_denominator;
		int denominator=first_denominator*second_numrator;
		int c=gcd(numerator,denominator);
		String str1="    :"+(numerator/c)+"/"+(denominator/c);
		String str=JOptionPane.showInputDialog(null, str1);
	}
	static int gcd(int m,int n){
		int i;
		if(m>n)
		{
			for(i=n;i>=0;i--)
			{
				if(n%i==0 && m%i==0)
				{
					return i;
					
				}
			}
		}
		else
		{
			for(i=m;i>=0;i--)
			{
				if(n%i==0 && m%i==0)
				{
					return i;
				}
			}
		}
		return 0;
		
	}
	static int lcm(int m,int n){
		return (m/gcd(m,n))*(n/gcd(m,n))*gcd(m,n);

	}

}

좋은 웹페이지 즐겨찾기