Exemple #1
0
		public Fraction( int num, int den )
			{
			CheckDenominatorZero( den );
			
			CheckMinValue( num );
			CheckMinValue( den );
			
			Fraction f = new Fraction( (decimal)num, (decimal)den );
			Initialize( f.num, f.den );
			}
Exemple #2
0
		public Fraction( Fraction f )
			{
			Initialize( f.num, f.den );
			}
Exemple #3
0
		// throws FormatException if wrong fraction format
		// throws OverflowException if reduced fraction does not fit in fraction range
		// throws ArithmeticException if denominator is zero
		public static Fraction Parse( string fraction )
			{
			if ( fraction == null )
				throw new FormatException();
			
			string[] split = fraction.Split( '/' );
			int len = split.Length;
			
			if ( len == 2 )
				{
				int s0 = int.Parse( split[0] );
				int s1 = int.Parse( split[1] );
				return new Fraction( s0, s1 );
				}
			else if ( len == 4 )
				{
				int s0 = int.Parse( split[0] );
				int s1 = int.Parse( split[1] );
				Fraction f1 = new Fraction( s0, s1 );
				
				int s2 = int.Parse( split[2] );
				int s3 = int.Parse( split[3] );
				Fraction f2 = new Fraction( s2, s3 );
				
				return f1 / f2;
				}
			else
				throw new FormatException();
			}