public static double DoCalculation(string[] args) { Calculations calc = new Calculations(); if (args[0] == "Add") { double a = double.Parse(args[1]); double b = double.Parse(args[2]); return(calc.Add(a, b)); } return(0); }
private void adding_Click(object sender, EventArgs e) { number1.Text = Calculations.RemoveWhiteCahracters(number1.Text); number2.Text = Calculations.RemoveWhiteCahracters(number2.Text); if (!Calculations.IsItNumber(number1.Text)) { MessageBox.Show("Number 1 is not really a number.\nYou can use digits, one comma and one minus at the front."); return; } if (!Calculations.IsItNumber(number2.Text)) { MessageBox.Show("Number 2 is not really a number.\nYou can use digits, one comma and one minus at the front."); return; } result.Text = Calculations.Add(number1.Text, number2.Text); }
static void Main(string[] args) { Console.WriteLine("Calculator App"); for (; ;) { Console.WriteLine(); Console.WriteLine("Select an operator"); Console.WriteLine("1. Add"); Console.WriteLine("2. Subtract"); Console.WriteLine("3. Multiply"); Console.WriteLine("4. Divide"); Console.WriteLine("5. Square Root"); Console.WriteLine("6. Power"); Console.WriteLine("7. Exit"); Console.Write("Operator: "); string response = Console.ReadLine(); if (Int32.TryParse(response, out int selection)) { double answer = -1; Calculations calculations = new Calculations(); switch (selection) { case 1: answer = calculations.Add(); break; case 2: answer = calculations.Subtract(); break; case 3: answer = calculations.Multiply(); break; case 4: answer = calculations.Divide(); break; case 5: answer = calculations.Square(); break; case 6: answer = calculations.Power(); break; case 7: Console.WriteLine("Thanks for using! Goodbye!"); Console.ReadLine(); Environment.Exit(0); break; default: Console.WriteLine("Invalid Entry"); break; } Console.WriteLine($"The answer is {answer}"); } else { Console.WriteLine("Invalid Entry"); } } }
static void Main(string[] args) { Console.Title = "Console calculator"; Console.WriteLine("This is an arithmetic claculator for console commands"); Console.WriteLine("Type 'HELP' to get the list of commands available"); Console.WriteLine("Type 'START' to start the calculation"); Console.WriteLine("To exit at any time type 'EXIT' command"); string cmd; do { Console.Write(":>"); cmd = Console.ReadLine().ToUpper(); //make input as case insensitive switch (cmd) { case "HELP": //help command case here PrintAvailableCommands(); break; case "EXIT": //exit command case here Environment.Exit(0); break; case "START": //start command case here { MathematicalDataContainer data = GetUserInput(); // get user input if (data.CorrrectInput) { if (data.MathOperation == "ADD") { data.Result = Calculations.Add(data.FirstNumber, data.SecondNumber); } else if (data.MathOperation == "SUB") { data.Result = Calculations.Subtract(data.FirstNumber, data.SecondNumber); } else if (data.MathOperation == "MUL") { data.Result = Calculations.Multiply(data.FirstNumber, data.SecondNumber); } else if (data.MathOperation == "DIV") { if (data.SecondNumber == 0) { PrintErrorMessage("Division by zero is not allowed!!"); break; } else { data.Result = Calculations.Divide(data.FirstNumber, data.SecondNumber); } } Console.WriteLine($"The result of the operation is: {data.Result}."); // print results of the calculation } else { PrintErrorMessage("Error!! Incorrect input!!"); // error message for failed input validation } break; } case "CLS": //clear screen command case Console.Clear(); break; default: PrintErrorMessage("Bad command!!"); //default wrong command message break; } }while (cmd.ToUpper() != "EXIT"); //run the screen till exit command typed }