-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#16 - Kód pro Fibonaacciho posloupnost
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
IS-Projekty/016-app16-Fibonacciho-posloupnost/016-app16-Fibonacciho-posloupnost.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<RootNamespace>_016_app16_Fibonacciho_posloupnost</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
119 changes: 119 additions & 0 deletions
119
IS-Projekty/016-app16-Fibonacciho-posloupnost/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System.Diagnostics; | ||
|
||
Console.Clear(); | ||
Console.WriteLine("********************************"); | ||
Console.WriteLine("******** Lukáš Moravec *********"); | ||
|
||
bool repeat = false; | ||
|
||
do | ||
{ | ||
var count = GetValue<int>("Zadejte žádanou délku Fibonacciho posloupnosti: "); | ||
|
||
Console.WriteLine("\nVýpočet..."); | ||
Stopwatch sw = Stopwatch.StartNew(); | ||
var array = GenerateFibbonacci(count); | ||
sw.Stop(); | ||
Console.WriteLine("Výpočet dokončen za: {0}", sw.Elapsed); | ||
|
||
Console.Write("\nPřejete si vypsat řadu? [y/n]: "); | ||
if (Console.ReadLine() == "y") | ||
PrintArray<ulong>(array); | ||
|
||
Console.ReadKey(); | ||
} | ||
while (repeat); | ||
|
||
|
||
static ulong[] GenerateFibbonacci(int count) | ||
{ | ||
ulong[] output = new ulong[count]; | ||
output[0] = 0; | ||
output[1] = 1; | ||
|
||
for (int i = 2; i < output.Length; i++) | ||
output[i] = output[i - 1] + output[i - 2]; | ||
|
||
return output; | ||
} | ||
|
||
|
||
static T? GetValue<T>(string text) | ||
{ | ||
T? value; | ||
Console.Write(text); | ||
while (true) | ||
{ | ||
try | ||
{ | ||
value = (T?)Convert.ChangeType(Console.ReadLine(), typeof(T)); | ||
return value; | ||
} | ||
catch (Exception) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Red; | ||
Console.WriteLine("Chybně zadaná hodnota!!"); | ||
Console.ResetColor(); | ||
|
||
Console.Write("\nZadejte znovu: "); | ||
} | ||
} | ||
} | ||
|
||
static void PrintArray<T>(T[] array) | ||
{ | ||
for (int i = 0; i < array.Length; i++) | ||
{ | ||
if (i == 0) Console.Write("[{0}; ", array[i]); | ||
else if (i == array.Length - 1) Console.WriteLine("{0}]", array[i]); | ||
else Console.Write("{0}; ", array[i]); | ||
} | ||
} | ||
|
||
static int[] GenerateRandomArray() | ||
{ | ||
(int genCount, int genMin, int genMax) = GetRandomGeneratorConfig(); | ||
return RandomGenerator(genCount, genMin, genMax); | ||
} | ||
|
||
static (int count, int min, int max) GetRandomGeneratorConfig() | ||
{ | ||
bool valid = true; | ||
int count, min, max; | ||
|
||
Console.Write("Zadejte počet čísel k vygenerování N (celé číslo): "); | ||
while (!int.TryParse(Console.ReadLine(), out count)) | ||
Console.Write("Chybně zadané celé číslo!! \nZadejte znovu: "); | ||
|
||
do | ||
{ | ||
if (!valid) | ||
Console.WriteLine("\n!!! Spodní mez musí být menší než horní mez !!!\n"); | ||
|
||
Console.Write("Zadejte dolní mez (celé číslo): "); | ||
while (!int.TryParse(Console.ReadLine(), out min)) | ||
Console.Write("Chybně zadané celé číslo!! \nZadejte znovu: "); | ||
|
||
Console.Write("Zadejte horní mez (celé číslo): "); | ||
while (!int.TryParse(Console.ReadLine(), out max)) | ||
Console.Write("Chybně zadané celé číslo!! \nZadejte znovu: "); | ||
|
||
valid = min < max; | ||
} | ||
while (!valid); | ||
|
||
return (count, min, max); | ||
} | ||
|
||
static int[] RandomGenerator(int count, int min, int max) | ||
{ | ||
int[] output = new int[count]; | ||
|
||
Console.WriteLine("Konfigurace generátoru je počet čísel: {0}, horní mez: {1}, dolní mez {2}", count, max, min); | ||
Random random = new(); | ||
|
||
for (int i = 0; i < output.Length; i++) | ||
output[i] = random.Next(min, max + 1); | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters