public void symbols(int i) { int j = 0; switch (ac[i]) { case '!': double num = opnum.Pop();num = Functions.factorial(num);opnum.Push(num);break;//factorial为阶乘运算。 case '(': opt op0 = new opt(ac[i], 4); optr.Push(op0); break; case ')': opt op1 = new opt('(', 4); opt op2 = optr.Pop(); while (op2.Equals(op1) != true) { op2 = optr.Pop(); } break;//计算括号间所有运算 case '+': j = 1; operation(i,j); break;//opreation需要重新定义 case '*': j = 2; operation(i,j); break; case '/': j = 2; operation(i,j); break; case '^': j = 3; operation(i,j); break; case '-': if (flag) { j = 1; operation(i,j); } else { double tempnum = opnum.Pop(); tempnum *= -1; opnum.Push(tempnum); }; break; default: break;//需重写 } //TODO push into the stack }
public void operation(int i,int j) { opt temp = optr.Peek(); opt inst = new opt(ac[i], j); double c = 0; if (temp.j > j) { //四则计算 double a = opnum.Pop(); double b = opnum.Pop(); char psmd = temp.opera; switch (psmd) { case '+': c = b + a; opnum.Push(c); break; case '-': c = b - a; opnum.Push(c); break; case '*': c = b * a; opnum.Push(c); break; case '/': if (a != 0) { c = b / a; opnum.Push(c); } break;//需要加入差错处理!!!!! case '^': c = Math.Pow (b,a); opnum.Push(c); break; default: break;//需要加入差错处理!!!!! } } optr.Push(inst); }