public int Berechne(Formel formel)
        {
            if (!rechenoperationen.Any(x => x.Operator == formel.Operator))
            {
                throw new NotSupportedException("Diese Rechenoperation wird nicht unterstütz");
            }

            return(rechenoperationen.First(x => x.Operator == formel.Operator)
                   .Berechne(formel.Operand1, formel.Operand2));
        }
Exemplo n.º 2
0
 public int Calculate(Formel f)
 {
     foreach (var rart in rechenarten)
     {
         if (rart.Operator == f.Operator)
         {
             return(rart.Berechne(f.Operand1, f.Operand2));
         }
     }
     throw new ArgumentException("Operator unbekannt");
 }
Exemplo n.º 3
0
        public Formel Parse(string input)
        {
            Formel f = new Formel();

            string[] formel = input.Split(); // nach leerzeichen trennen
            f.Operand1 = Convert.ToInt32(formel[0]);
            f.Operand2 = Convert.ToInt32(formel[2]);
            f.Operator = Convert.ToChar(formel[1]);

            return(f);
        }
Exemplo n.º 4
0
            public Formel Parse(string input)
            {
                Formel output = new Formel();

                string[] parts = input.Split();
                output.Operand1 = Convert.ToInt32(parts[0]);
                output.Operator = parts[1];
                output.Operand2 = Convert.ToInt32(parts[2]);

                return(output);
            }
Exemplo n.º 5
0
 public int Calculate(Formel f)
 {
     if (Rechenarten.Any(x => x.Operator == f.Operator))
     {
         var rechenart = Rechenarten.First(x => x.Operator == f.Operator);
         return(rechenart.Berechne(f.Operand1, f.Operand2));
     }
     else
     {
         throw new FormatException($"Der Operator {f.Operator} wird nicht unterstüzt");
     }
 }
Exemplo n.º 6
0
        public int Berechne(Formel formel)
        {
            var rechenart = rechenarten.FirstOrDefault(x => x.Rechenoperator == formel.Rechenoperator);

            if (rechenart != null)
            {
                return(rechenart.Berechne(formel.Operand1, formel.Operand2));
            }
            else
            {
                throw new InvalidOperationException($"Operator '{formel.Rechenoperator}'ist leider unbekannt");
            }
        }
Exemplo n.º 7
0
        public int Berechne(Formel formel)
        {
            if (formel.Operator == '+')
            {
                return(formel.Operand1 + formel.Operand2);
            }
            else if (formel.Operator == '-')
            {
                return(formel.Operand1 + formel.Operand2);
            }

            throw new NotSupportedException("Operator ist unbekannt");
        }
Exemplo n.º 8
0
        public void Start()
        {
            Console.WriteLine("Bitte geben Sie die Formel ein:"); // "2 + 2"
            string eingabe = Console.ReadLine();

            Formel f   = parser.Parse(eingabe);
            int    erg = rechner.Calculate(f);

            Console.WriteLine($"Das Ergebnis ist {erg}");

            Console.WriteLine("---ENDE---");
            Console.ReadKey();
        }
 public int Calculate(Formel formel)
 {
     if (formel.Operator == "+")
     {
         return(formel.Operand1 + formel.Operand2);
     }
     else if (formel.Operator == "-")
     {
         return(formel.Operand1 - formel.Operand2);
     }
     else
     {
         throw new FormatException($"Operator '{formel.Operator}' wird nicht unterstützt");
     }
 }
Exemplo n.º 10
0
 public int Calculate(Formel f)
 {
     if (f.Operator == '+')
     {
         return(f.Operand1 + f.Operand2);
     }
     else if (f.Operator == '-')
     {
         return(f.Operand1 - f.Operand2);
     }
     else
     {
         throw new Exception("Operator unbekannt");
     }
 }
 public int Berechne(Formel formel)
 {
     if (formel.Rechenoperator == "+")
     {
         return(formel.Operand1 + formel.Operand2);
     }
     else if (formel.Rechenoperator == "-")
     {
         return(formel.Operand1 - formel.Operand2);
     }
     else
     {
         throw new InvalidOperationException("Der Rechenoperator ist unbekannt");
     }
 }
            // https://regexr.com/
            public Formel Parse(string input)
            {
                Regex r      = new Regex(@"(\d+)\s*(\D+?)\s*(\d+)");
                var   result = r.Match(input);

                if (result.Success)
                {
                    Formel output = new Formel();
                    output.Operand1 = Convert.ToInt32(result.Groups[1].Value);
                    output.Operand2 = Convert.ToInt32(result.Groups[3].Value);
                    output.Operator = result.Groups[2].Value;
                    return(output);
                }
                else
                {
                    throw new FormatException("Die Eingabe hat das falsche Formel-Format");
                }
            }
Exemplo n.º 13
0
        public Formel Parse(string input)
        {
            Regex r      = new Regex(@"\b(\d+)\s*(\D)\s*(\d+)\b");
            var   result = r.Match(input);

            if (result.Success)
            {
                Formel f = new Formel();
                f.Operand1 = Convert.ToInt32(result.Groups[1].Value); // 2
                f.Operator = result.Groups[2].Value[0];               // +
                f.Operand2 = Convert.ToInt32(result.Groups[3].Value); // 2
                return(f);
            }
            else
            {
                throw new FormatException("Ihre Eingabe ist keine gültige Formel");
            }
        }
Exemplo n.º 14
0
        public Formel Parse(string input)
        {
            Regex r      = new Regex(@"(\d+)\s*(\D)\s*(\d+)");
            var   result = r.Match(input);

            if (!result.Success)
            {
                throw new FormatException("Falsches Formelformat");
            }

            Formel f = new Formel();

            // Info: Groups[0] == Alles
            f.Operand1 = Convert.ToInt32(result.Groups[1].Value);
            f.Operator = Convert.ToChar(result.Groups[2].Value);
            f.Operand2 = Convert.ToInt32(result.Groups[3].Value);

            return(f);
        }
        public Formel Parse(string input)
        {
            Regex r      = new Regex(@"(\d+)\s*([+\-*/])\s*(\d+)", RegexOptions.Compiled);
            var   result = r.Match(input);

            if (result.Success)
            {
                Formel formel = new Formel();

                formel.Operand1       = Convert.ToInt32(result.Groups[1].Value);
                formel.Rechenoperator = result.Groups[2].Value;
                formel.Operand2       = Convert.ToInt32(result.Groups[3].Value);

                return(formel);
            }
            else
            {
                throw new FormatException("Ungültiges Formelformat");
            }
        }
Exemplo n.º 16
0
 static string FormelToString(Formel formel)
 {
     return(formel.Id + "[" + string.Join(", ", formel.Params.Select(p => $"{p.Name}={p.Value?.ToString() ?? p.Variable}")) + "]");
 }