Пример #1
0
        static void RunIntro(string name, int num, bool isExpt2,
                             string rsrcPath = "resources")
        {
            DateTime  time1      = DateTime.Now;
            Stopwatch stopwatch1 = new Stopwatch();

            stopwatch1.Start();

            // basic datatypes
            bool isDone = false;
            int  numI = 0, arrLen = (int)ConstItems.ZERO, delayMsecs = 2500,
                 seedp     = time1.Millisecond;
            float timeDiff = 0.0f;
            char  ch       = '\0';

            // strings & arrays
            string greetStr, dateStr, greetPath = "greet.txt";

            char[] str1   = new char[64];
            int[]  numArr = { 9, 9, 0x9, 9 }; // {bin, oct, hex, dec}

            //composites
            UVar uVar1 = new UVar();
            //User user1 = new User() {Name = "World", Num = 0, TimeIn = time1};
            User   user1 = new User("World", 0, time1);
            var    tup1  = new Tuple <byte, short>((byte)Kind.INTSHORT, 100);
            Person pers;

            Random rnd = new Random(seedp);

            IO.TextWriter fOut  = Console.Out;
            TimeZone      tz1   = TimeZone.CurrentTimeZone;
            string        tzStr = tz1.IsDaylightSavingTime(time1) ? tz1.DaylightName
                : tz1.StandardName;

            arrLen = numArr.Length;

            for (int i = 0; arrLen > i; ++i) // foreach (int elem in numArr)
            {
                numI += numArr[i];           //     numI += elem;
            }
            Trace.Assert((numArr.Length * numArr[0]) == numI,
                         "arrLen * numArr[0] != numI");

            ch = Misc.DelayChar(delayMsecs);

            do
            {
                uVar1.kind = tup1.Item1;
                uVar1.val  = new UnionVal()
                {
                    sh = tup1.Item2
                };
                uVar1.kind = (byte)Kind.INTSHORT;
                uVar1.val  = new UnionVal()
                {
                    sh = -1
                };
                uVar1.kind = (byte)Kind.UINTLONG;
                uVar1.val  = new UnionVal()
                {
                    ul = 1UL
                };
                uVar1.kind = (byte)Kind.FLOAT;
                uVar1.val  = new UnionVal()
                {
                    f = 100.0f
                };
                uVar1.kind = (byte)Kind.DOUBLE;
                uVar1.val  = new UnionVal()
                {
                    d = 100.0d
                };
                uVar1.kind = (byte)Kind.DECIMAL;
                uVar1.val  = new UnionVal()
                {
                    m = 1000.0m
                };
                str1[0] = '\0';
                Trace.Assert(((byte)Kind.DECIMAL == uVar1.kind) &&
                             (1000.0m == uVar1.val.m),
                             "kind == Kind.DECIMAL && val == 1000.0m is false");
            } while (isDone);

            user1.Name = name;
            user1.Num  = 0 == num?rnd.Next(0, 17) + 2 : num;

            SysTextRegex.Regex re = new SysTextRegex.Regex(@"(?i)^quit$");
            SysTextRegex.Match m  = re.Match(name);
            Console.WriteLine("{0} match: {1} to {2}\n",
                              m.Success ? "Good" : "Does not", name, re);

            dateStr = user1.TimeIn.ToString("ddd MMM dd HH:mm:ss yyyy zzz");

            //greetStr = Misc.Greeting(greetPath, user1.Name);
            try {
                //greetStr = (new IO.StreamReader(rsrcPath + "/" + greetPath)).ReadToEnd().TrimEnd('\n', '\r') + user1.Name;
                greetStr = IO.File.ReadAllText(rsrcPath + "/" + greetPath).TrimEnd('\n', '\r') + user1.Name;
            } catch (Exception exc0) {
                Console.Error.WriteLine("(exc: {0}) Bad env var RSRC_PATH: {1}\n",
                                        exc0, rsrcPath);
                try {
                    greetStr = Util.GetFromResources(greetPath, assembly).TrimEnd('\n', '\r') + user1.Name;
                } catch (Exception exc1) {
                    throw;
                    Environment.Exit(1);
                }
            }
            Console.WriteLine("{0} {1}\n{2}!", dateStr, tzStr, greetStr);

            stopwatch1.Stop();
            timeDiff = stopwatch1.ElapsedMilliseconds;
            Console.WriteLine("(program {0}) Took {1:F1} seconds.", progName,
                              timeDiff * 1.0e-3);

            int[] ints = { 2, 1, 0, 4, 3 };
            var   lst  = new SysCollGen.List <int>(ints);

            if (isExpt2)
            {
                fOut.WriteLine("Expt(2.0, {0}) = {1}", user1.Num,
                               Classic.ExptLp(2.0f, user1.Num));

                var res = Util.MkString(lst);
                Console.Write("Reverse({0}): ", res);
                var lstTmp = Seqops.CopyOfLp <int>(lst);
                Seqops.ReverseLp <int>(lstTmp);
                res = Util.MkString(lstTmp);
                Console.WriteLine("{0}", res);

                res = Util.MkString(lst);
                Console.Write("{0}.Sort(): ", res);
                lst.Sort();
                res = Util.MkString(lst);
                Console.WriteLine("{0}", res);
            }
            else
            {
                fOut.WriteLine("Fact({0}) = {1}", user1.Num,
                               Classic.FactLp(user1.Num));

                int el  = 3;
                var res = Util.MkString(lst);
                int idx = Seqops.IndexOfLp <int>(el, lst);
                Console.WriteLine("IndexOf({0}, {1}): {2}", el, res, idx);

                int newVal = 50;
                Console.Write("{0}.Add({1}): ", res, newVal);
                lst.Add(newVal);
                res = Util.MkString(lst);
                Console.WriteLine("{0}", res);
            }
            Console.WriteLine(new string('-', 40));

            int nPascal = 5;

            int[][] arrPascal = Classic.PascalTriAdd(nPascal);
            Console.WriteLine("PascalTri(n: {0}): ", nPascal);
            Classic.PrintPascalTri(nPascal, arrPascal);
            Console.WriteLine(new string('-', 40));

            int ndisks = 4, lenHanoi = (int)Math.Pow(2.0f, ndisks) - 1;

            int[][] arrHanoi = ClassicPuzzles.Hanoi(1, 2, 3, ndisks);
            Console.WriteLine("Hanoi(src: 1, dest: 2, spare: 3, ndisks: {0}): ",
                              ndisks);
            for (int i = 0; lenHanoi > i; ++i)
            {
                Console.WriteLine("move #{0,-2}: move from {1} to {2}", i + 1,
                                  arrHanoi[i][0], arrHanoi[i][1]);
            }
            Console.WriteLine(new string('-', 40));

            int numqueens = 8, queensNdx = rnd.Next(0, 50);

            int[] arrNqueens = ClassicPuzzles.Nqueens(queensNdx, numqueens);

            Console.WriteLine("Nqueens(ndx: {0}, numqueens: {1}):",
                              queensNdx, numqueens);
            Console.Write("{");
            for (int r = 0; numqueens > r; ++r)
            {
                Console.Write("({0}, {1}), ", (char)('a' + r), arrNqueens[r]);
            }
            Console.WriteLine("}");

            for (int r = 0; numqueens > r; ++r, Console.WriteLine())
            {
                Console.Write("'{0}'", numqueens - 1 - r);
                for (int c = 0; numqueens > c; ++c)
                {
                    Console.Write("'{0}'",
                                  (numqueens - 1 - r) == arrNqueens[c] ? 'Q' : '.');
                }
            }
            Console.Write("' '");
            for (int c = 0; numqueens > c; ++c)
            {
                Console.Write("'{0}'", (char)('a' + c));
            }
            Console.WriteLine("\n");
            Console.WriteLine(new string('-', 40));

            //pers = new Person("John", 32);
            pers = new Person {
                Name = "I.M. Computer", Age = 32
            };

            Trace.Assert(pers.GetType() == typeof(Person),
                         "Debug Error: Type mismatch");
            Trace.Assert(pers is Object,
                         "Trace Error: Type inheritance mismatch");
            Console.WriteLine("{0}", pers.ToString());
            Console.Write("pers.Age = {0}: ", 33);
            pers.Age = 33;
            Console.WriteLine("{0}", pers.ToString());
            Console.WriteLine(new string('-', 40));
        }