Beispiel #1
0
        static void Main(string[] args)
        {
            if (!(args.GetLength(0) > 0))
            {
                Console.WriteLine("Error 1: No file name as argument!\n");
                Console.WriteLine("Press the Key ENTER to continue:\n");
                Console.ReadKey();
                return;
            }
            string fileNamePolish = args[0];
            int    len            = fileNamePolish.Length;

            if (fileNamePolish.Substring(len - 4) != ".txt")
            {
                Console.WriteLine("Error 2: No text file  as argument!\n");
                Console.WriteLine("Press the Key ENTER to continue:\n");
                Console.ReadKey();
                return;
            }
            string currDir = Directory.GetCurrentDirectory();

            Console.WriteLine("Current Directory:{0}\n", currDir);
            if (!File.Exists(fileNamePolish))
            {
                Console.WriteLine("Error 3: Not exists text file {0} as argument in current directory!\n", fileNamePolish);
                Console.WriteLine("Press the Key ENTER to continue:\n");
                Console.ReadKey();
                return;
            }
            StreamReader  sr         = new StreamReader(fileNamePolish);
            string        shortName  = sr.ReadLine();
            string        name       = sr.ReadLine();
            List <string> listPolish = new List <string>();

            while (!sr.EndOfStream)
            {
                string currLine = sr.ReadLine();
                listPolish.Add(currLine);
            }
            F
            Console.WriteLine("Polish Count :{0}\n", listPolish.Count);

            string[] arrPolish = new string[listPolish.Count];
            for (int i = 0; i < listPolish.Count; i++)
            {
                arrPolish[i] = listPolish[i];
            }

            //1) Demo
            //ExprNameArray namedPostfixExpID = new ExprNameArray("Id", " Identifier", "a", "a", "9", "Join", "Star", "Concat");
            // Draw_Diagram(namedPostfixExpID, bDrawPolish);

            ExprNameArray namedPostfixExpression = new ExprNameArray(shortName, name, arrPolish);

            Draw_Diagram(namedPostfixExpression, true);//true is for "Studing"

            Console.WriteLine("Press the Key ENTER to continue:\n");
            Console.ReadKey();
            return;

            ////////
            ///////////////////////////


            bool bDrawPolish = true; //Studing

            //foreach(string opt in args)
            //    Console.WriteLine(opt);
            //Console.WriteLine();

            if (args.GetLength(0) > 0)
            {
                if (args[0] == "test")
                {
                    bDrawPolish = false;//Testing
                }
                else
                {
                    bDrawPolish = true;
                }
            }
            else
            {
                bDrawPolish = true;
            }

            if (bDrawPolish)
            {
                Console.WriteLine("Studing\n");
            }
            else
            {
                Console.WriteLine("Testing\n");
            }
            Console.WriteLine("Press the Key ENTER to continue:\n");
            Console.ReadKey();

            //1) Demo
            ExprNameArray namedPostfixExpID = new ExprNameArray("Id", " Identifier", "a", "a", "9", "Join", "Star", "Concat");

            Draw_Diagram(namedPostfixExpID, bDrawPolish);

            //2) Demo
            ExprNameArray namedPostfixExpOCT = new ExprNameArray("Oct", " Octodecimal", "0", "0", "7", "Join", "Star", "Concat", "", "ul", "Join", "lu", "Join", "u", "Join", "l", "Join", "Concat");

            // //3) Input --> Resulting Diagram
            Draw_Diagram(namedPostfixExpOCT, bDrawPolish);

            ExprNameArray namedPostfixExp = new ExprNameArray();

            //Console.WriteLine("shortName:{0}", (namedPostfixExp.shortName == null));
            //Console.WriteLine("name:{0}", (namedPostfixExp.name == null));
            //Console.WriteLine("arrPolish:{0}", (namedPostfixExp.arrPolish == null));

            Console.WriteLine("Prompt 1: Enter the 'shortName' for Polish Expression : ");
            namedPostfixExp.shortName = Console.ReadLine();
            if (namedPostfixExp.shortName.Length > 7 || namedPostfixExp.shortName.Length == 0)
            {
                namedPostfixExp.shortName = "Student";
            }


            Console.WriteLine("\nPrompt 2: Enter the 'name' for Polish Expression : ");
            namedPostfixExp.name = Console.ReadLine();
            if (namedPostfixExp.name.Length <= 7)
            {
                namedPostfixExp.name = "Student's Diagram";
            }


            List <string> listPostfix = new List <string>();
            string        tokens      = "";

            Console.WriteLine("\nEnter a list of  tokens delimited Space for Polish Expression.");
            Console.WriteLine("For example, Polish Expression for the identifiers is: ");
            Console.WriteLine("\na a 9 \"\" Join Join Star Concat");
            Console.WriteLine("\n\"\" is the token of the empty string. ");
            Console.WriteLine("a is the token of the class [_a-zA-Z]");
            Console.WriteLine("9 is the token of the class [0-9]");
            Console.WriteLine("Join, Concat, Star are tokens for |, *, either {} or ^");
            Console.WriteLine("\nSo Regular Expression for the identifiers can be like as:");
            Console.WriteLine("\na*{a|9|\"\"}\nor\na*{9|a}");

            Console.WriteLine("\nPrompt 3: Enter a list of  tokens delimited Space for Polish Expression:");

            tokens = Console.ReadLine();
            namedPostfixExp.arrPolish = tokens.Split(' ');

            //Output
            Console.WriteLine("\nPolish Expression:\n");
            Console.WriteLine("1)shortname:{0}", namedPostfixExp.shortName);
            Console.WriteLine("2)name:{0}", namedPostfixExp.name);
            Console.WriteLine("3)arrayPolish:");
            foreach (string s in namedPostfixExp.arrPolish)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("============");

            string infixExpression = PolishToInfix(new List <string> (namedPostfixExp.arrPolish));

            Console.WriteLine("\nInfix Expression:\n");
            Console.WriteLine("Length:({0})", infixExpression.Length);
            Console.WriteLine(infixExpression);
            Console.WriteLine("============");
            if (infixExpression == "")
            {
                Console.WriteLine("Invalid POLISH EXPRESSION!");
                Console.WriteLine("You get NO DIAGRAM, try again!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("THIS is a POLISH EXPRESSION!");
            Console.WriteLine("You  get its DIAGRAM !");
            Console.ReadKey();
            //===========Draw Diagramm for  ExprNameArray namedPostfixExp

            Draw_Diagram(namedPostfixExp, true);    //bDrawPolish

            //======================================================
            Console.WriteLine("Press the Key Enter to Exit:");
            Console.ReadKey();
            return;
        }
Beispiel #2
0
        static void Draw_Diagram(ExprNameArray expression, bool bDrawPolish)
        {
            FormDiagramm formDiagramm = new FormDiagramm();

            formDiagramm.TopMost = true;

            string[] arrPolish = expression.arrPolish;

            string RegExpName = expression.name;

            List <string> polish = new List <string>(arrPolish);

            //=======Change "" for EMPTY STRING========
            for (int i = 0; i < polish.Count; i++)
            {
                if (polish[i] != "\"\"")
                {
                    continue;
                }
                else
                {
                    polish[i] = "";
                }
            }
            //========================================

#if NoTest
            Console.WriteLine("Source Polish Expression.");
            Console.WriteLine("PolishToTree: Do reduction for strings and Do join 1-strings-symbols!!!");

            foreach (string s in polish)
            {
                Console.WriteLine(s);
            }
            Console.WriteLine();

            //Console.WriteLine("Press the Key Enter to Exit:");
            //Console.ReadKey();
            //return;

            //now USING
            Console.WriteLine("\n Now USING\nTree Chart attributed with size-RECTANGLES");

            //1) List<string> polish --(PolishToTree)--> TreeChart treeChart
            Console.WriteLine("Getting Reduced Polish Expression with Simple Operands untill Joined");
            Console.WriteLine("and With no Size");
#endif

            TreeChart     treeChart = TreeChart.PolishToTree(polish);//No Size, No Numbers,ENUMERATION IMPOSSIBLE
            List <string> postfix   = treeChart.GetPostfixForm();
#if NoTest
            Console.WriteLine("\nPostfix = treeChart.GetPostfixForm();");
            foreach (string sp in postfix)
            {
                Console.WriteLine(sp);
            }
            Console.WriteLine();
            Console.ReadKey();

            //treeChart.PrintTree(); //With No Size No Numbers//Working
#endif

            //2) TreeChart treeChart --(AssignSizeToTree)--> TreeChart treeChart

            treeChart = treeChart.AssignSizeToTree();     //Get Size, But No Changing Numbers, ENUMERATION IMPOSSIBLE
#if NoTest
            //TODO:treeChart.AssignNumbersToTree();
            Console.WriteLine("\nTree Chart With Size");

            treeChart.PrintTree();     //With Size ~ //PrintTree(treeChart); No Numbers;ENUMERATION POSSIBLE
#endif

            //2) TreeChart treeChart --> string treeChart.RegExp

            //Back to Expression from TreeChart treeChart.

            treeChart.RegExp = treeChart.GetExpression().RegExp;    // ~ PrintExpression(treeChart);

            if (bDrawPolish)
            {
                treeChart.namedPostfixExp = expression;
            }

            //TreeChart.PrintExpression(treeChart); //Working
#if NoTest
            Console.WriteLine("\ntreeChart.element:{0}", treeChart.element);
            Console.WriteLine("treeChart.RegExp:={0}", treeChart.RegExp);
#endif



            treeChart.DiagramName = "Diagram of Regular Expression:" + RegExpName;
#if NoTest
            Console.WriteLine("TreeChart.DiagramName:={0}", treeChart.DiagramName);
#endif


            // FormDiagramm formDiagramm = new FormDiagramm();

            //a) Add <<new TabPage>> for new <<expression>> in <<arrExpr>>

            TabPage currTabPage = new TabPage(expression.shortName);
            formDiagramm.tabControlDiagram.TabPages.Add(currTabPage);
            //currTabPage.Tab

            //b) Add <<treeChart>> of <<expression>> to <<listTreeChart>>
            formDiagramm.listTreeChart.Add(treeChart);


            formDiagramm.Width = treeChart.rectSize.Width + 20 + (int)(4 * TreeChart.emSizeChar);
            if (treeChart.DiagramName.Length * 14 > formDiagramm.Width)
            {
                formDiagramm.Width = treeChart.DiagramName.Length * 14;
            }

            //formDiagramm.TopMost = true;

            //c)Set <<currTabPage>>
            //int indexCurrTabPage = formDiagramm.tabControlDiagram.TabPages.Count - 1;

            formDiagramm.tabControlDiagram.SelectedTab = currTabPage;

            currTabPage.Paint += new PaintEventHandler(formDiagramm.currTabPage_Paint);

            formDiagramm.ShowDialog();
            //Console.WriteLine("Press the Key Enter to Exit:");
            //Console.ReadKey();
        }