Beispiel #1
0
        public double Compute()
        {
            Stack<Token> compu_stack = new Stack<Token>();
            try{
                foreach(Token tok in calc_list){
                    Token res = new Token();

                    if(tok._class == (int)type.num || tok._class == (int)type.var){
                        compu_stack.Push(tok);
                    }else if(tok._class == (int)type.op){

                        Token rhs = compu_stack.Pop();
                        Token lhs = compu_stack.Pop();

                        switch (tok.repr)
                        {
                            case "+":
                                res.repr = (double.Parse(GetValue(lhs)) + double.Parse(GetValue(rhs))).ToString();
                                compu_stack.Push(res);
                                break;
                            case "-":
                                res.repr = (double.Parse(GetValue(lhs)) - double.Parse(GetValue(rhs))).ToString();
                                compu_stack.Push(res);
                                break;
                            case "/":
                                res.repr = (double.Parse(GetValue(lhs)) / double.Parse(GetValue(rhs))).ToString();
                                compu_stack.Push(res);
                                break;
                            case "*":
                                res.repr = (double.Parse(GetValue(lhs)) * double.Parse(GetValue(rhs))).ToString();
                                compu_stack.Push(res);
                                break;
                            default:
                                return double.Parse(compu_stack.Pop().repr);
                        }
                    }
                }
                return double.Parse(GetValue(compu_stack.Pop()));
            }catch(Exception ex){
                throw new Exception("Expression not well formed.", ex);
            }
        }
Beispiel #2
0
        private bool RPN(string expression)
        {
            Regex reg = new Regex(@"\[\b[A-Za-z]+\b(?![\d])\]|[\+\-/\*]|[\d]+(\.[\d]+)?|[()]");
            Stack<Token> op_stack = new Stack<Token>();

            foreach(Match token in reg.Matches(expression)){
                Token tok = new Token();
                string value = token.Captures[0].Value;

                if(IsNumeric(value)){
                    tok._class = (int)type.num;
                    tok.repr = value;
                    calc_list.Add(tok);

                }else if(IsOperator(value)){
                    while(op_stack.Count != 0 && op_stack.Peek().repr != "("){
                        if (HasPrecidenceOrEqual(op_stack.Peek().repr, value)){
                            calc_list.Add(op_stack.Pop());
                        }
                        else break;
                    }
                    tok._class = (int)type.op;
                    tok.repr = value;

                    op_stack.Push(tok);
                }else if (IsVariable(value)){
                    tok._class = (int)type.var;
                    tok.repr = value;
                    calc_list.Add(tok);

                }else if(value == "("){
                    tok._class = (int)type.var;
                    tok.repr = value;

                    op_stack.Push(tok);
                }else if(value == ")"){
                   while(op_stack.Count != 0 && op_stack.Peek().repr != "("){
                       calc_list.Add(op_stack.Pop());
                    }
                   if (op_stack.Count != 0) op_stack.Pop();
                }
            }
            while(op_stack.Count != 0){
                calc_list.Add(op_stack.Pop());
            }

            return true;
        }
Beispiel #3
0
 private string GetValue(Token input)
 {
     if(input._class == (int)type.num){
         return input.repr;
     }else{
         return  data_store[input.repr].ToString();
     }
 }