Пример #1
0
        static void Main(string[] args)
        {
            double result = 0.0;

            MathText.ReversePolishNotation rpn = new MathText.ReversePolishNotation();
            rpn.Parse("3+4*2/(1-5)^2^3");
            result = rpn.Evaluate();
            Console.WriteLine("orig:   {0}", rpn.OriginalExpression);
            Console.WriteLine("tran:   {0}", rpn.TransitionExpression);
            Console.WriteLine("post:   {0}", rpn.PostfixExpression);
            Console.WriteLine("result: {0}", result);

            Console.WriteLine();
            rpn.Parse("-4.5* sin(-pi/6) + e -0.25");
            result = rpn.Evaluate();
            Console.WriteLine("orig:   {0}", rpn.OriginalExpression);
            Console.WriteLine("tran:   {0}", rpn.TransitionExpression);
            Console.WriteLine("post:   {0}", rpn.PostfixExpression);
            Console.WriteLine("result: {0}", result);

            Console.WriteLine();
            rpn.Parse("-4^-2");
            result = rpn.Evaluate();
            Console.WriteLine("orig:   {0}", rpn.OriginalExpression);
            Console.WriteLine("tran:   {0}", rpn.TransitionExpression);
            Console.WriteLine("post:   {0}", rpn.PostfixExpression);
            Console.WriteLine("result: {0}", result);

            Console.WriteLine();
            rpn.Parse("(A+B)*(C+D)");
            Console.WriteLine("orig:   {0}", rpn.OriginalExpression);
            Console.WriteLine("tran:   {0}", rpn.TransitionExpression);
            Console.WriteLine("post:   {0}", rpn.PostfixExpression);

            Console.WriteLine("End of program");
            Console.ReadLine();
        }
Пример #2
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker  = sender as BackgroundWorker;
            string           kString = this.keyBox.Text;
            bool             isArray = false;
            int aPos = 0;
            int aLng = 0;

            int[] aArr = new int[50];
            if (kString.IndexOfAny(",".ToCharArray()) != -1)
            {
                isArray = true;
                string [] sArr = kString.Split(",".ToCharArray());
                aArr = sArr.Select(x => int.Parse(x)).ToArray();
                aLng = aArr.Length;
            }
            double result = 0.0;
            string pp;
            char   tC;
            bool   cs     = this.checkBox1.Checked;
            bool   preSum = this.checkBox2.Checked;

            char[,] keyHash = new char[25500, 255];
            MathText.ReversePolishNotation rpn = new MathText.ReversePolishNotation();
            StreamReader sr       = new StreamReader(this.openFileDialog1.FileName);
            StreamWriter sw       = new StreamWriter(this.saveFileDialog1.FileName);
            int          uline    = 0;
            ulong        checksum = 0;

            if (cs)
            {
                sw.NewLine = "\n"; // this kind of newline for .dat files
            }
            else
            {
                sw.NewLine = "\r\n"; // this kind of newline for .txt files
            }
            if (cs)
            {
                string line   = "File generated by KITO";
                string output = "";
                for (int i = 0; i < (int)line.Length; i++)
                {
                    tC = line[i];
                    if (!preSum)
                    {
                        checksum += (ulong)tC;
                    }
                    output += tC;
                    // Next part only used for checksum
                    if (preSum) // Reversed order of preSum
                    {
                        if (isArray)
                        {
                            result = aArr[aPos];
                            aPos++;
                            if (aPos == aLng)
                            {
                                aPos = 0;
                            }
                        }
                        else
                        {
                            pp = kString.Replace("i", i.ToString());
                            rpn.Parse(pp);
                            result = rpn.Evaluate();
                        }
                        tC ^= (char)result;
                        if ((int)tC > 126 || (int)tC < 32)
                        {
                            tC = line[i];
                        }
                        checksum += (ulong)tC;
                    }
                }
                aPos = 0;
                sw.WriteLine(output);
            }
            aPos = 0;
            char nullchar = (char)"\0"[0];
            bool nl       = false;
            bool el       = false;

            while (sr.Peek() >= 0)
            {
                string line = sr.ReadLine();
                nl = false;
                if (line == "File generated by KITO")
                {
                    nl = true;
                }
                if (line == "-------------------------")
                {
                    el = true;
                }
                string output = "";
                if (!nl && !el)
                {
                    for (int i = 0; i < (int)line.Length; i++)
                    {
                        tC = line[i];
                        if (cs && preSum)
                        {
                            checksum += (ulong)tC;
                        }
                        if (keyHash[i, line[i]] != nullchar && !isArray)
                        {
                            tC = keyHash[i, tC];
                        }
                        else
                        {
                            if (isArray)
                            {
                                result = aArr[aPos];
                                aPos++;
                                if (aPos == aLng)
                                {
                                    aPos = 0;
                                }
                            }
                            else
                            {
                                pp = kString.Replace("i", i.ToString());
                                rpn.Parse(pp);
                                result = rpn.Evaluate();
                            }
                            tC ^= (char)result;
                            //tC ^= (char)(7 * ((int)i % 7 + 1));
                            if ((int)tC > 126 || (int)tC < 32)
                            {
                                tC = line[i];
                            }
                            keyHash[i, line[i]] = tC;
                        }
                        output += tC;
                        if (cs && !preSum)
                        {
                            checksum += (ulong)tC;
                        }
                    }
                    aPos = 0;
                    sw.WriteLine(output);
                }
                uline++;
                backgroundWorker1.ReportProgress(0, uline.ToString());
            }
            aPos = 0;
            string checksumC = "";

            if (checksum != 0)
            {
                string line   = "-------------------------";
                string output = "";
                for (int i = 0; i < (int)line.Length; i++)
                {
                    tC = line[i];
                    if (!preSum)
                    {
                        checksum += (ulong)tC;
                    }
                    output += tC;
                    // Next part only used for checksum
                    if (preSum)
                    {
                        if (isArray)
                        {
                            result = aArr[aPos];
                            aPos++;
                            if (aPos == aLng)
                            {
                                aPos = 0;
                            }
                        }
                        else
                        {
                            pp = kString.Replace("i", i.ToString());
                            rpn.Parse(pp);
                            result = rpn.Evaluate();
                        }
                        tC ^= (char)result;
                        if ((int)tC > 126 || (int)tC < 32)
                        {
                            tC = line[i];
                        }
                        checksum += (ulong)tC;
                    }
                }
                aPos = 0;
                sw.WriteLine(output);
            }
            aPos = 0;
            if (checksum != 0)
            {
                for (int i = 0; i < (int)checksum.ToString().Length; i++)
                {
                    tC = (char)(checksum.ToString())[i];
                    if (isArray)
                    {
                        result = aArr[aPos];
                        aPos++;
                        if (aPos == aLng)
                        {
                            aPos = 0;
                        }
                    }
                    else
                    {
                        pp = kString.Replace("i", i.ToString());
                        rpn.Parse(pp);
                        result = rpn.Evaluate();
                    }
                    tC ^= (char)result;
                    //tC ^= (char)(7 * ((int)i % 7 + 1));
                    if ((int)tC > 126 || (int)tC < 32)
                    {
                        tC = (char)(checksum.ToString())[i];
                    }
                    checksumC += tC;
                }
                aPos = 0;
                sw.WriteLine(checksumC);
            }
            sr.Close();
            sw.Close();
        }