/// <summary> /// Обработчик нажатия на клавишу-оператор /// </summary> /// <param name="sender">Объект-отправитель</param> /// <param name="e">Аргументы события</param> private void OnOperatorButtonClick(object sender, EventArgs e) { if (wasLastPressedGetAnswer) { wasLastPressedGetAnswer = false; previousOperator = "="; } var button = sender as Button; string currentOperator = button.Text; try { var updatedValues = CalcFunctions.CalculateCurrentAnswer(currentAnswer, currentInput, previousOperator); currentAnswer = updatedValues.Item2; currentValue = 0; currentInput = ""; previousOperator = currentOperator; UpdateScreen(); } catch (DivideByZeroException) { answerLabel.Text = "Дел ноль"; currentValue = 0; currentInput = ""; } catch (SyntaxErrorException) { answerLabel.Text = "ошибка ввода"; currentValue = 0; currentInput = ""; } }
/// <summary> /// Обработчик события нажатия на равно /// </summary> /// <param name="sender">Объект-отправитель</param> /// <param name="e">Аргументы события</param> private void OnGetAnswerClick(object sender, EventArgs e) { wasLastPressedGetAnswer = true; operatorLabel.Text = "="; try { var updatedValues = CalcFunctions.CalculateCurrentAnswer(currentAnswer, currentInput, previousOperator); currentValue = updatedValues.Item1; currentAnswer = updatedValues.Item2; } catch (DivideByZeroException) { answerLabel.Text = "Дел ноль"; currentValue = 0; currentInput = ""; return; } catch (SyntaxErrorException) { answerLabel.Text = "ошибка ввода"; currentValue = 0; currentInput = ""; return; } string toPrint = currentAnswer.ToString(); int index = toPrint.IndexOf(','); if (index > 0 && ((index + 2) < toPrint.Length)) { toPrint = toPrint.Substring(0, index + 3); } if (toPrint.Length <= 8) { answerLabel.Text = toPrint; } else { answerLabel.Text = "Не влез"; } }
private static String Calculate(String value1, String value2, String op) { String res = null; int? total = null; switch (op) { case "+": total = CalcFunctions.addFunction(Int32.Parse(value1), Int32.Parse(value2)); break; case "-": total = CalcFunctions.minusFunction(Int32.Parse(value1), Int32.Parse(value2)); break; case "*": total = CalcFunctions.multipleFunction(Int32.Parse(value1), Int32.Parse(value2)); break; case "/": total = CalcFunctions.divideFunction(Int32.Parse(value1), Int32.Parse(value2)); break; default: total = null; break; } if (total != null) { res = value1 + " " + op + " " + value2 + " = " + total; } else { res = null; } return(res); }
public void SimpleCalculator() { CalcFunctions calFunctions = new CalcFunctions(); bool menu = true; try { calFunctions.GrabNumbers(); double userNumber1 = Convert.ToInt32(Console.ReadLine()); while (menu == true) { Further: calFunctions.GrabNumbers(); double userNumber2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Would you like to Add, Subtract, Multiply, Divide or Square these numbers?\r\nPress Q to quit.\r\n"); string userInput = Console.ReadLine().ToLower(); switch (userInput) { case "add": calFunctions.AddNumbers(ref userNumber1, ref userNumber2); Console.WriteLine(calFunctions.answer); break; case "subtract": calFunctions.SubtractNumbers(ref userNumber1, ref userNumber2); Console.WriteLine(calFunctions.answer); break; case "multiply": calFunctions.MultiplyNumbers(ref userNumber1, ref userNumber2); Console.WriteLine(calFunctions.answer); break; case "divide": calFunctions.DivideNumbers(ref userNumber1, ref userNumber2); Console.WriteLine(calFunctions.answer); break; case "square": calFunctions.Exponential(ref userNumber1, ref userNumber2); Console.WriteLine(calFunctions.answer); break; case "q": Environment.Exit(1); break; default: Console.WriteLine("Not a valid option"); break; } calFunctions.MoreMath(); string secondAddInput = Console.ReadLine().ToLower(); if (secondAddInput == "yes") { calFunctions.answer = userNumber1; Console.Clear(); goto Further; } else { Console.WriteLine("Thank you and have a good day! "); Thread.Sleep(2000); Environment.Exit(1); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadLine(); } }