Informatika gyűjtemény

Egy szinttel feljebb lengyelforma.cs

2004050607080910

NézetNyomtat

lengyelforma.cs (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 7 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Kifkiert
{
    class Program
    {
        static Queue<Token> queue;
        static Stack<Token> stack;
        static List<Token> queue0;

        static void Tokenise()
        {
            queue = new Queue<Token>();
            stack = new Stack<Token>();
            queue0 = new List<Token>();

            StreamReader sr = new StreamReader("be.txt");
            string buffer = sr.ReadLine();
            Console.WriteLine(buffer);
            sr.Close();

            while (buffer != "")
            {
                string c = Convert.ToString(buffer[0]);
                if (== "(")
                {
                    Parenthesis t = new Parenthesis(true);
                    queue0.Add(t);
                }
                else if (== ")")
                {
                    Parenthesis t = new Parenthesis(false);
                    queue0.Add(t);
                }
                else if (IsOperator(c))
                {
                    bool binary = true;
                    if (== "-")
                    {
                        binary = false;
                        if (queue0.Count != 0)
                        {
                            Token lastToken = queue0[queue0.Count - 1];
                            if ((lastToken is Number) || (lastToken is Parenthesis && !((Parenthesis)lastToken).left))
                            {
                                binary = true;
                            }
                        }
                    }
                    Operator t = new Operator(binary, c);
                    queue0.Add(t);
                    //stack.Push(t);
                }
                else
                {
                    for (int i = 1; i < buffer.Length && !IsOperator(Convert.ToString(buffer[i])); i++)
                    {
                        c += buffer[i];
                    }

                    c = c.Replace('.', ',');
                    Number szam = new Number(Convert.ToDouble(c));
                    queue0.Add(szam);
                    //queue.Push(szam);
                }

                buffer = buffer.Remove(0, c.Length);
            }
        }

        static void ToRPN()
        {
            for (int i = 0; i != queue0.Count; i++)
            {
                Token t = queue0[i];
                if (is Number)
                {
                    queue.Enqueue(t);
                }
                else if (is Operator)
                {
                    Token t2;
                    while (stack.Count != 0 && (t2 = stack.Peek()) is Operator)
                    {

                        Operator op1 = (Operator)t;
                        Operator op2 = (Operator)t2;
                        if ((op1.rightassoc && op1.priority < op2.priority) || (!op1.rightassoc && op1.priority <= op2.priority))
                        {
                            queue.Enqueue(stack.Pop());
                        }
                        else
                        {
                            break;
                        }
                    }
                    stack.Push(t);
                }
                else if (is Parenthesis)
                {
                    Parenthesis p = (Parenthesis)t;
                    if (p.left)
                    {
                        stack.Push(t);
                    }
                    else
                    {
                        do
                        {
                            Token last = stack.Pop();
                            if (last is Parenthesis && ((Parenthesis)last).left) break;
                            queue.Enqueue(last);

                        } while (true);
                    }
                }
            }

            while (stack.Count != 0)
            {
                queue.Enqueue(stack.Pop());
            }
        }

        static double Evaluate()
        {
            Stack<Number> estack = new Stack<Number>();
            foreach (Token t in queue)
            {
                if (is Number) estack.Push((Number)t);
                if (is Operator)
                {
                    Operator op = (Operator)t;

                    if (!op.binary)
                    {
                        Number n1 = estack.Pop();
                        estack.Push(new Number(op.Evalute(n1)));
                    }
                    else
                    {
                        Number n1 = estack.Pop();
                        Number n2 = estack.Pop();
                        estack.Push(new Number(op.Evalute(n1, n2)));
                    }
                }
            }

            return estack.Pop().number;
        }

        static void Main(string[] args)
        {
            Tokenise();
            ToRPN();

            foreach (Token t in queue) Console.Write(t.ToString() + " ");
            Console.WriteLine(" =  " + Evaluate());
            Console.Read();
        }

        static bool IsOperator(string op)
        {
            if (op == "(" || op == ")" || op == "+" || op == "-" || op == "*" || op == "/" || op == "^") return true;
            return false;
        }

    }

    class Token
    {

    }

    class Operator : Token
    {
        public string op;
        public bool binary;
        public bool rightassoc;
        public int priority;

        public Operator(bool binary, string op)
        {
            this.binary = binary;
            this.op = op;
            if (op == "^" || op == "-") rightassoc = true;
            else rightassoc = false;

            if (op == "^") priority = 3;
            if (!binary && op == "-") priority = 3;
            if (op == "*" || op == "/") priority = 2;
            if (op == "+" || op == "-") priority = 1;
        }

        public double Evalute(Number n1)
        {
            if (!binary)
            {
                switch (this.op)
                {
                    case "-": return -n1.number;
                }
            }

            throw new Exception("wtf");
        }

        public double Evalute(Number n1, Number n2)
        {
            if (binary)
            {
                switch (this.op)
                {
                    case "^": return Math.Pow(n2.number, n1.number);
                    case "*": return n2.number * n1.number;
                    case "/": return n2.number / n1.number;
                    case "+": return n2.number + n1.number;
                    case "-": return n2.number - n1.number;
                }
            }

            throw new Exception("wtf");
        }

        public override string ToString()
        {
            return this.op;
        }
    }

    class Number : Token
    {
        public double number;

        public Number(double number)
        {
            this.number = number;
        }

        public override string ToString()
        {
            return Convert.ToString(this.number);
        }
    }

    class Parenthesis : Token
    {
        public bool left;

        public Parenthesis(bool left)
        {
            this.left = left;
        }

        public override string ToString()
        {
            if (left) return "(";
            return ")";
        }
    }

}
(Vissza)