示例#1
0
        //public Table(int n = 1)
        //{
        //    N = n;
        //    string key = "";

        //    // bordered
        //    // +\- 0
        //    for (int i = 0; i < n + 1; i++)
        //        key += '0';
        //    Contibution.Add(key, 0);
        //    key = key.Replace('0', '1');
        //    Contibution.Add(key, 0);
        //    // +\- N
        //    key = "0";
        //    for (int i = 0; i < n; i++)
        //        key += '1';
        //    Contibution.Add(key, N);
        //    key = "1";
        //    for (int i = 0; i < n; i++)
        //        key += '0';
        //    Contibution.Add(key, -N);

        //    // intermediate, +1 per 2
        //    GenerateIntermediate(n);

        //    void GenerateIntermediate(int step)
        //    {
        //        List<string> list = new List<string>() {"0", "1"};
        //        while (step > 0)
        //        {
        //            List<string> l = new List<string>();
        //            foreach (var e in list)
        //            {
        //                l.Add($"{e}0");
        //                l.Add($"{e}1");
        //            }

        //            list = l;
        //            step--;
        //        }

        //        foreach (var kvp in Contibution)
        //            list.Remove(kvp.Key);

        //        int m = 1;
        //        for (int i = 0; i < list.Count; i += 4)
        //        {
        //            Contibution.Add(list[i], m);
        //            Contibution.Add(list[i + 1], m);

        //            Contibution.Add(list[list.Count - (i + 2)], -m);
        //            Contibution.Add(list[list.Count - (i + 1)], -m);

        //            m++;
        //        }
        //    }
        //}

        public Dictionary <string, BinaryWord> Generate(BinaryWord b)
        {
            Dictionary <string, BinaryWord> bDict = new Dictionary <string, BinaryWord>();

            foreach (var kvp in Contibution)
            {
                BinaryWord bw = new BinaryWord(b.Value.Length);
                int        m  = Math.Abs(kvp.Value);
                if (m > 0)
                {
                    for (int i = 0; i < m; i++)
                    {
                        bw = bw.Add(b);
                    }
                }
                if (kvp.Value < 0)
                {
                    bw = bw.Minus;
                }
                bDict.Add(kvp.Key, bw);
            }

            return(bDict);
        }
示例#2
0
        public void Calculate(string a, string b)
        {
            a = Binary.AlignFor(a, Word);
            b = Binary.AlignFor(b, Word);

            //int align = (int) Math.Pow(2, Word - 1); // max koef
            //int extend = (int) Math.Pow(2, Word - 1); //(_table.Max - 1) + ((_table.Max - 1) % Word);

            int extend = _table.N - 1;
            {
                int d = extend % Word;
                extend = d > 0 ? extend + (Word - extend) : extend;
            }

            string asrc = a;

            a = $"{a}0";

            string bsrc = b;
            {
                string postfix = "";
                foreach (var ch in bsrc)
                {
                    postfix += '0';
                }

                string prefix = "";
                char   flr    = b[0];
                //for (int i = 0; i < align; i++)
                for (int i = 0; i < extend; i++)
                {
                    prefix += flr;
                }

                b = $"{prefix}{b}{postfix}";
            }

            BinaryWord aa = new BinaryWord(a);
            BinaryWord bb = new BinaryWord(b);
            Dictionary <string, BinaryWord> bDics = _table.Generate(bb);

            BinaryWord spp = new BinaryWord(bb.Value.Length);

            string aPattern = "";

            for (int i = 0; i < asrc.Length; i++)
            {
                aPattern += '*';
            }
            aPattern += ".*";

            string bPattern = "";

            //for (int i = 0; i < align; i++)
            //    bPattern += '*';
            //if (align > 0)
            //    bPattern += '.';

            if (extend > 0)
            {
                for (int i = 0; i < extend; i++)
                {
                    bPattern += '*';
                }
                bPattern += '.';
            }

            for (int i = 0; i < bsrc.Length; i++)
            {
                bPattern += '*';
            }
            bPattern += '.';
            for (int i = 0; i < bsrc.Length; i++)
            {
                bPattern += '*';
            }

            Console.WriteLine("----------------------");
            Console.WriteLine($"0|a:{Binary.Pattern(aa.Value, aPattern)}|b:{Binary.Pattern(bb.Value, bPattern)}|spp:{Binary.Pattern(spp.Value, bPattern)}|input");
            Console.WriteLine("----------------------");

            for (int i = 0; i < (aa.Value.Length - 1) / Word; i++)
            {
                string key = aa.Value.Substring(aa.Value.Length - (Word + 1), (Word + 1));

                spp = spp.Add(bDics[key]);
                string op;
                {
                    int m = _table.Contibution[key];
                    op = m == 0 ? "" : $"{m}b; ";
                }

                Console.WriteLine($"{i + 1}|a:{Binary.Pattern(aa.Value, aPattern)}|b:{Binary.Pattern(bb.Value, bPattern)}|spp:{Binary.Pattern(spp.Value, bPattern)}|{op}move a, spp;");

                aa.MoveRight(Word, false);
                spp.MoveRight(Word);
            }

            Console.WriteLine($"r|a:{Binary.Pattern(aa.Value, aPattern)}|b:{Binary.Pattern(bb.Value, bPattern)}|spp:{Binary.Pattern(spp.Value, bPattern)}|result;");
        }
示例#3
0
        public void Calculate(string a, string b)
        {
            a = Binary.AlignFor(a, Word);
            b = Binary.AlignFor(b, Word);

            int extend = _table.N - 1;
            {
                int d = extend % Word;
                extend = d > 0 ? extend + (Word - extend) : extend;
            }

            string asrc = a;

            string bsrc = b;
            {
                char flr = b[0];

                string postfix = "";
                foreach (var ch in bsrc)
                {
                    postfix += flr;
                }

                string prefix = "";
                for (int i = 0; i < extend; i++)
                {
                    prefix += flr;
                }

                b = $"{prefix}{postfix}{b}";
            }

            BinaryWord aa = new BinaryWord(a);
            BinaryWord bb = new BinaryWord(b);
            Dictionary <string, BinaryWord> bDics = _table.Generate(bb);

            BinaryWord spp = new BinaryWord(bb.Value.Length);

            string aPattern = "";

            for (int i = 0; i < asrc.Length; i++)
            {
                aPattern += '*';
            }

            string bPattern = "";

            if (extend > 0)
            {
                for (int i = 0; i < extend; i++)
                {
                    bPattern += '*';
                }
                bPattern += '.';
            }

            for (int i = 0; i < bsrc.Length; i++)
            {
                bPattern += '*';
            }
            bPattern += '.';
            for (int i = 0; i < bsrc.Length; i++)
            {
                bPattern += '*';
            }

            Console.WriteLine("----------------------");
            Console.WriteLine($"0|a:{Binary.Pattern(aa.Value, aPattern)}|b:{Binary.Pattern(bb.Value, bPattern)}|spp:{Binary.Pattern(spp.Value, bPattern)}|input");
            Console.WriteLine("----------------------");

            for (int i = 0; i < aa.Value.Length / Word; i++)
            {
                string key = aa.Value.Substring(0, Word + 1);

                spp.MoveLeft(Word);

                spp = spp.Add(bDics[key]);
                string op;
                {
                    int m = _table.Contibution[key];
                    op = m == 0 ? "" : $"{m}b; ";
                }

                Console.WriteLine($"{i + 1}|a:{Binary.Pattern(aa.Value, aPattern)}|b:{Binary.Pattern(bb.Value, bPattern)}|spp:{Binary.Pattern(spp.Value, bPattern)}|{op}move a, spp;");

                aa.MoveLeft(Word, false);
            }

            Console.WriteLine($"r|a:{Binary.Pattern(aa.Value, aPattern)}|b:{Binary.Pattern(bb.Value, bPattern)}|spp:{Binary.Pattern(spp.Value, bPattern)}|result;");
        }
        public void Divide(string l, string d)
        {
            int n = l.Length;


            while (d.Length < n)
            {
                d = $"0{d}";
            }
            d = $"0{d}";

            string dPattern = "*,";

            for (int i = 0; i < n; i++)
            {
                dPattern += '*';
            }

            for (int i = 0; i < n + 1; i++)
            {
                l = $"0{l}";
            }

            string lPattern = "*,";

            for (int i = 0; i < n; i++)
            {
                lPattern += '*';
            }
            lPattern += ' ';
            for (int i = 0; i < n; i++)
            {
                lPattern += '*';
            }

            BinaryWord qq = new BinaryWord(n);
            BinaryWord ll = new BinaryWord(l);
            BinaryWord dd;
            BinaryWord ndd;

            {
                string db  = d;
                string ndb = Binary.InvertSign(db);
                while (db.Length < l.Length)
                {
                    db  += '0';
                    ndb += '0';
                }
                dd  = new BinaryWord(db);
                ndd = new BinaryWord(ndb);
            }


            Console.WriteLine("----------------------");
            Console.WriteLine($"0|q:{qq.Value}|l:{ll.Pattern(lPattern)}|d:{Binary.Pattern(d, dPattern)}|input");
            Console.WriteLine("----------------------");

            for (int i = 0; i < n; i++)
            {
                qq.MoveLeft(1, false);
                ll.MoveLeft(1, false);

                string op = $"l{i + 1} >= 0;";
                ll = ll.Add(ndd);
                char qd = '1';
                if (ll.Value[0] == '1')
                {
                    ll = ll.Add(dd);
                    op = $"l{i + 1} < 0 => restore;";
                    qd = '0';
                }
                qq.Value = qq.Value.Substring(0, qq.Value.Length - 1) + qd;

                Console.WriteLine($"{i + 1}|q:{qq.Value}|l:{ll.Pattern(lPattern)}|d:{dd.Pattern(dPattern)}|move q, l; {op}");
            }
        }