static void CalculateNaturalLogarithm() { Console.Write("Natural logarithm of float: "); ArithmeticOperations.DisplayExecutionTime(() => { float result = 1000000f; for (int i = 0; i < 100000; i++) { result = (float)Math.Log(result, Math.E); } }); Console.Write("Natural logarithm of double: "); ArithmeticOperations.DisplayExecutionTime(() => { double result = 1000000.0; for (int i = 0; i < 100000; i++) { result = Math.Log(result, Math.E); } }); Console.Write("Natural logarithm of decimal: "); ArithmeticOperations.DisplayExecutionTime(() => { decimal result = 1000000m; for (int i = 0; i < 100000; i++) { //StackOverflowException() //result = (decimal)Math.Log((double)result, Math.E); } }); }
static void Main(string[] args) { Console.Write("Enter first variable -> "); int firstV = int.Parse(Console.ReadLine()); Console.Write("Enter second variable -> "); int secondV = Int32.Parse(Console.ReadLine()); Console.Write("Enter third variable -> "); int thirdV = Int32.Parse(Console.ReadLine()); var operations = new ArithmeticOperations(); operations.Average(firstV, secondV, thirdV); Console.WriteLine(operations.Sum(firstV, secondV, thirdV)); Console.WriteLine(operations.Average(firstV, secondV, thirdV)); var tell = new Tell(); Console.WriteLine("What is your name?"); var name = Console.ReadLine(); tell.Greeting(name); }
static void CalculateSquareRoot() { Console.Write("Square root of float: "); ArithmeticOperations.DisplayExecutionTime(() => { float result = 1000000f; for (int i = 0; i < 100000; i++) { result = (float)Math.Sqrt(result); } }); Console.Write("Square root of double: "); ArithmeticOperations.DisplayExecutionTime(() => { double result = 1000000.0; for (int i = 0; i < 100000; i++) { result = Math.Sqrt(result); } }); Console.Write("Square root of decimal: "); ArithmeticOperations.DisplayExecutionTime(() => { decimal result = 1000000m; for (int i = 0; i < 100000; i++) { result = (decimal)Math.Sqrt((double)result); } }); }
/// <summary> /// Calculates the sum /// </summary> public FuncParameter Aggregate(IEnumerable <IComparable> myValues, IPropertyDefinition myPropertyDefinition) { var sumType = myPropertyDefinition.BaseType; IComparable sum = null; try { foreach (var value in myValues) { if (sum == null) { sum = ArithmeticOperations.Add(sumType, 0, value); } else { sum = ArithmeticOperations.Add(sumType, sum, value); } } } catch (ArithmeticException e) { throw new InvalidArithmeticAggregationException(sumType, this.PluginShortName, e); } return(new FuncParameter(sum, myPropertyDefinition)); }
public static void QuickSortArray() { Console.WriteLine("Quick sort"); int[] array = Sorting.GenerateRandomArray(); int[] intArray = new int[100]; Array.Copy(array, intArray, 100); Console.Write("Sort array of random int: "); ArithmeticOperations.DisplayExecutionTime(() => { intArray = QuickSort <int>(intArray, 0, 99); }); double[] doubleArray = new double[100]; Array.Copy(array, doubleArray, 100); Console.Write("Sort array of random double: "); ArithmeticOperations.DisplayExecutionTime(() => { doubleArray = QuickSort <double>(doubleArray, 0, 99); }); string[] stringArray = new string[100]; for (int count = 0; count < 100; count++) { stringArray[count] = array[count].ToString(); } Console.Write("Sort array of random string: "); ArithmeticOperations.DisplayExecutionTime(() => { stringArray = QuickSort <string>(stringArray, 0, 99); }); }
public static Operation <TNumber> GetArithmeticOperation <TNumber>(ArithmeticOperations opType) { Operation <TNumber> returnOp = null; switch (opType) { case ArithmeticOperations.PLUS: returnOp = new Operation <TNumber>("+", addition <TNumber>); break; case ArithmeticOperations.MINUS: returnOp = new Operation <TNumber>("-", subtraction <TNumber>); break; case ArithmeticOperations.TIMES: returnOp = new Operation <TNumber>("X", multiplication <TNumber>); break; case ArithmeticOperations.DIVIDE: returnOp = new Operation <TNumber>("÷", division <TNumber>); break; default: throw new NotSupportedException("Operation not supported"); } return(returnOp); }
public static void SelectionSortArray() { Console.WriteLine("Selection sort"); int[] sortedArray = new int[100]; for (int count = 0; count < 100; count++) { sortedArray[count] = count; } Console.Write("Sort array of sorted int: "); ArithmeticOperations.DisplayExecutionTime(() => { sortedArray = SelectionSort <int>(sortedArray, int.MaxValue); }); int[] reversedArray = new int[100]; for (int count = 0; count < 100; count++) { reversedArray[count] = 100 - count; } Console.Write("Sort array of reversed int: "); ArithmeticOperations.DisplayExecutionTime(() => { reversedArray = SelectionSort <int>(reversedArray, int.MaxValue); }); Console.Write("Sort array of random int: "); int[] array = Sorting.GenerateRandomArray(); int[] intArray = new int[100]; Array.Copy(array, intArray, 100); ArithmeticOperations.DisplayExecutionTime(() => { intArray = SelectionSort <int>(intArray, int.MaxValue); }); double[] doubleArray = new double[100]; Array.Copy(array, doubleArray, 100); Console.Write("Sort array of random double: "); ArithmeticOperations.DisplayExecutionTime(() => { doubleArray = SelectionSort <double>(doubleArray, double.MaxValue); }); string[] stringArray = new string[100]; for (int count = 0; count < 100; count++) { stringArray[count] = array[count].ToString(); } Console.Write("Sort array of random string: "); ArithmeticOperations.DisplayExecutionTime(() => { stringArray = SelectionSort <string>(stringArray, "9999999"); }); }
public static void SelectOperation(ArithmeticOperations operations) { switch ((int)operations) { case 1: { EnterNums(); MyDelegate Addition = (double a, double b) => { return(a + b); }; double result = Addition(Num1, Num2); Console.WriteLine($"Ответ: {result}"); break; } case 2: { EnterNums(); MyDelegate Subtraction = (double a, double b) => { return(a - b); }; double result = Subtraction(Num1, Num2); Console.WriteLine($"Ответ: {result}"); break; } case 3: { EnterNums(); MyDelegate Multiplying = (double a, double b) => { return(a * b); }; double result = Multiplying(Num1, Num2); Console.WriteLine($"Ответ: {result}"); break; } case 4: { EnterNums(); #region Проверка деления на НОЛЬ while (Num2 == 0) { if (Num2 == 0) { NumsEnter num2 = () => { Console.WriteLine("На ноль делить нельзя, введите новое число"); return(Convert.ToDouble(Console.ReadLine())); }; Num2 = num2(); } } #endregion #region Проверка деления на НОЛЬ в лямбда операторе при помощи тернарного оператора MyDelegate Division = (double a, double b) => b != 0 ? a / b : 0; #endregion double result = Division(Num1, Num2); Console.WriteLine($"Ответ: {result}"); break; } } }
public static void SelectOperation(ArithmeticOperations operations) { switch ((int)operations) { case 1: { Console.WriteLine("Введите первое число:"); int num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Введите второе число:"); int num2 = Convert.ToInt32(Console.ReadLine()); int result = Addition(num1, num2); Console.WriteLine($"Ответ: {result}"); break; } case 2: { Console.WriteLine("Введите первое число:"); int num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Введите второе число:"); int num2 = Convert.ToInt32(Console.ReadLine()); int result = Subtraction(num1, num2); Console.WriteLine($"Ответ: {result}"); break; } case 3: { Console.WriteLine("Введите первое число:"); int num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Введите второе число:"); int num2 = Convert.ToInt32(Console.ReadLine()); int result = Multiplying(num1, num2); Console.WriteLine($"Ответ: {result}"); break; } case 4: { Console.WriteLine("Введите первое число:"); int num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Введите второе число:"); int num2 = Convert.ToInt32(Console.ReadLine()); double result = Division(num1, num2); Console.WriteLine($"Ответ с округлением: {string.Format("{0:N2}",result)}"); Console.WriteLine($"Ответ без округления: {result}"); break; } } }
private void ExecuteButton_Click(object sender, RoutedEventArgs e) { ArithmeticOperations arithmeticSavings = GetTotalSavings; ArithmeticOperations arithmeticBalance = GetBalance; ArithmeticOperations arithmeticCommission = GetTellerCommission; double[] saves = new double[] { 4, 6, 3, 1 }; //textboxAnswer.Text = arithmeticSavings(saves, "Victor"); // Multicast method 1 ArithmeticOperations allOperations = arithmeticSavings + arithmeticBalance + arithmeticCommission; textboxAnswer.Text = allOperations(saves, "Kabir"); // Multicast Method 2 ArithmeticOperations allOps = new ArithmeticOperations(GetTotalSavings); allOps += GetBalance; allOps += GetTellerCommission; // double[] saves = new double[] { 4, 6, 64, 3} // ATM }
private ArithmeticOperations getNextOperation() { ArithmeticOperations returnValue = default(ArithmeticOperations); if (OpListBox.CheckedIndices.Count == 0) { OpListBox.SetItemChecked(0, true); } int randomIndex = DateTime.Now.Millisecond % OpListBox.CheckedIndices.Count; int i = 0; foreach (string item in OpListBox.CheckedItems) { if (i == randomIndex) { returnValue = (ArithmeticOperations)Enum.Parse(typeof(ArithmeticOperations), item); } i++; } return(returnValue); }
private ExpressionResponse ParseMDASExpression(string expression, char arithmeticOperator, int?precision) { var strArray = expression.Split(arithmeticOperator); var result = ArithmeticOperations.EvaluateMDAS(strArray[0], arithmeticOperator, strArray[1]); if (result != null) { return(new ExpressionResponse() { error = null, result = new List <string>() { result.ToString() } }); } else { return(new ExpressionResponse() { error = $"There was an error evaluating the expression - {expression}", result = null }); } }
/// <summary> /// Calculates the average /// </summary> public FuncParameter Aggregate(IEnumerable <IComparable> myValues, IPropertyDefinition myPropertyDefinition) { var divType = myPropertyDefinition.BaseType; IComparable sum = null; uint total = 0; foreach (var value in myValues) { if (sum == null) { sum = ArithmeticOperations.Add(divType, 0, value); } else { sum = ArithmeticOperations.Add(divType, sum, value); } total++; } var aggregateResult = ArithmeticOperations.Div(typeof(Double), sum, total); return(new FuncParameter(aggregateResult, myPropertyDefinition)); }
// division public static int Div(int a, int b) { return(ArithmeticOperations.Division(a, b)); }
public void PerformOperation() { if (Operand1 == null) { throw new InvalidOperationException($"Can't perform operation if {nameof(Operand1)} is null"); } if (Operand2 == null) { throw new InvalidOperationException($"Can't perform operation if {nameof(Operand2)} is null"); } if (Operator == AluOperator.Undefined) { throw new InvalidOperationException($"Can't perform operation if {nameof(Operator)} is undefined"); } Result = new Data(); switch (Operator) { case AluOperator.Add: ArithmeticOperations.Add(Result, Operand1, Operand2); break; case AluOperator.Subtract: ArithmeticOperations.Subtract(Result, Operand1, Operand2); break; case AluOperator.And: BitWiseOperations.And(Result, Operand1, Operand2); break; case AluOperator.Or: BitWiseOperations.Or(Result, Operand1, Operand2); break; case AluOperator.ExclusiveOr: BitWiseOperations.ExclusiveOr(Result, Operand1, Operand2); break; case AluOperator.Negate: BitWiseOperations.Negate(Result, Operand1); break; case AluOperator.ArithmeticShiftRight: BitShiftOperations.ArithmeticShiftRight(Result, Operand1, Operand2); break; case AluOperator.ArithmeticShiftLeft: BitShiftOperations.ArithmeticShiftLeft(Result, Operand1, Operand2); break; case AluOperator.LogicalShiftRight: BitShiftOperations.LogicalShiftRight(Result, Operand1, Operand2); break; case AluOperator.LogicalShiftLeft: BitShiftOperations.LogicalShiftLeft(Result, Operand1, Operand2); break; case AluOperator.RotateRight: BitShiftOperations.RotateRight(Result, Operand1, Operand2); break; case AluOperator.RotateLeft: BitShiftOperations.RotateLeft(Result, Operand1, Operand2); break; case AluOperator.RotateRightThroughCarry: BitShiftOperations.RotateRightThroughCarry(Result, Operand1, Operand2); break; case AluOperator.RotateLeftThroughCarry: BitShiftOperations.RotateLeftThroughCarry(Result, Operand1, Operand2); break; } }
static void Main(string[] args) { CreateMathLibrary.ArithmeticOperations o = new ArithmeticOperations(); Console.WriteLine("Calculator operations: 1.Add, 2.Subtract, 3.Multiply, 4.Division, 5.Modulus"); int i = int.Parse(Console.ReadLine()); Console.WriteLine("are your number 1.int or 2.double"); int j = int.Parse(Console.ReadLine()); if (j == 1) { Console.WriteLine("enter numbers"); int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); switch (i) { case 1: int add = o.Add(a, b); Console.WriteLine(add); break; case 2: int sub = o.Sub(a, b); Console.WriteLine(sub); break; case 3: int mul = o.Mul(a, b); Console.WriteLine(mul); break; case 4: int div = o.Div(a, b); Console.WriteLine(div); break; case 5: int mod = o.Mod(a, b); Console.WriteLine(mod); break; default: Console.WriteLine("enter correct value"); break; } } else if (j == 2) { Console.WriteLine("enter numbers"); double a = double.Parse(Console.ReadLine()); double b = double.Parse(Console.ReadLine()); switch (i) { case 1: double add = o.Add(a, b); Console.WriteLine(add); break; case 2: double sub = o.Sub(a, b); Console.WriteLine(sub); break; case 3: double mul = o.Mul(a, b); Console.WriteLine(mul); break; case 4: double div = o.Div(a, b); Console.WriteLine(div); break; case 5: double mod = o.Mod(a, b); Console.WriteLine(mod); break; default: Console.WriteLine("enter correct value"); break; } } else { Console.WriteLine("only int or double types"); } Console.ReadLine(); }
static void Main() { Console.WriteLine("1. Addition"); Console.WriteLine("2. Subtraction"); Console.WriteLine("3. Multiplication"); Console.WriteLine("4. Division"); Console.WriteLine("5. Modulo"); Console.WriteLine("Select an operation"); ArithmeticOperations d = new ArithmeticOperations(); int option = int.Parse(Console.ReadLine()); if (option >= 1 && option <= 5) { Console.WriteLine("Enter Two Numbers"); double a = double.Parse(Console.ReadLine()); var b = double.Parse(Console.ReadLine()); switch (option) { case 1: { //if (a.Contains(".") && b.Contains(".")) Console.WriteLine(d.Add((a), (b))); //else // Console.WriteLine(d.Add(int.Parse(a), int.Parse(b))); break; } case 2: { //if (a.Contains(".") && b.Contains(".")) Console.WriteLine(d.Subtract((a), (b))); //else // Console.WriteLine(d.Subtract(int.Parse(a), int.Parse(b))); break; } case 3: { //if (a.Contains(".") && b.Contains(".")) Console.WriteLine(d.Multiply((a), (b))); //else // Console.WriteLine(d.Multiply(int.Parse(a), int.Parse(b))); break; } case 4: { // if (a.Contains(".") && b.Contains(".")) Console.WriteLine(d.Division((a), (b))); //else // Console.WriteLine(d.Division(int.Parse(a), int.Parse(b))); break; } case 5: { // if (a.Contains(".") && b.Contains(".")) Console.WriteLine(d.Modulo((a), (b))); //else // Console.WriteLine(d.Modulo(int.Parse(a), int.Parse(b))); break; } } } else { Console.WriteLine("Wrong Input"); } }
// addition public static int Add(int a, int b) { return(ArithmeticOperations.Addition(a, b)); }
// substraction public static int Sub(int a, int b) { return(ArithmeticOperations.Substraction(a, b)); }
public static IProblem GetArithmeticProblem <TNumber>(ArithmeticOperations op, int numberCount, int maxNumber, bool includeNegative) { Operation <TNumber> operation = OperationFactory.GetArithmeticOperation <TNumber>(op); return(new ArithmeticProblem <TNumber>(operation, generateDecreasingRandomNumbers <TNumber>(numberCount, maxNumber, includeNegative))); }
static void Main(string[] args) { try { ArithmeticOperations ao = new ArithmeticOperations(); Console.WriteLine("Enter Two Integers : "); int firstNumber = Convert.ToInt32(Console.ReadLine()); int secondNumber = Convert.ToInt32(Console.ReadLine()); if (firstNumber < 0 || secondNumber < 0) { throw (new CustomException("Input numbers cannot be negative")); } Console.WriteLine("Enter choice : \n 1.Add \n 2.Subtract \n 3.Multiply \n 4.Divide"); int choice = Convert.ToInt16(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Result of Addition : {0}", ao.MyAdd(firstNumber, secondNumber)); break; case 2: Console.WriteLine("Result of Subtraction : {0}", ao.MySubtract(firstNumber, secondNumber)); break; case 3: Console.WriteLine("Result of Multiplication : {0}", ao.MyMultiply(firstNumber, secondNumber)); break; case 4: Console.WriteLine("Result of Division : {0}", ao.MyDivide(firstNumber, secondNumber)); break; default: Console.WriteLine("Invalid choice"); break; } } catch (DivideByZeroException e) { Console.WriteLine(e); } catch (FormatException e) { Console.WriteLine(e.StackTrace); } catch (InvalidCastException e) { Console.WriteLine(e.InnerException); } catch (NullReferenceException e) { Console.WriteLine(e.Message); } catch (CustomException e) { Console.WriteLine(e.Message + " (This is a Custom Exception)"); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); }
// multiplication public static int Mult(int a, int b) { return(ArithmeticOperations.Multiplication(a, b)); }
/// <summary> /// Creates a new instance of this class. /// </summary> /// <param name="left">Left operand.</param> /// <param name="right">Right operand.</param> /// <param name="opr">Binary arithmetic operator.</param> public Arithmetic(IExpression left, IExpression right, ArithmeticOperations opr) { Left = left; Right = right; Operator = opr; }