Пример #1
0
        private static void CalculateOperation(string[] args)
        {
            OperationType operationType = GetOperationType(args[0]);

            if (operationType == OperationType.Unknown)
            {
                Console.WriteLine("Error parsing the operation (first) parameter. Expected one of these: [-a,-s,-m,-d]\nUse \"help\" as first parameter for more info.");
                return;
            }

            InputParameters inputParams = ParseArguments(args);

            Calculator calc = new Calculator(inputParams);

            calc.RunOperation(operationType);
        }
Пример #2
0
        private List <int> ProcessOperationParameters(string logOpStr, string scrubbedOpParams, InputParameters inputParams)
        {
            List <int> opValueList = new List <int>();

            string[] rawValues  = scrubbedOpParams.Split(',');
            int      upperBound = inputParams.UpperBound.GetValueOrDefault(0);

            if (rawValues == null || rawValues.Length < 2)
            {
                CalcLog.Append($"Invalid parameter for operation. Parameter: {scrubbedOpParams}");
                return(null);
            }

            int           cnt         = 0;
            bool          foundNegNum = false;
            StringBuilder sbNegNum    = new StringBuilder();

            foreach (string val in rawValues)
            {
                if (cnt > 0)
                {
                    CalcLog.Append(logOpStr);
                }

                if (Int32.TryParse(val, out int adInt))
                {
                    // check for negative number
                    if (adInt < 0)
                    {
                        if (inputParams.AllowNegativeNumbers)
                        {
                            opValueList.Add(adInt);
                            CalcLog.Append($"{adInt}");
                        }
                        else
                        {
                            // save the negative number for printing in the exception
                            if (sbNegNum.Length > 0)
                            {
                                sbNegNum.Append(",");
                            }

                            sbNegNum.Append(adInt);
                            foundNegNum = true;
                        }
                    }
                    // set upper bound if one was chosen
                    else if (upperBound > 1)
                    {
                        if (adInt < upperBound)
                        {
                            opValueList.Add(adInt);
                            CalcLog.Append($"{adInt}");
                        }
                        else
                        {
                            CalcLog.Append("0");
                        }
                    }
                    else
                    {
                        opValueList.Add(adInt);
                        CalcLog.Append($"{adInt}");
                    }
                }
                else
                {
                    opValueList.Add(0);
                    CalcLog.Append("0");
                }

                cnt++;
            }

            if (foundNegNum)
            {
                CalcLog.Append("Negative values found: " + sbNegNum);
                throw new ArgumentOutOfRangeException("Negative values are not allowed! Set the proper parameter to allow negatives values.");
            }

            return(opValueList);
        }
Пример #3
0
 public Calculator(InputParameters inputParams)
 {
     InputParameters = inputParams;
     CalcLog         = new StringBuilder();
 }