IPB

Welcome Guest ( Log In | Register )


2 Pages V  < 1 2  
Reply to this topicStart new topic
> programming question!
impala454
post Feb 15 2008, 10:13 AM
Post #16





Group: Members
Posts: 10,620
Joined: 23-February 06
From: Houston, TX
Member No.: 48



Finally got around to this in some bored time at work. Works pretty damn good so far. Feel free to break it and let me know what did it.

CODE
using System;
using System.Collections.Generic;
using System.Text;
//-------------------------
using System.Text.RegularExpressions;
using System.Collections;

namespace FractionConvert
{
    class Program
    {
        static void Main(string[] args)
        {
            Fraction f = new Fraction();
            double d = 0;
            Double.TryParse(args[0], out d);
            Fraction.TryParse(d, out f);
            Console.WriteLine(f.ToString());
        }
    }

    class Fraction
    {
        int m_nNumerator;
        int m_nDenominator;
        bool m_bNegative;
        double m_dDecimal;

        public Fraction()
        {
            m_nNumerator = 0;
            m_nDenominator = 0;
            m_dDecimal = 0;
            m_bNegative = false;
        }

        public Fraction(double DecimalToConvert)
        {
            m_nNumerator = 0;
            m_nDenominator = 0;
            m_dDecimal = DecimalToConvert;
            m_bNegative = false;

            ParseFraction();
        }

        public static bool TryParse(double DecimalInput, out Fraction FractionOutput)
        {
            try
            {
                FractionOutput = new Fraction(DecimalInput);
            }
            catch (Exception)
            {
                FractionOutput = new Fraction(0);
                return false;
            }

            return true;
        }

        public override string ToString()
        {
            if(m_bNegative)
                return string.Format("-{0}/{1}", m_nNumerator, m_nDenominator);
            else
                return string.Format("{0}/{1}", m_nNumerator, m_nDenominator);
        }

        private void ParseFraction()
        {
            if (m_dDecimal == 0)
            {
                m_nNumerator = 0;
                m_nDenominator = 1; // this could be any number really
                return;
            }

            string Whole = "";
            string Decimal = m_dDecimal.ToString();

            // strip off the minus sign if there is one
            if (Decimal[0] == '-')
            {
                m_bNegative = true;
                Decimal = Decimal.Substring(1, Decimal.Length - 1);
            }

            // split out the whole number, no need to parse that
            if (Decimal.Contains("."))
            {
                Whole = Decimal.Split('.')[0];
                Decimal = Decimal.Split('.')[1];
            }

            string Pattern = GetPattern(Decimal);

            int ParseInt = 0;
            int Divisor = 1;
            ArrayList Divisors = new ArrayList();

            if (!Int32.TryParse(Pattern, out ParseInt))
            {
                throw new Exception("Not a valid decimal number");
                return;
            }

            while (ParseInt > Divisor)
            {
                if (ParseInt % Divisor == 0)
                    Divisors.Insert(0, Divisor);

                Divisor++;
            }

            int WholeNumber = 0;
            Int32.TryParse(Whole, out WholeNumber);

            // if we only have one divisor, obviously it's 1
            if (Divisors.Count == 1)
            {
                m_nNumerator = 1;
                m_nDenominator = ParseInt;
                return;
            }

            foreach (int n in Divisors)
            {
                if (10 % n == 0)
                {
                    if(Pattern.Length <= 1)
                        m_nDenominator = 10 * n / ParseInt;
                    else
                        m_nDenominator = (Pattern.Length - 1) * (int)Math.Pow(10, (double)n+1) / ParseInt;

                    if (WholeNumber > 0)
                        m_nNumerator = m_nDenominator * WholeNumber + n;
                    else
                        m_nNumerator = n;
                    break;
                }
            }
        }

        private string GetPattern(string Decimal)
        {
            // the accuracy can't be any better than half the number of digits!
            //if (m_nAccuracy > Decimal.Length / 2)
            int Accuracy = Decimal.Length / 2;

            string Pattern = "";
            int NumRepeats = 0;
            int MeasuredAccuracy = 0;
            int MaxRepeats = 0;

            for (int i = 1; i <= Accuracy; i++)
            {
                NumRepeats = 0;

                Pattern = Decimal.Substring(0, i);
                for (int j = 0; j <= Decimal.Length - Accuracy; j += Pattern.Length)
                {
                    if (Decimal.Substring(j, Pattern.Length) == Pattern)
                        NumRepeats++;
                    else
                        break;
                }
                if (NumRepeats >= MaxRepeats)
                {
                    MaxRepeats = NumRepeats;
                    MeasuredAccuracy = Pattern.Length;
                }
            }

            // if we didn't find the pattern more than once, there is no pattern
            if (MaxRepeats <= 1)
                return Decimal;
            else
                return Decimal.Substring(0, MeasuredAccuracy);
        }
    }
}
Go to the top of the page
 
+Quote Post
Spectatrix
post Feb 15 2008, 10:22 AM
Post #17





Group: Admin
Posts: 6,906
Joined: 22-February 06
From: Austin
Member No.: 9



Too long. Cliff notes?

tongue.gif (I kid)


--------------------
QUOTE (pebkac @ Oct 14 2006, 03:15 PM) *
You and your logic.

QUOTE (Foamy)

http://xkcd.com/386/
Go to the top of the page
 
+Quote Post
pebkac
post Feb 15 2008, 10:29 AM
Post #18


From Atlantis to Interzone


Group: Global Moderators
Posts: 2,512
Joined: 23-February 06
From: Somewhere in space and time
Member No.: 65



Interesting. I can see why Desrosiers told me not to worry about it now. tongue.gif When he showed us his version of this program, he didn't even implement the function at all because it was too "ambiguous."

At any rate, it's pretty helpful to know how to actually do that.


--------------------
QUOTE (Spectatrix @ Oct 13 2006, 09:51 PM) *
Holy shit, pebkac, you're awesome!



"Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind." - Theodor Seuss Geisel (AKA Dr. Seuss)

"An idea that is not dangerous is unworthy of being called an idea at all." - Oscar Wilde
Go to the top of the page
 
+Quote Post
impala454
post Feb 15 2008, 11:30 AM
Post #19





Group: Members
Posts: 10,620
Joined: 23-February 06
From: Houston, TX
Member No.: 48



QUOTE (Spectatrix @ Feb 15 2008, 10:22 AM) *
Too long. Cliff notes?

tongue.gif (I kid)

DO IT WITH LESS CODE THEN BITCH

smile.gif

oh and actually didn't use regexes, so that "Using Regexes" line at the top can be removed.
Go to the top of the page
 
+Quote Post
Spectatrix
post Feb 15 2008, 11:31 AM
Post #20





Group: Admin
Posts: 6,906
Joined: 22-February 06
From: Austin
Member No.: 9



QUOTE (impala454 @ Feb 15 2008, 11:30 AM) *
DO IT WITH LESS CODE THEN BITCH

smile.gif

oh and actually, didn't use regexes, so that "Using Regexes" line at the top can be removed.

OK MAYBE I WILL!

tongue.gif


--------------------
QUOTE (pebkac @ Oct 14 2006, 03:15 PM) *
You and your logic.

QUOTE (Foamy)

http://xkcd.com/386/
Go to the top of the page
 
+Quote Post
zetec
post Feb 15 2008, 02:03 PM
Post #21


Retired Funk-bringer


Group: Moderators
Posts: 2,656
Joined: 22-February 06
From: Dallas
Member No.: 14



I was right about to say, why aren't you using regular expressions again?


--------------------
WAIT. I'm not finished.
Go to the top of the page
 
+Quote Post

2 Pages V  < 1 2
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 14th August 2026 - 03:51 PM
Skin made by: skeedio.com