Exemple #1
0
        /* Split the equation into two parts based on the equal operator,
         * to identify which hand side contains the unknown variable or constant
         * numbers only. Then implement different calculation process based on the certain content.
         * E.g. if right hand side has unknown variable, then it will deal with X in FindResultInEquation calss,
         * next left hand side starts the constant calculation in MathmaticalCalculation calss.
         */
        public static void EquationPrecessing(string userInput, string[] args)
        {
            MathematicalCalculation mathCal = new MathematicalCalculation();
            CalculateX calculateX           = new CalculateX();
            ArrayList  leftList             = new ArrayList();
            ArrayList  rightList            = new ArrayList();
            string     leftInput            = userInput.Split('=')[0];
            string     rightInput           = userInput.Split('=')[1];
            int        equalIndex           = Array.IndexOf(args, "=");

            if (leftInput.ToString().ToLower().Contains('x'))
            {
                ArrayList constantResult = mathCal.Calculation(rightInput);

                //To gain the length of left hand side quation stap at equal operator.
                for (int variableLoopInLeftList = 0; variableLoopInLeftList < equalIndex; variableLoopInLeftList++)
                {
                    leftList.Add(args[variableLoopInLeftList]);
                }

                int xResultFromLeft = calculateX.FindResultInEquation(leftList, constantResult);
                Console.WriteLine("X = " + xResultFromLeft);
            }
            else if (rightInput.ToString().ToLower().Contains('x'))
            {
                ArrayList constantResult = mathCal.Calculation(leftInput);

                //To gain the length of right hand side quation from equal operator.
                int rightElementsLength = args.Length - equalIndex;

                //Since the initial index 0 is '=', skip it and gain the whole right hand side equation.
                for (int variableLoopInRightList = 1; variableLoopInRightList < rightElementsLength; variableLoopInRightList++)
                {
                    rightList.Add(args[variableLoopInRightList + equalIndex]);
                }

                int xResultFromRight = calculateX.FindResultInEquation(rightList, constantResult);
                Console.WriteLine("X = " + xResultFromRight);
            }
        }
        public int FindResultInEquation(ArrayList xUnsolvedList, ArrayList constantResultList)
        {
            MathematicalCalculation mathCal = new MathematicalCalculation();
            int constantNumber = 1;
            int resultX        = new int();

            //Detected the constant number that is next to X and would contain negative number.
            bool hasMinusSymbol = false;

            //Generate a new constants mathmatics equation without X and convert appropriate arthmetics from the left hand side.
            //Also, remove the elements once finished the convertion.
            for (int indexOfxUnsolvedList = 0; indexOfxUnsolvedList < xUnsolvedList.Count; indexOfxUnsolvedList++)
            {
                int    constantValueNearToX;
                string indexValue = xUnsolvedList[indexOfxUnsolvedList].ToString();

                for (int nextIndexOfxUnsolvedList = indexOfxUnsolvedList + 1; nextIndexOfxUnsolvedList < xUnsolvedList.Count; nextIndexOfxUnsolvedList++)
                {
                    string nextNlementValue = xUnsolvedList[nextIndexOfxUnsolvedList].ToString().ToLower();

                    if (indexValue.Contains('+') || indexValue.Contains('-'))
                    {
                        if (indexValue.Equals("+") && !nextNlementValue.Contains('x'))
                        {
                            constantResultList.Add('-');
                            xUnsolvedList.RemoveAt(indexOfxUnsolvedList);
                        }

                        if (indexValue.Equals("-") && !nextNlementValue.Contains('x'))
                        {
                            constantResultList.Add('+');
                            xUnsolvedList.RemoveAt(indexOfxUnsolvedList);
                        }
                    }


                    if (indexValue.Contains('*') || indexValue.Contains('/'))
                    {
                        if (indexValue.Equals("*"))
                        {
                            constantResultList.Add('/');
                            xUnsolvedList.RemoveAt(indexOfxUnsolvedList);
                        }

                        if (indexValue.Equals("/"))
                        {
                            constantResultList.Add('*');
                            xUnsolvedList.RemoveAt(indexOfxUnsolvedList);
                        }
                    }

                    //If the left hand side constant number is positive and is the first element,
                    //then move to the right hand side with minus.
                    if (int.TryParse(xUnsolvedList[indexOfxUnsolvedList].ToString(), out constantValueNearToX))
                    {
                        if (xUnsolvedList.IndexOf(indexValue) == 0)
                        {
                            constantResultList.Add('-');
                        }

                        constantResultList.Add(constantValueNearToX);
                        xUnsolvedList.RemoveAt(indexOfxUnsolvedList);
                    }
                }
            }

            //Find out the constant number a where close the X by substring method and assume the minus represents the negative value of constant.
            for (int indexOfxUnsolvedList = 0; indexOfxUnsolvedList < xUnsolvedList.Count; indexOfxUnsolvedList++)
            {
                string indexValue = xUnsolvedList[indexOfxUnsolvedList].ToString().ToLower();

                if (indexValue.Contains('-'))
                {
                    hasMinusSymbol = true;
                }

                if (indexValue.Contains("x") && xUnsolvedList[indexOfxUnsolvedList].ToString().Length > 1)
                {
                    int    xElementValue = xUnsolvedList[indexOfxUnsolvedList].ToString().ToLower().IndexOf("x");
                    string constantValue = xUnsolvedList[indexOfxUnsolvedList].ToString().Substring(0, xElementValue);
                    constantNumber = Convert.ToInt32(constantValue);
                }
            }

            if (hasMinusSymbol == true)
            {
                constantNumber = constantNumber * -1;
            }

            string    finalConstantResult     = string.Join("", constantResultList.ToArray());
            ArrayList finalConstantResultList = mathCal.Calculation(finalConstantResult);

            //Handle the error if the result is out of range or invalid input.
            try
            {
                int finalConstantValue = Convert.ToInt32(finalConstantResultList[0]);
                resultX = finalConstantValue / constantNumber;
            }
            catch (FormatException)
            {
                Console.WriteLine("Error: invalid input");
            }
            catch (OverflowException)
            {
                Console.WriteLine("Error: out of integer range");
            }

            return(resultX);
        }