Example #1
0
        /// <summary>
        /// 运算符优先级比较
        /// </summary>
        /// <param name="optA">运算符类型A</param>
        /// <param name="optB">运算符类型B</param>
        /// <returns>A与B相比,-1,低;0,相等;1,高</returns>
        public static int ComparePriority(OperatorTypes optA, OperatorTypes optB)
        {
            if (optA == optB)
            {
                //A、B优先级相等
                return 0;
            }

            //乘,除,余(*,/,%)
            if ((optA >= OperatorTypes.MUL && optA <= OperatorTypes.MOD) &&
                (optB >= OperatorTypes.MUL && optB <= OperatorTypes.MOD))
            {
                return 0;
            }
            //加,减(+,-)
            if ((optA >= OperatorTypes.ADD && optA <= OperatorTypes.SUB) &&
                (optB >= OperatorTypes.ADD && optB <= OperatorTypes.SUB))
            {
                return 0;
            }
            //小于,小于或等于,大于,大于或等于(<,<=,>,>=)
            if ((optA >= OperatorTypes.LT && optA <= OperatorTypes.GE) &&
                (optB >= OperatorTypes.LT && optB <= OperatorTypes.GE))
            {
                return 0;
            }
            //等于,不等于(=,<>)
            if ((optA >= OperatorTypes.ET && optA <= OperatorTypes.UT) &&
                (optB >= OperatorTypes.ET && optB <= OperatorTypes.UT))
            {
                return 0;
            }
            //三角函数
            if ((optA >= OperatorTypes.TAN && optA <= OperatorTypes.ATAN) &&
                    (optB >= OperatorTypes.TAN && optB <= OperatorTypes.ATAN))
            {
                return 0;
            }

            if (optA < optB)
            {
                //A优先级高于B
                return 1;
            }

            //A优先级低于B
            return -1;
        }
Example #2
0
 public Operator(OperatorTypes type, string value)
 {
     this.Type = type;
     this.Value = value;
 }
Example #3
0
 /// <summary>
 /// Handle click event on the addition button to perform an addition operation
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Add_Click(object sender, RoutedEventArgs e)
 {
     currentOperation = OperatorTypes.Addition;
     isNewNumber = true;
 }
Example #4
0
 /// <summary>
 /// Handle click event on the substraction button to perform an substraction operation
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Subtract_Click(object sender, RoutedEventArgs e)
 {
     currentOperation = OperatorTypes.Subtraction;
     isNewNumber = true;
 }
Example #5
0
 /// <summary>
 /// Handle click event on the multiplication button to perform an multiplication operation
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Multiply_Click(object sender, RoutedEventArgs e)
 {
     currentOperation = OperatorTypes.Multiplication;
     isNewNumber = true;
 }
Example #6
0
 /// <summary>
 /// Handle click event on the equals button to perform the operation in course
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Equals_Click(object sender, RoutedEventArgs e)
 {
     operands.Add(DisplayNumber);
     switch (currentOperation)
     {
         case OperatorTypes.Addition:
             DisplayNumber = SumOperands();
             break;
         case OperatorTypes.Subtraction:
             DisplayNumber = SubstractOperands();
             break;
         case OperatorTypes.Multiplication:
             DisplayNumber = MultiplyOperands();
             break;
         case OperatorTypes.Division:
             DisplayNumber = DivideOperands();
             break;
         default:
             break;
     }
     isNewNumber = true;
     currentOperation = OperatorTypes.None;
     operands.Clear();
 }
Example #7
0
 /// <summary>
 /// Handle click event on the division button to perform an division operation
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Divide_Click(object sender, RoutedEventArgs e)
 {
     currentOperation = OperatorTypes.Division;
     isNewNumber = true;
 }