tweedledum11
tweedledum11

Reputation: 121

Negative value in string based C# Calculator

I'm relatively new to programming, and I'm currently working on a C# string based calculator. A lot of it works fine already, but I'm having problems with negative coefficients. My calculator engine always looks for the next operator and calculates accordingly, so the problem is that if I want to calculate "-5+6", the first operation is "-5", but it obviously can't be calculated. How can I separate operator and coefficient? What I've come up with so far (small extract of the whole code)

if (nextOperation.Contains("+"))
        {
            string firstOperationResult = Calculate(nextOperation.Split('+').ToList(), "+")[0];
            string partialFormulaReplacement = partialFormula.Replace(nextOperation, firstOperationResult);

            return CalculateDashOperation(partialFormulaReplacement);
        }

        else if (nextOperation.Contains("-") && nextOperation.IndexOf("-") > 0)
        {
            string resultOfFirstOperation = Calculate(nextOperation.Split('-').ToList(), "-")[0];
            string partialFormulaReplacement = partialFormula.Replace(nextOperation, resultOfFirstOperation);

            return CalculateDashOperation(partialFormulaReplacement);
        }
        //added
        else if (nextOperation.Contains("-") && nextOperation.IndexOf("-") == 0)
        {
            //what to do
        }
        //added
        return partialFormula;

Upvotes: 4

Views: 1168

Answers (3)

Igor Quirino
Igor Quirino

Reputation: 1195

Use NCalc Library:

Dont reinvent wheel, * and / priorities is already implemented.

Simple expressions

Expression e = new Expression("2 + 3 * 5"); Debug.Assert(17 == e.Evaluate());

Handles mathematical functional from System.Math

Debug.Assert(0 == new Expression("Sin(0)").Evaluate()); Debug.Assert(2 == new Expression("Sqrt(4)").Evaluate()); Debug.Assert(0 == new Expression("Tan(0)").Evaluate());

Upvotes: 0

Adam H
Adam H

Reputation: 1573

"-5" can be treated as meaning "0-5", so you could say there's an implicit zero if you see an operand in the first position of the string. Note that this approach will only work for the operators + and -.

As for the problem of attempting to calculate "-5" again, I suggest you use the 0 as an argument to your Calculate function, rather than prepending it to the string you're processing:

Calculate(new List<string>{"0", nextOperation[1]}, "-")

Also, as has been pointed out in the comments, this approach will not cover all possible cases, and if this isn't an academic exercise then there are solutions out there that already solve this problem.

Upvotes: 3

SolvedForHome
SolvedForHome

Reputation: 162

The sample code looks a little short. But let's try to suggest: nextOperation is a string containing something like "1 * -6 + 6"

In order to evaluate this expression yout have to 'encrypt' your string first. The topic you are looking for is parenthesis A nice explanation of the basic (in python) can be found here.

But this question is already answered here.

Upvotes: 0

Related Questions