Skip to content

Instantly share code, notes, and snippets.

@harshityadav95
Created January 24, 2018 12:05
Show Gist options
  • Save harshityadav95/ad005eb5b0e2a3bc9f30d0596face211 to your computer and use it in GitHub Desktop.
Save harshityadav95/ad005eb5b0e2a3bc9f30d0596face211 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace calculatorApp
{
class operations
{
double _retrunValue = 0;
/// <summary>
/// Main calculation engine which apply operation based on operand and operator passed
/// </summary>
/// <param name="x"></param>
/// <param name="v1"></param>
/// <param name="v2"></param>
/// <returns></returns>
public double engine(string x, double v1,double v2=0)
{
switch (x)
{
case "+":
{
_retrunValue = v1 + v2;
}
break;
case "-":
{
_retrunValue = v1 - v2;
}
break;
case "/":
{
if (v2==0)
{
throw new System.DivideByZeroException();
}
_retrunValue = v1 / v2;
}
break;
case "√":
{
_retrunValue = Math.Sqrt(v1);
}
break;
case "*":
{
_retrunValue = v1*v2;
}
break;
case "±":
{
_retrunValue = v1 * -1;
}
break;
case "%":
{
if (v2 == 0 || v2==0)
{
throw new System.DivideByZeroException();
}
_retrunValue = v1 % v2;
}
break;
case "1/x":
{
if (v1==0)
{
throw new System.DivideByZeroException();
}
_retrunValue = 1/v1;
}
break;
}
return _retrunValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment