예제 #1
0
        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}");
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        public int Rechne(Formel formel)
        {
            foreach (IRechenOperation o in operationen)
            {
                if (o.Operator == formel.Operator)
                {
                    return(o.Berechne(formel));
                }
            }

            throw new ArgumentException("Operator unbekannt");
        }
예제 #5
0
 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");
     }
 }
예제 #6
0
 public int Berechne(Formel f)
 {
     return(f.Operand1 * f.Operand2);
 }