Tuesday, December 14, 2010

C# Tutorial: Equation Calculator++ [Trigonometric Functions]

Here is the code

private static double trigFunctions(char[] expr, ref int index)
{
    if (((int)expr[index] >= 48 && (int)expr[index] <= 57) || expr[index] == 46 || expr[index] == 45)
        return GetDouble(expr, ref index);
    else
    {
        char op = expr[index];
        if (op != 'c' && op != 't' && op != 's')
            return GetDouble(expr, ref index);
        index += 4;
        double x = GetDouble(expr, ref index);
        if (index + 1 != expr.Length) index++;
        if (op == 'c')
            return Math.Round(Math.Cos(x), MidpointRounding.AwayFromZero);
        else if (op == 't')
            return Math.Round(Math.Tan(x), MidpointRounding.AwayFromZero);
        else
            return Math.Round(Math.Sin(x), MidpointRounding.AwayFromZero);  
    }
}

1 comment:

  1. that the team means
    index += 4;
    double x = GetDouble(expr, ref index);
    if (index + 1 != expr.Length)
    index++;

    ReplyDelete