public void Start() { Console.WriteLine("Formel eingeben:"); string eingabe = Console.ReadLine(); // "2 + 2" Formel x = parser.Parse(eingabe); var ergebnis = rechner.Rechne(x); Console.WriteLine($"Das Ergebnis is {ergebnis}"); }
public Formel Parse(String input) { Formel output = new Formel(); var match = Regex.Match(input, @"(\d+)\s*(\D)\s*(\d+)"); //TODO: regex output.Operand1 = Convert.ToInt32(match.Groups[1].Value); output.Operand2 = Convert.ToInt32(match.Groups[3].Value); output.Operator = match.Groups[2].Value[0]; return(output); }
public Formel Parse(String input) { Formel output = new Formel(); string[] operanden = input.Split('+'); //TODO: regex output.Operand1 = Convert.ToInt32(operanden[0]); output.Operand2 = Convert.ToInt32(operanden[1]); output.Operator = '+'; return(output); }
public int Rechne(Formel formel) { foreach (IRechenOperation o in operationen) { if (o.Operator == formel.Operator) { return(o.Berechne(formel)); } } throw new ArgumentException("Operator unbekannt"); }
public int Rechne(Formel formel) { if (formel.Operator == '+') { return(formel.Operand1 + formel.Operand2); } else if (formel.Operator == '-') { return(formel.Operand1 - formel.Operand2); } else { throw new ArgumentException("Operator unbekannt"); } }
public int Berechne(Formel f) { return(f.Operand1 * f.Operand2); }