Skip to content

Commit

Permalink
#16 - Kód pro Fibonaacciho posloupnost
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasl32 committed Dec 4, 2024
1 parent 0093f84 commit f28fca8
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
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 IS-Projekty/016-app16-Fibonacciho-posloupnost/Program.cs
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;
}
2 changes: 2 additions & 0 deletions IS-Projekty/CodebaseIS.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "000-doucovani", "000-doucov
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "000-BASE", "000-BASE\000-BASE.csproj", "{31732F9C-E266-4F8E-B24A-9A7875A9F024}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "016-app16-Fibonacciho-posloupnost", "016-app16-Fibonacciho-posloupnost\016-app16-Fibonacciho-posloupnost.csproj", "{7247E651-268A-4FFE-9806-0288CD5EA19C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down

0 comments on commit f28fca8

Please sign in to comment.