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)); }
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"); }
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); }
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); }
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"); } }
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"); } }
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"); }
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"); } }
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"); } }
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"); } }
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"); } }
static string FormelToString(Formel formel) { return(formel.Id + "[" + string.Join(", ", formel.Params.Select(p => $"{p.Name}={p.Value?.ToString() ?? p.Variable}")) + "]"); }