Example #1
0
        static void My_strings()
        {
            Console.Clear();
            //Declare int and string variables for decimal and binary presentations
            int[] UserNumbers = new int[3] {
                0, 0, 0
            };
            string[] UserStrings = new string[2] {
                "", ""
            };
            string UserAnswer;

            string[] UserAnswerArr;
            int      parseResult;

            while (true)
            {
                //Implement two positive integer variables input
                UserAnswer    = ConsoleFunc.AskUser(UserNumbers[0] == 0 ? "Please,  enter 1 or 2 numbers" : "Please,  enter 1 more number");
                UserAnswerArr = UserAnswer.Split();
                if (UserAnswerArr.Length == 0)
                {
                    continue;
                }

                for (int i = 0; i <= Math.Min(UserNumbers[0], UserAnswerArr.Length - 1); i++)
                {
                    if (int.TryParse(UserAnswerArr[i], out parseResult) && parseResult == 0)
                    {
                        continue;
                    }
                    UserNumbers[++UserNumbers[0]] = parseResult;
                }

                if (UserNumbers[0] != 2)
                {
                    continue;
                }

                //To present each of them in the form of unary string use for loop
                for (int i = 0; i < UserStrings.Length; i++)
                {
                    for (int j = 0; j < UserNumbers[i + 1]; j++)
                    {
                        UserStrings[i] += "1";
                    }
                }

                Console.WriteLine($"Unary string for number {UserNumbers[1]} is {UserStrings[0]}");
                Console.WriteLine($"Unary string for number {UserNumbers[2]} is {UserStrings[1]}");

                //Use concatenation of these two strings
                //Note it is necessary to use some symbol ( for example “#”) to separate
                string separate = "#";

                //Realize the  algorithm for replacing '1#1' to '#' by using the for loop
                for (int i = 0; i <= Math.Min(UserNumbers[1], UserNumbers[2]) + 1; i++)
                {
                    //Delete the '#' from algorithm result
                    if (UserNumbers[1] - i < 0 && UserNumbers[2] - i < 0)
                    {
                        separate = "";
                    }                                                                        //Check the numbers on the equality 0
                    else if (UserNumbers[1] - i < 0 || UserNumbers[2] - i < 0)
                    {
                        separate = "1";
                    }
                    //Output the result
                    Console.WriteLine($"{((UserNumbers[1] - i > 0) ? new String('1', UserNumbers[1] - i): "")}" +
                                      $"{separate}" +
                                      $"{((UserNumbers[2] - i > 0) ? new String('1', UserNumbers[2] - i) : "")}");
                }

                break;
            }
        }
Example #2
0
        public override void Run()
        {
            const int UserNumbersCount = 2;

            Console.Clear();
            ShowTheCondition();
            string UserAnswer;

            string[]          UserAnswerArr;
            double?[]         UserNumbers;
            CalculatorVariant SelectedVariant;

            while (true)
            {
                UserAnswer = AskUser("Please,  type numbers by step (0 to repeat the rules)");
                if (UserAnswer.ToLower() == "q")
                {
                    return;
                }
                else if (!Enum.TryParse(UserAnswer, out SelectedVariant) || !Enum.IsDefined(typeof(CalculatorVariant), SelectedVariant))
                {
                    continue;
                }

                if (SelectedVariant == CalculatorVariant.Repeat)
                {
                    ShowTheCondition();
                    continue;
                }

                UserNumbers = new double?[UserNumbersCount + 1] {
                    null, null, null
                };                                                                    // +1 for result

                int j = 0;

                while (true)
                {
                    UserAnswer    = AskUser(j == 0 ? "Please,  enter 1 or 2 numbers" : "Please,  enter 1 more number");
                    UserAnswerArr = UserAnswer.Split();
                    if (UserAnswerArr.Count() == 0)
                    {
                        continue;
                    }

                    for (int i = 0; i <= Math.Min(UserNumbersCount, UserAnswerArr.Count() - 1); i++)
                    {
                        double parseResult;
                        Double.TryParse(UserAnswerArr[i], out parseResult);

                        for (j = 0; j <= UserNumbersCount - 1; j++)
                        {
                            if (UserNumbers[j] == null)
                            {
                                UserNumbers[j] = parseResult;
                                break;
                            }
                        }
                        if (j == UserNumbersCount - 1)
                        {
                            break;
                        }
                    }

                    if (j != UserNumbersCount - 1)
                    {
                        continue;
                    }
                    else if (SelectedVariant == CalculatorVariant.Divide && UserNumbers[1] == 0)
                    {
                        ShowError("You can't Divide to 0!!!");
                        double?reserv = UserNumbers[0];
                        UserNumbers = new double?[UserNumbersCount + 1] {
                            null, null, null
                        };
                        UserNumbers[0] = reserv;
                        continue;
                    }

                    switch (SelectedVariant)
                    {
                    case CalculatorVariant.Addition:
                        UserNumbers[2] = UserNumbers[0] + UserNumbers[1];
                        break;

                    case CalculatorVariant.Divide:
                        UserNumbers[2] = UserNumbers[0] / UserNumbers[1];
                        break;

                    case CalculatorVariant.Multiplication:
                        UserNumbers[2] = UserNumbers[0] * UserNumbers[1];
                        break;

                    case CalculatorVariant.Subtraction:
                        UserNumbers[2] = UserNumbers[0] - UserNumbers[1];
                        break;

                    case CalculatorVariant.Exponentiation:
                        UserNumbers[2] = Math.Pow((double)UserNumbers[0], (double)UserNumbers[1]);
                        break;
                    }
                    ShowResult(UserNumbers[0] + " " + GetSymbol(SelectedVariant) + " " + UserNumbers[1] + " = " + UserNumbers[2]);
                    ShowTheCondition();
                    break;
                }
            }
        }