I am relatively new to programming and am currently working on a string based calculator in C #. Many of them work fine, but I have problems with negative odds. My calculator engine is always looking 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 cannot be calculated. How can I separate the operator and the coefficient? What I came up with so far (a small piece of all the 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);
}
else if (nextOperation.Contains("-") && nextOperation.IndexOf("-") == 0)
{
}
return partialFormula;
source
share