コード例 #1
0
        public string MakeOperation(string text, string typeOfOperation)
        {
            bool firstOperation = false;

            if (!operationStack.Any())
            {
                previousValue = previousValue + Double.Parse(text);
                numberStack.Push(previousValue);
                firstOperation = true;
            }
            else
            {
                numberStack.Push(Double.Parse(text));
                previousOperation = operationStack.Pop();
            }
            if (firstOperation == true)
            {
                operationStack.Push(typeOfOperation);
                return(previousValue.ToString());
            }
            else
            {
                operationStack.Push(typeOfOperation);
            }
            switch (previousOperation)
            {
            case "+":
                firstValueFromStack  = numberStack.Pop();
                secondValueFromStack = numberStack.Pop();
                numberStack.Push(secondValueFromStack + firstValueFromStack);
                return(numberStack.Peek().ToString());

            case "-":
                firstValueFromStack  = numberStack.Pop();
                secondValueFromStack = numberStack.Pop();
                numberStack.Push(secondValueFromStack - firstValueFromStack);
                return(numberStack.Peek().ToString());

            case "*":
                firstValueFromStack  = numberStack.Pop();
                secondValueFromStack = numberStack.Pop();
                numberStack.Push(secondValueFromStack * firstValueFromStack);
                return(numberStack.Peek().ToString());

            case "/":
                firstValueFromStack  = numberStack.Pop();
                secondValueFromStack = numberStack.Pop();
                if (firstValueFromStack == 0)
                {
                    return("Cannot divide by zero!");
                }
                else
                {
                    numberStack.Push(secondValueFromStack / firstValueFromStack);
                    return(numberStack.Peek().ToString());
                }

            default:
                return("0");
            }
        }