예제 #1
0
 private void zistiPrvocisla_Click(object sender, EventArgs e)
 {
     try
     {
         Prvocisla.Clear();
         for (int i = 0; i < pole.Length; i++)
         {
             if (pole[i] < 2)
             {
                 continue;
             }
             bool   prvocislo = true;
             double root      = Math.Sqrt(Convert.ToDouble(pole[i]));
             for (int j = 2; j < root + 1; j++)
             {
                 if ((pole[i] % j == 0) && (pole[i] != j))
                 {
                     prvocislo = false;
                     break;
                 }
             }
             if (prvocislo == true)
             {
                 Prvocisla.Text += pole[i] + ", ";
             }
         }
     } catch { MessageBox.Show("Pole nie je vygenerovane", "Chyba poľa", MessageBoxButtons.OK, MessageBoxIcon.Error); }
 }
예제 #2
0
        public void JePrvocisloTest4()
        {
            int  x        = 651547;
            bool expected = false;
            bool result   = Prvocisla.JePrvocislo(x);

            Assert.AreEqual(expected, result);
        }
예제 #3
0
        public void JePrvocisloTest3()
        {
            int  x        = 2;
            bool expected = true;
            bool result   = Prvocisla.JePrvocislo(x);

            Assert.AreEqual(expected, result);
        }
예제 #4
0
 private void vytvorPole_Click(object sender, EventArgs e)
 {
     try
     {
         int velkost = Convert.ToInt32(Velkost.Text);
         int min     = Convert.ToInt32(Min.Text);
         int max     = Convert.ToInt32(Max.Text);
         if (velkost < 50)
         {
             MessageBox.Show("Minimalna velkost pola je 50", "Nespravny vstup", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             return;
         }
         if (min < 0)
         {
             MessageBox.Show("Prvočisla môzu byt iba kladné", "Nespravny vstup", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             return;
         }
         if (max - min < 1000)
         {
             MessageBox.Show("Rozsah má byť aspoň 1000 čísel", "Nespravny vstup", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             return;
         }
         pole = new int[velkost];
         Random rand = new Random();
         Vypis.Clear();
         Prvocisla.Clear();
         for (int i = 0; i < velkost; i++)
         {
             pole[i]     = rand.Next(min, max);
             Vypis.Text += Convert.ToString(pole[i]) + ", ";
         }
     }
     catch
     {
         MessageBox.Show("Vstup zadavajte ako cele čísla", "Nespravny vstup", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
예제 #5
0
        static void Main(string[] args)
        {
            //Console.OutputEncoding = Encoding.UTF8;
            //Console.ForegroundColor = ConsoleColor.Yellow;
            //Console.WriteLine("Vítejte v ultra super kalkulačce prvočísel!", Console.ForegroundColor);
            //Console.WriteLine("===========================================\n", Console.ForegroundColor);

            //Console.ForegroundColor = ConsoleColor.White;

            //int maximum = LoadNumber();
            int maximum;

            if (!int.TryParse(args[0], out maximum))
            {
                Console.WriteLine("ERROR: Chybný parametr");
                return;
            }

            int[] polePrvocisel = new int[1];
            try
            {
                polePrvocisel = Prvocisla.VratPrvocislaDo(maximum);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Console.Error.WriteLine(ex.Message);
            }

            Array.Sort(polePrvocisel);

            //Console.ForegroundColor = ConsoleColor.Yellow;
            //Console.WriteLine("\nVýsledek:", Console.ForegroundColor);

            //Console.ForegroundColor = ConsoleColor.White;

            int maxDigitCount = ($"{polePrvocisel[polePrvocisel.Length - 1]}").Length;

            int tmp          = 1;
            int pocetNaRadek = Settings.Default.PocetNaRadek;

            StringBuilder sb = new StringBuilder(10000);

            //Console.Write("\t");
            for (int i = 0; i < polePrvocisel.Length; i++)
            {
                string formattedValue = $"{polePrvocisel[i]}".PadLeft(maxDigitCount, ' ');

                if ((tmp % pocetNaRadek == 0) || (i + 1 == polePrvocisel.Length))
                {
                    //Console.Write($"{formattedValue}\n\t");
                    sb.Append($"{formattedValue}\n");
                }
                else
                {
                    //Console.Write($"{formattedValue}, ");
                    sb.Append($"{formattedValue}, ");
                }

                tmp++;
            }

            //Console.ReadKey();

            if (string.IsNullOrWhiteSpace(args[1]))
            {
                Console.Error.WriteLine("Chybný název souboru! Název souboru je prázný nebo obsahuje pouze bílé znaky.");
                return;
            }

            try
            {
                File.WriteAllText(args[1], sb.ToString());
            }
            catch (IOException ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }