Skip to content

Instantly share code, notes, and snippets.

@bobixaka
Created August 28, 2013 21:10
Show Gist options
  • Save bobixaka/6371344 to your computer and use it in GitHub Desktop.
Save bobixaka/6371344 to your computer and use it in GitHub Desktop.
Calculate math expressions input from the console
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
namespace Task_7_MathExpressions
{
class MathExpressions
{
static void Main()
{
Console.WriteLine("================================================================================");
Console.WriteLine(" Math expressions calculator!\n");
Console.WriteLine("================================================================================");
Console.WriteLine("Allowed elements:\n");
Console.WriteLine("* Real numbers, e.g. 5, 18.33, 3.14159, 12.6");
Console.WriteLine("* Arithmetic operators: +, -, *, / (standard priorities)");
Console.WriteLine("* Mathematical functions: ln(x), sqrt(x), pow(x,y)");
Console.WriteLine("* Brackets (for changing the default priorities)\n");
Console.WriteLine("================================================================================");
Console.Write("Please enter the math expression: ");
string ExpressionInput = Console.ReadLine();
//Make sure you don't have any spaces, because the program doesn't work with spaces
string Expression = ExpressionInput.Replace(" ", "");
double Result = 0;
double Brackets = 0;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
for (int i = 0; i < Expression.Length; i++)
{
char Operator = '.';
char OperatorBrackets = '.';
if (Expression[i] == '(')
{
CalculateExpressionInBrackets(Expression, ref Result, ref Brackets, ref i, ref Operator, ref OperatorBrackets);
}
else if (Expression[i] == 'l' && Expression[i + 1] == 'n' && Expression[i + 2] == '(')
{
CalculateLogarithm(Expression, ref Result, ref i, ref Operator);
}
else if (Expression[i] == 's' && Expression[i + 1] == 'q' && Expression[i + 2] == 'r' && Expression[i + 3] == 't'
&& Expression[i + 4] == '(')
{
CalculateSqrt(Expression, ref Result, ref i, ref Operator);
}
else if (Expression[i] == 'p' && Expression[i + 1] == 'o' && Expression[i + 2] == 'w' && Expression[i + 3] == '(')
{
CalculatePow(Expression, ref Result, ref i, ref Operator);
}
else if (Expression[i] != '(' && Expression[i] != ')' && Expression[i] != '+' && Expression[i] != '-'
&& Expression[i] != '*' && Expression[i] != '/')
{
CalculateNormalExpression(Expression, ref Result, ref i, ref Operator);
}
}
Console.WriteLine("\n********************************************************************************");
Console.WriteLine("{0} = {1:0.00}", ExpressionInput, Result);
Console.WriteLine("\n********************************************************************************");
}
private static void CalculateNormalExpression(string Expression, ref double Result, ref int i, ref char Operator)
{
if (i > 0)
{
Operator = Expression[i - 1];
}
string Number = "";
while (Expression[i] != '+' && Expression[i] != '-' && Expression[i] != '*' && Expression[i] != '/'
&& Expression[i] != '(' && Expression[i] != ')')
{
Number += Expression[i];
i++;
if (i == Expression.Length)
{
break;
}
}
double Num = double.Parse(Number);
switch (Operator)
{
case '+': Result += Num;
break;
case '-': Result -= Num;
break;
case '*': Result *= Num;
break;
case '/': Result /= Num;
break;
default: Result += Num;
break;
}
}
private static void CalculatePow(string Expression, ref double Result, ref int i, ref char Operator)
{
if (i > 0)
{
Operator = Expression[i - 1];
}
//Move directly to the expression after "pow("
i += 4;
string num = "";
string pow = "";
while (Expression[i] != ',')
{
num += Expression[i];
i++;
}
//Skip the "," in the expression in the brackets of pow(x, y)
i++;
while (Expression[i] != ')')
{
pow += Expression[i];
i++;
}
double Pow = Math.Pow(double.Parse(num), double.Parse(pow));
switch (Operator)
{
case '+': Result += Pow;
break;
case '-': Result -= Pow;
break;
case '*': Result *= Pow;
break;
case '/': Result /= Pow;
break;
default: Result += Pow;
break;
}
}
private static void CalculateSqrt(string Expression, ref double Result, ref int i, ref char Operator)
{
if (i > 0)
{
Operator = Expression[i - 1];
}
//Move directly to the expression after "sqrt("
i += 5;
string sqrt = "";
while (Expression[i] != ')')
{
sqrt += Expression[i];
i++;
}
double Sqrt = Math.Sqrt(double.Parse(sqrt));
switch (Operator)
{
case '+': Result += Sqrt;
break;
case '-': Result -= Sqrt;
break;
case '*': Result *= Sqrt;
break;
case '/': Result /= Sqrt;
break;
default: Result += Sqrt;
break;
}
}
private static void CalculateLogarithm(string Expression, ref double Result, ref int i, ref char Operator)
{
if (i > 0)
{
Operator = Expression[i - 1];
}
//Move directly to the expression after "ln("
i += 3;
string Log = "";
while (Expression[i] != ')')
{
Log += Expression[i];
i++;
}
double Logarithm = Math.Log(double.Parse(Log));
switch (Operator)
{
case '+': Result += Logarithm;
break;
case '-': Result -= Logarithm;
break;
case '*': Result *= Logarithm;
break;
case '/': Result /= Logarithm;
break;
default: Result += Logarithm;
break;
}
}
private static void CalculateExpressionInBrackets(string Expression, ref double Result, ref double Brackets, ref int i, ref char Operator, ref char OperatorBrackets)
{
if (i > 0)
{
OperatorBrackets = Expression[i - 1];
}
//Calculate the expression in brackets
while (Expression[i] != ')')
{
if (Expression[i] == 'l' && Expression[i + 1] == 'n' && Expression[i + 2] == '(')
{
if (i > 0)
{
Operator = Expression[i - 1];
}
//Move directly to the expression after "ln("
i += 3;
string Log = "";
while (Expression[i] != ')')
{
Log += Expression[i];
i++;
}
double Logarithm = Math.Log(double.Parse(Log));
switch (Operator)
{
case '+': Brackets += Logarithm;
break;
case '-': Brackets -= Logarithm;
break;
case '*': Brackets *= Logarithm;
break;
case '/': Brackets /= Logarithm;
break;
default: Brackets += Logarithm;
break;
}
}
else if (Expression[i] == 's' && Expression[i + 1] == 'q' && Expression[i + 2] == 'r' && Expression[i + 3] == 't'
&& Expression[i + 4] == '(')
{
if (i > 0)
{
Operator = Expression[i - 1];
}
//Move directly to the expression after "sqrt("
i += 5;
string sqrt = "";
while (Expression[i] != ')')
{
sqrt += Expression[i];
i++;
}
double Sqrt = Math.Sqrt(double.Parse(sqrt));
switch (Operator)
{
case '+': Brackets += Sqrt;
break;
case '-': Brackets -= Sqrt;
break;
case '*': Brackets *= Sqrt;
break;
case '/': Brackets /= Sqrt;
break;
default: Brackets += Sqrt;
break;
}
}
else if (Expression[i] == 'p' && Expression[i + 1] == 'o' && Expression[i + 2] == 'w' && Expression[i + 3] == '(')
{
if (i > 0)
{
Operator = Expression[i - 1];
}
//Move directly to the expression after "pow("
i += 4;
string num = "";
string pow = "";
while (Expression[i] != ',')
{
num += Expression[i];
i++;
}
//Skip the "," in the expression in the brackets in pow(x, y)
i++;
while (Expression[i] != ')')
{
pow += Expression[i];
i++;
}
double Pow = Math.Pow(double.Parse(num), double.Parse(pow));
switch (Operator)
{
case '+': Brackets += Pow;
break;
case '-': Brackets -= Pow;
break;
case '*': Brackets *= Pow;
break;
case '/': Brackets /= Pow;
break;
default: Brackets += Pow;
break;
}
}
else if (Expression[i] != '(' && Expression[i] != ')' && Expression[i] != '+' && Expression[i] != '-'
&& Expression[i] != '*' && Expression[i] != '/')
{
if (i > 0)
{
Operator = Expression[i - 1];
}
string Number = "";
while (Expression[i] != '+' && Expression[i] != '-' && Expression[i] != '*' && Expression[i] != '/'
&& Expression[i] != '(' && Expression[i] != ')')
{
Number += Expression[i];
i++;
if (i == Expression.Length)
{
break;
}
}
double Num = double.Parse(Number);
switch (Operator)
{
case '+': Brackets += Num;
break;
case '-': Brackets -= Num;
break;
case '*': Brackets *= Num;
break;
case '/': Brackets /= Num;
break;
default: Brackets += Num;
break;
}
}
//Move to the next character
if (Expression[i] != ')')
{
i++;
}
if (i == Expression.Length)
{
break;
}
}
//End of brackets
//Calculate the amount in the brackets in the result
switch (OperatorBrackets)
{
case '+': Result += Brackets;
break;
case '-': Result -= Brackets;
break;
case '*': Result *= Brackets;
break;
case '/': Result /= Brackets;
break;
default: Result += Brackets;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment