Skip to content

Instantly share code, notes, and snippets.

@bolorundurowb
Created November 26, 2016 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bolorundurowb/a49a900eeece39f29f1cdd17a1178c20 to your computer and use it in GitHub Desktop.
Save bolorundurowb/a49a900eeece39f29f1cdd17a1178c20 to your computer and use it in GitHub Desktop.
This snippet takes an arithmetic expression as a string and attempts to evaluate it
using System;
using System.Linq;
public class Calc
{
/// <summary>
/// Takes a mathematical expression and tries to evaluate it
/// </summary>
/// <param name="expression">The mathematical expression</param>
/// <returns>The result of evaluating the string</returns>
public double DoCalc(string expression)
{
string[] parts = expression.Split(' ');
while (parts.Contains("/"))
{
parts = Division(parts);
}
while (parts.Contains("*"))
{
parts = Multiplication(parts);
}
while (parts.Contains("+"))
{
parts = Addition(parts);
}
while (parts.Contains("-"))
{
parts = Subtraction(parts);
}
return double.Parse(parts[0]);
}
public string[] Division(string[] parts)
{
int index = Array.IndexOf(parts, "/");
if (index != 0 || index != -1)
{
double value = double.Parse(parts[index - 1]) / double.Parse(parts[index + 1]);
string[] result = new string[parts.Length - 2];
for (int i = 0; i < index - 1; i++)
{
result[i] = parts[i];
}
for (int j = parts.Length; j > index + 1; j--)
{
result[j - 3] = parts[j - 1];
}
result[index - 1] = value.ToString();
return result;
}
else
{
return parts;
}
}
public string[] Multiplication(string[] parts)
{
int index = Array.IndexOf(parts, "*");
if (index != 0 || index != -1)
{
double value = double.Parse(parts[index - 1]) * double.Parse(parts[index + 1]);
string[] result = new string[parts.Length - 2];
for (int i = 0; i < index - 1; i++)
{
result[i] = parts[i];
}
for (int j = parts.Length; j > index + 1; j--)
{
result[j - 3] = parts[j - 1];
}
result[index - 1] = value.ToString();
return result;
}
else
{
return parts;
}
}
public string[] Addition(string[] parts)
{
int index = Array.IndexOf(parts, "+");
if (index != 0 || index != -1)
{
double value = double.Parse(parts[index - 1]) + double.Parse(parts[index + 1]);
string[] result = new string[parts.Length - 2];
for (int i = 0; i < index - 1; i++)
{
result[i] = parts[i];
}
for (int j = parts.Length; j > index + 1; j--)
{
result[j - 3] = parts[j - 1];
}
result[index - 1] = value.ToString();
return result;
}
else
{
return parts;
}
}
public string[] Subtraction(string[] parts)
{
int index = Array.IndexOf(parts, "-");
if (index != 0 || index != -1)
{
double value = double.Parse(parts[index - 1]) - double.Parse(parts[index + 1]);
string[] result = new string[parts.Length - 2];
for (int i = 0; i < index - 1; i++)
{
result[i] = parts[i];
}
for (int j = parts.Length; j > index + 1; j--)
{
result[j - 3] = parts[j - 1];
}
result[index - 1] = value.ToString();
return result;
}
else
{
return parts;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment