コード例 #1
0
        static void Main(string[] args)
        {
            string s = Console.ReadLine();

            s = s != "" ? s : "a * (b - t / s * h) / t - x * y"; //Sirul exemplu de la seminar
            SirPolonez sir = new SirPolonez(s);

            Console.WriteLine(sir.sir);
            Expresie expresie = new Expresie(s);

            Console.WriteLine(expresie.exp);
        }
コード例 #2
0
        private string creazaExpresie(string s)
        {
            Stack <char>   stivaOperatori = new Stack <char>();
            Stack <int>    costOperatori  = new Stack <int>();
            Stack <string> stivaOperanzi  = new Stack <string>();
            string         rez            = "";
            int            p = 0;

            foreach (var x in s)
            {
                if (x == '(')
                {
                    p += 10; stivaOperanzi.Push(x.ToString());  continue;
                }
                if (x == ')')
                {
                    p -= 10; var l = stivaOperanzi.Pop(); stivaOperanzi.Push(l + x.ToString()); continue;
                }
                if (x == ' ')
                {
                    continue;
                }
                if (SirPolonez.isChar(x))
                {
                    try
                    {
                        if (stivaOperanzi.Peek() == "(")
                        {
                            stivaOperanzi.Pop(); stivaOperanzi.Push("(" + x.ToString());
                        }
                        else
                        {
                            stivaOperanzi.Push(x.ToString());
                        }
                    }
                    catch
                    {
                        stivaOperanzi.Push(x.ToString());
                    }
                }
                else
                {
reincearca:
                    if (stivaOperatori.Count > 0)
                    {
                        if (costOperatori.Peek() < SirPolonez.cost(x))
                        {
                            stivaOperatori.Push(x);
                            costOperatori.Push(SirPolonez.cost(x) + p);
                        }
                        else
                        {
                            var    vf     = stivaOperatori.Pop(); costOperatori.Pop();
                            var    ult    = stivaOperanzi.Pop();
                            string penult = stivaOperanzi.Pop();
                            penult = penult + vf + ult;
                            stivaOperanzi.Push(penult);
                            rez = penult;
                            goto reincearca;
                        }
                    }
                    else
                    {
                        stivaOperatori.Push(x);
                        costOperatori.Push(SirPolonez.cost(x) + p);
                    }
                }
            }
            while (stivaOperatori.Count > 0)
            {
                var    vf     = stivaOperatori.Pop(); costOperatori.Pop();
                var    ult    = stivaOperanzi.Pop();
                string penult = stivaOperanzi.Pop();
                penult = penult + vf + ult;
                stivaOperanzi.Push(penult);
                rez = penult;
            }
            return(rez);
        }