public void Run() { int chosenNumber = ServiceClass.InputExerciseNumber(); string choice = $"Exercises.Exercise{chosenNumber}"; System.Runtime.Remoting.ObjectHandle oh; try { oh = Activator.CreateInstance("Exercises", choice); var Ex = (Exercise)oh.Unwrap(); Ex.ExerciseRun(); } catch { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"\nProbably exercise with number {chosenNumber} has not yet been completed." + $"\nLet's try again. Please, enter another number more than 3, but less than {chosenNumber}"); Console.ResetColor(); Run(); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\nFor escape press key \"e\"\n" + "If you want to run one more exercise press any other key"); Console.ResetColor(); if (Console.ReadKey(true).KeyChar != 'e') { Run(); } }
public override void ExerciseRun() { List <int> list = DecomposeNumber(ServiceClass.InputAnyIntegerNumber()); Console.WriteLine("The number was decomposed to digits, which were put in list"); list.ShowList(); }
/// <task_description> /// 5. Modify the previous program such that only multiples of three /// or five are considered in the sum, e.g. 3, 5, 6, 9, 10, 12, 15 for n=17 /// <previous> 4. Write a program that asks the user for a number n and prints /// the sum of the numbers 1 to n</previous> /// </task_description> public override void ExerciseRun() { long sum = 0; int number = ServiceClass.InputPositiveIntegerNumber(); long RestrictedFactorial(int num) { if (num == 1) { return(0); } if (num % 3 == 0 || num % 5 == 0) { sum = num + RestrictedFactorial(num - 1); } else { sum = RestrictedFactorial(num - 1); } return(sum); } for (int i = number; i > 0; i--) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } Console.WriteLine($"sum of numbers from 1 to {number}, such that only multiples of three or five = {RestrictedFactorial(number)}"); }
/// <task_description> /// 6.Write a program that asks the user for a number n and gives them /// the possibility to choose between computing the sum and computing /// the product of 1,…,n. /// </task_description> public override void ExerciseRun() { var choiсe = AskSumOrProduct.Ask(); int number; switch (choiсe) { case "s": Console.WriteLine($"The sum of the numbers from 1 to {number = ServiceClass.InputPositiveIntegerNumber()} = {Sum(number)}"); break; case "p": Console.WriteLine($"The product of the numbers from 1 to {number = ServiceClass.InputPositiveIntegerNumber()} = {Product(number)}"); break; case "e": break; default: { Console.WriteLine("Data you've inputted is incorrect. Let's try again."); ExerciseRun(); } break; } }
public override void ExerciseRun() { Console.WriteLine("This programm will decompose arbitary integer number that you will enter" + "and then will sort it by five various methods"); List <int> list = Exercise214.DecomposeNumber(ServiceClass.InputAnyIntegerNumber()); list.ShowList(); List <int> sortedBySelectionList = SortBySelection(list); Console.Write("\nAfter sortig with Selection sort"); sortedBySelectionList.ShowList(); List <int> sortedByInsertionList = SortByInsertion(list); Console.Write("\nAfter sortig with Insertion sort"); sortedByInsertionList.ShowList(); List <int> sortedByMergeList = SortByMerge(list); Console.Write("\nAfter sortig with Merge sort "); sortedByMergeList.ShowList(); List <int> sortedByStoogeList = new List <int>(list); StoogeSort(sortedByStoogeList); Console.Write("\nAfter sortig with Stooge sort "); sortedByStoogeList.ShowList(); List <int> sortedByQSortList = new List <int>(list); SortByQSort(sortedByQSortList); Console.Write("\nAfter sortig with Quick sort "); sortedByQSortList.ShowList(); }
void StartGame() { for (int i = 0; i < 18; i++) { Console.Write("* "); int delay = 500 - (i * 30) + 20; if (delay < 0) { delay = 0; } System.Threading.Thread.Sleep(delay); } Console.WriteLine("\nI've just thought a number. What the number is?\n" + "Input your guess and press \"Enter\""); var rnd = new Random(); int thoughtNumber = rnd.Next(1, 100); int guessNumber = (int)ServiceClass.InputPositiveIntegerNumber(); int previousGuessNumber; int tryCounter = 1; while (guessNumber != thoughtNumber) { previousGuessNumber = guessNumber; if (guessNumber < thoughtNumber) { Console.WriteLine("Your number is TOO SMALL"); } else { Console.WriteLine("Your number is TOO LARGE"); } guessNumber = (int)ServiceClass.InputPositiveIntegerNumber(); if (previousGuessNumber != guessNumber) { tryCounter++; } } Console.WriteLine("!!!CONGRATULATIONS, YOU WON!!!\n" + $"The number is {thoughtNumber}. You've done it in {tryCounter} tries!\n\n"); System.Threading.Thread.Sleep(3000); Console.WriteLine("Want to play one more time? >>> press SPACE-key\n" + "For escape press any other key\n"); if (Console.ReadKey(true).KeyChar == ' ') { StartGame(); } }
public override void ExerciseRun() { Console.WriteLine("***This programm will decompose any integer number, that" + " you enter, to list of digits in base 10. And then it will be converted between two" + " another bases that you will chose***"); int number = ServiceClass.InputAnyIntegerNumber("Enter any integer number to decompose it to digits in base 10"); DigitList list = new DigitList(number); Console.Write("\nSET first number base to convert number >>>"); byte base1 = (byte)ServiceClass.InputNumberBaseNumber(); list.ChangeBase(base1); list.ShowListOfDigits(); Console.Write("\nSET first number base to convert number >>>"); byte base2 = (byte)ServiceClass.InputNumberBaseNumber(); list.ChangeBase(base2); list.ShowListOfDigits(); }
public override void ExerciseRun() { long sum = 0; int number = ServiceClass.InputPositiveIntegerNumber(); for (int i = number; i > 0; i--) { sum += i; } Console.WriteLine($"sum of numbers from 1 to {number} (calculated by cycle) = {sum}"); long Factorial(int num) { // не могу понять почему не получается перехватить исключение на этой строке try { if (num == 1) { return(1); } sum = num + Factorial(num - 1); return(sum); } catch (StackOverflowException) { Console.WriteLine($"Recursive method of calculating of sum from 1 to {number} failed, supposedly becase of StackOverflow EXEPTION"); return(0); } } try { Console.WriteLine($"sum of numbers from 1 to {number} (calculated by recursion) = {Factorial(number)}"); } catch (StackOverflowException) { Console.WriteLine($"Recursive method of calculating of sum from 1 to {number} failed, supposedly becase of StackOverflow EXEPTION"); } }
public override void ExerciseRun() { Console.WriteLine("\nThis programm will decompose two integer numbers to it's digits and then\n" + "will add, subtract and multiply them in digit-by-digit mode"); Console.WriteLine("Enter the first number"); int firstNumber = ServiceClass.InputAnyIntegerNumber(); int secondNumber = ServiceClass.InputAnyIntegerNumber(); List <int> decomposedFirstNumber = Exercise214.DecomposeNumber(firstNumber); List <int> decomposedSecondNumber = Exercise214.DecomposeNumber(secondNumber); Console.WriteLine(); decomposedFirstNumber.ShowList("decomposedFirstNumber"); decomposedSecondNumber.ShowList("decomposedSecondNumber"); Console.WriteLine(); List <int> SUM = AddTwoNumbersDigits(decomposedFirstNumber, decomposedSecondNumber); SUM.ShowList("RESULT OF ADDITION"); decomposedFirstNumber.ShowList("decomposedFirstNumber"); decomposedSecondNumber.ShowList("decomposedSecondNumber"); Console.WriteLine(); List <int> PROD = MultiplyTwoNumbersDigits(decomposedFirstNumber, decomposedSecondNumber); PROD.ShowList("RESULT OF MULTIPLYING"); decomposedFirstNumber.ShowList("decomposedFirstNumber"); decomposedSecondNumber.ShowList("decomposedSecondNumber"); Console.WriteLine(); List <int> SUBTR = SubtractTwoNumbersDigits(decomposedFirstNumber, decomposedSecondNumber); SUBTR.ShowList("RESULT OF SUBTRACTION"); decomposedFirstNumber.ShowList("decomposedFirstNumber"); decomposedSecondNumber.ShowList("decomposedSecondNumber"); Console.WriteLine(); }