Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int eval(string exp)
- {
- stack1 s = new stack1 (9);
- for (int i = 0; i <= exp.Length - 1;i++ )
- {
- if (exp[i] != '*' && exp[i] != '+' && exp[i] != '/' && exp[i] != '-')
- {
- string t = exp[i].ToString();
- s.push(int.Parse(t));
- }
- else
- {
- switch(exp[i])
- {
- case '*':
- int op1 = s.pop();
- int op2 = s.pop();
- int res = op1 * op2;
- s.push(res);
- break;
- case '+':
- int op3 = s.pop();
- int op4 = s.pop();
- int res1 = op3 + op4;
- s.push(res1);
- break;
- case '-':
- int op5 = s.pop();
- int op6 = s.pop();
- int res2 = op6 - op5;
- s.push(res2);
- break;
- case '/':
- int op7 = s.pop();
- int op8 = s.pop();
- int res3 = op8 / op7;
- s.push(res3);
- break;
- }
- }
- }
- return s.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement