예제 #1
0
            public DifferentNum Division(DifferentNum y)
            {
                DifferentNum result = new DifferentNum();
                DifferentNum x      = ConvertTo10();

                y = y.ConvertTo10();
                string sx = x.ToString(DecimalDec);
                string sy = y.ToString(DecimalDec);

                tryParse((Convert.ToDouble(sx) / Convert.ToDouble(sy)).ToString(), 10, DecimalDecoder, out result);
                return(result);
            }
예제 #2
0
            public DifferentNum Multiplication(DifferentNum y)
            {
                DifferentNum result = new DifferentNum();
                DifferentNum x      = ConvertTo10();

                y = y.ConvertTo10();
                string sx = x.ToString(DecimalDec);
                string sy = y.ToString(DecimalDec);

                if (x.after.Count != 0 || y.after.Count != 0)
                {
                    tryParse((Convert.ToDouble(sx) * Convert.ToDouble(sy)).ToString(), 10, DecimalDecoder, out result);
                }
                else
                {
                    tryParse((Convert.ToInt32(sx) * Convert.ToInt32(sy)).ToString(), 10, DecimalDecoder, out result);
                }
                return(result);
            }
예제 #3
0
            public DifferentNum ConvertToSys(int sys)
            {
                DifferentNum val = ConvertTo10();

                if (sys == 10)
                {
                    return(val);
                }
                string value    = val.ToString(DecimalDec);
                double dval     = 0;
                long   ival     = 0;
                bool   usingInt = false;

                if (val.after.Count == 0)
                {
                    ival     = Convert.ToInt64(value);
                    usingInt = true;
                }
                else
                {
                    ival = Convert.ToInt64(value.Substring(0, value.IndexOf(",")));
                    dval = Convert.ToDouble("0" + value.Substring(value.IndexOf(",")));
                }

                DifferentNum r = new DifferentNum();

                r.system = sys;
                r.sign   = sign;
                if (sign == -1)
                {
                    ival = -ival;
                }
                Stack <int> stack = new Stack <int>();

                while (ival >= sys)
                {
                    stack.Push((int)ival % sys);
                    ival /= sys;
                }
                stack.Push((int)ival);
                while (stack.Count != 0)
                {
                    r.before.Add(stack.Pop());
                }

                if (!usingInt)
                {
                    Queue <int> q    = new Queue <int>();
                    int         step = -1;
                    while (Math.Pow(sys, step) >= 0.0001 && !dval.Equals(0))
                    {
                        dval *= sys;
                        q.Enqueue((int)dval);
                        dval = dval - (int)dval;
                        --step;
                    }
                    while (q.Count != 0)
                    {
                        r.after.Add(q.Dequeue());
                    }
                }

                return(r);
            }