Ejemplo n.º 1
0
        public static double SolveOperation(OperableEnum operable, params string[] numsString)
        {
            double[] nums   = new double[numsString.Length - 1];
            double   result = 0f;

            for (int i = 0; i < numsString.Length; i++)
            {
                if (i == 0)
                {
                    result = nums[0];
                    continue;
                }
                while (numsString[i - 1].StartsWith('_'))
                {
                    numsString[i - 1].Substring(1);
                }
                nums[i] = double.Parse(numsString[i - 1]);
            }
            switch (operable)
            {
            case OperableEnum.Add:
                foreach (double num in nums)
                {
                    result += num;
                }
                break;

            case OperableEnum.Subtract:
                foreach (double num in nums)
                {
                    result -= num;
                }
                break;

            case OperableEnum.Multiply:
                foreach (double num in nums)
                {
                    result *= num;
                }
                break;

            case OperableEnum.Divide:
                if (nums.Length != 1)
                {
                    throw new Exception("You cannot divide more than 2 things at once.");
                }
                result /= nums[0];
                break;

            case OperableEnum.Solved:
                break;

            default:
                throw new Exception("Operations.SolveOperation was given an operable it cannot yet handle.");
            }
            return(result);
        }
Ejemplo n.º 2
0
        public static OperableEnum ParseOperable(char operable)
        {
            OperableEnum value = OperableEnum.Solved;

            switch (operable)
            {
            case '+':
                value = OperableEnum.Add;
                break;

            case '-':
                value = OperableEnum.Subtract;
                break;

            case '*':
                value = OperableEnum.Multiply;
                break;

            case '/':
                value = OperableEnum.Divide;
                break;
            }
            return(value);
        }
Ejemplo n.º 3
0
 public TreeNode(string _id, OperableEnum _operation)
 {
     id        = _id;
     operation = _operation;
 }