Example #1
0
        // Main class

        static void Main(string[] args)
        {
            // print output based on invalidity tests

            if (args.Length != 0)
            {
                string input = args[0];
                input = input.Trim();
                List <string> inputList = ProcessInput.splitInput(input);

                if (CheckInput.isInvalid(input, inputList) || args.Length != 1)
                {
                    Console.WriteLine("Invalid Input");
                }
                else if (CheckInput.divZero(inputList))
                {
                    Console.WriteLine("Division by zero");
                }
                else if (!CheckInput.isInt(Operations.getFinalAns(inputList)))
                {
                    Console.WriteLine("Out of Range");
                }
                else
                {
                    Console.WriteLine("Result = " + Operations.getFinalAns(inputList));
                }
            }
            else
            {
                Console.WriteLine("Invalid Input");
            }
            Console.ReadLine();
        }
        // class contains all mathematical operation-related functions

        public static string getFinalAns(List <string> input)
        {
            // loop through final formatted list
            // pass numbers and operator into calculate function

            List <string> formattedList = ProcessInput.formatInput(input);

            for (int i = 0; i < formattedList.Count; i++)
            {
                if (CheckInput.timesDivOrMod(formattedList[i]))
                {
                    formattedList = calculate(formattedList, i);
                    --i;
                }
            }

            for (int i = 0; i < formattedList.Count; i++)
            {
                if (CheckInput.plusOrMinus(formattedList[i]))
                {
                    formattedList = calculate(formattedList, i);
                    --i;
                }
            }
            return("" + long.Parse(formattedList[0]));
        }
Example #3
0
        private static bool hasNonInt(string input)
        {
            // check whether there is a non-integer value in input

            List <string> numList = ProcessInput.toNumList(input);

            foreach (string value in numList)
            {
                if (!(isInt(value)))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #4
0
        public static bool divZero(List <string> inputList)
        {
            // check whether there is a "division by 0" expression in input

            List <string> formattedList = ProcessInput.formatInput(inputList);

            for (int i = 0; i < formattedList.Count; i++)
            {
                if ((formattedList[i].Equals("/") || formattedList[i].Equals("%")) &&
                    int.Parse(formattedList[i + 1]) == 0)
                {
                    return(true);
                }
            }
            return(false);
        }