c# 3.0 - Issue with getting 2 chars from string using indexer -
I have a problem reading four values. See my program below I want to evaluate an infix expression.
As you can see, I want to read '10', '*', '20' and then use them ... But if I use string indexers [0] then '1' and Not '10' and therefore I am not able to get the expected results. Can you give me some suggestions? Code C #
class is in the program {static zero main (string [] arg) {string infix = "10 * 2 + 20-20 + 3"; Float result = evaluation infix (infix); Console.WriteLine (results); Console.ReadKey (); } Evaluation of public static float (String) {Stack & lt; Float & gt; Operand = new stack & lt; Float & gt; (); Stack & LT; Four & gt; Operator1 = new stack & lt; Char & gt; (); Int len = s.Length; For (int i = 0; i & lt; len; i ++) {if (Iopater (s [ii]) / has been an issue because s [i] gives each letter and i want number operator 10 Am Push (s [i]); Else {operand.Push (S [ii]); If (operand.Count == 2) compute (operand, operator1); }} Return Operand.Pop (); } Public static zero compute (stack & lt; float & gt; operand, stack & lt; char & operator1) {float operand1 = operand.Pop (); Float Operand 2 = Operand Pop (); Four op = operator1 Pop (); If (op == '+') operand.Push (operand1 + operand2); Else if (op == '-') operand.Push (operand1 - operand2); Else if (op == '*') operand.Push (operand1 * operand2); Else if (op == '/') operand.Push (operand1 / operand2); } Public Static Bull is Operator (four C) {bool result = false; If (C == '+' || C == '-' || C == '*' || C == '/') Result = true; Return result; }}}}
You will need to split the string - which means that the fact Working in How to you want to split the string I suspect you will find the most suitable partitioning equipment in this case, because you are working with patterns Alternatively, you have your own split daily You might want to write.
Do you just need to deal with integers and operators? How about spaces? Bracket? Major negative numbers? Multiplied by negative numbers (e.g. "3 * -5")?
Comments
Post a Comment