コード例 #1
0
ファイル: AProblem.cs プロジェクト: sgpicone/SuperMaths
        public override string ToString()
        {
            string result = Values[0].ToString();

            for (int i = 0; i < Operators.Count; i++)
            {
                result += String.Format(" {0} {1}", OperatorExtensions.OperatorToString(Operators[i]), Values[i + 1].ToString());
            }
            return(result);
        }
コード例 #2
0
        public const double INVALID_ANSWER = -9999; //I have a sneaking feeling that this is terrible programming practice.

        /// <summary>
        /// Calculate the answer of a problem with an arbitrary number of values and operations.
        /// </summary>
        /// <param name="vals">The values of the problem</param>
        /// <param name="ops">The operators in the problem</param>
        /// <returns></returns>
        public static long CalcLong(List <long> vals, List <Operator> ops)
        {
            string equation = "";

            equation += vals[0];
            for (int i = 0; i < ops.Count; i++)
            {
                equation += OperatorExtensions.OperatorToString(ops[i]) + vals[i + 1];
            }
            double result = Convert.ToDouble(new DataTable().Compute(equation, null).ToString());

            result = Math.Round(result, 0);
            return((long)result);
        }
コード例 #3
0
        /// <summary>
        /// Calculate the answer of a problem with an arbitrary number of values and operations.
        /// </summary>
        /// <param name="vals">The values of the problem</param>
        /// <param name="ops">The operators in the problem</param>
        /// <returns>A double, rounded to 2 decimal places, representing the answer to this problem</returns>
        public static double CalcDouble(List <long> vals, List <Operator> ops)
        {
            string equation = "";
            double result   = 0;

            equation += vals[0];
            for (int i = 0; i < ops.Count; i++)
            {
                equation += OperatorExtensions.OperatorToString(ops[i]) + vals[i + 1];
            }
            try
            {
                result = Convert.ToDouble((new DataTable().Compute(equation, null).ToString()));
            }
            catch (OverflowException)
            {
                return(INVALID_ANSWER);
            }
            result = Math.Round(result, 2);
            return(result);
        }