Operation
This is a code that implements four mixed operations, including three methods, which, when invoked, use the compute () method, which can be used for dynamic computations.
Code:
string Precede (string p, string q)
{
Switch (p)
{
Case "+":
Case "-":
Return ("*/". IndexOf (q)!=-1)? "<": ">";
Case "*":
Case "/":
return (q = = "()?" < ": >";
Case "(":
return (q = = ")")? "=": "<";
Case ")":
return (q = = "(") "?": ">";
Case "#":
return (q = = "#")? "=": "<";
}
Return "?";
}
Double Operate (Double A, char O, double b)
{
Switch (o)
{
Case ' + ':
return a + B;
Case '-':
return a-b;
Case ' * ':
return a * b;
Case '/':
return a/b;
}
return 0;
}
Object Compute (string expression)
{
Stack Narr = new stack (), Oarr = new stack ();
int j = 0;
Double A = 0, b = 0;
string w = "";
Char o;
MatchCollection arr = regex.matches (expression. Replace ("", "") + "#", @ "((? <= (^|\ ())-)? \d+ (\.\d+)? | \d) ");
Oarr.push ("#");
W = convert.tostring (arr[j++]);
while (!) ( w = "#" && convert.tostring (Oarr.peek ()) = = "#")
{
if ("+-*/()". IndexOf (W)!=-1)
{
Switch (precede (oarr.peek). ToString (), W))
{
Case "<":
Oarr.push (w);
W = convert.tostring (arr[j++]);
Break
Case "=":
Oarr.pop ();
W = convert.tostring (arr[j++]);
Break
Case ">":
o = Convert.tochar (Oarr.pop ());
b = convert.todouble (Narr.pop ());
A = Convert.todouble (Narr.pop ());
Narr.push (Operate (A, O, b));
Break
Default
Return "Calculation Error! ";
}
}
Else
{
Narr.push (w);
W = convert.tostring (arr[j++]);
}
}
return Narr.pop ();
}