Advertisement
ntamas

Házi feladat dec. 10.

Dec 9th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace hazifeladat
  7. {
  8.   class Program
  9.   {
  10.     static void elemkeres(int[,] tomb, uint sorsz, uint oszlopsz, int bekert)
  11.     {
  12.       bool talal = false;
  13.       int j, i = 0;
  14.       for (j = 0; j < sorsz && !talal; j++)
  15.       {
  16.         for (i = 0; i < oszlopsz && !talal; i++)
  17.         {
  18.           if (tomb[j, i] == bekert)
  19.           {
  20.             talal = true;
  21.           }
  22.           else
  23.           {
  24.             talal = false;
  25.           }
  26.         }
  27.       }
  28.       if (talal)
  29.       {
  30.         Console.WriteLine("Sorindex: {0}, Oszlopindex: {1}", j, i);
  31.       }
  32.       else
  33.       {
  34.         Console.WriteLine("Nem található az elem.");
  35.       }
  36.     }
  37.     static void eltolas(int[,] tomb, uint sorsz, uint oszlopsz, bool sortolas, bool oszloptolas, uint tolasszam)
  38.     {
  39.       int elem = 0;
  40.       if (sortolas)
  41.       {
  42.         tolasszam %= sorsz;
  43.       }
  44.       else if (oszloptolas)
  45.       {
  46.         tolasszam %= oszlopsz;
  47.       }
  48.       if (tolasszam != 0)
  49.       {
  50.         for (int k = 0; k < tolasszam; k++ )
  51.         {
  52.           for (int j = 0; j < sorsz; j++)
  53.           {
  54.             for (int i = 0; i < oszlopsz; i++)
  55.             {
  56.               if (sortolas)
  57.               {
  58.                 elem = tomb[0, i];
  59.                 tomb[0, i] = tomb[j, i];
  60.                 tomb[j, i] = elem;
  61.               }
  62.               else if (oszloptolas)
  63.               {
  64.                 elem = tomb[j, 0];
  65.                 tomb[j, 0] = tomb[j, i];
  66.                 tomb[j, i] = elem;
  67.               }
  68.             }
  69.           }
  70.         }
  71.       }
  72.       else
  73.       {
  74.         Console.WriteLine("Egyetlen sort/oszlopot sem toltunk sehova.");
  75.       }
  76.     }
  77.     static void kiiras(int[,] tomb, uint sorsz, uint oszlopsz)
  78.     {
  79.       for (int j = 0; j < sorsz; j++)
  80.       {
  81.         for (int i = 0; i < oszlopsz; i++)
  82.         {
  83.           Console.Write("{0}  ", tomb[j, i]);
  84.         }
  85.         Console.WriteLine();
  86.       }
  87.     }
  88.  
  89.     static void tombfeltolt(int[,] tomb, uint sorsz, uint oszlopsz, Random vel, uint szjegy, bool kul)
  90.     {
  91.       int tombhatar1 = Convert.ToInt32(Math.Pow(10, szjegy - 1));
  92.       int tombhatar2 = Convert.ToInt32(Math.Pow(10, szjegy));
  93.       int lelemszam = tombhatar2 - tombhatar1;
  94.       if (kul == true)
  95.       {
  96.         if (lelemszam < (sorsz * oszlopsz))
  97.         {
  98.           Console.WriteLine("A tömböt nem lehet feltölteni, mert több elemet tartalmazhat, mint ahány különböző elem a megadott határokon belül generálható.");
  99.         }
  100.         else
  101.         {
  102.           bool talal;
  103.           int i, j, generalt = 0;
  104.           tomb[0, 0] = vel.Next(tombhatar1, tombhatar2);
  105.           for (j = 0; j < sorsz; j++)
  106.           {
  107.             if (j == 0)
  108.             {
  109.               i = 1;
  110.             }
  111.             else
  112.             {
  113.               i = 0;
  114.             }
  115.             for (; i < oszlopsz; i++)
  116.             {
  117.               do
  118.               {
  119.                 talal = false;
  120.                 generalt = vel.Next(tombhatar1, tombhatar2);
  121.                 for (int l = 0; l < sorsz; l++)
  122.                 {
  123.                   for (int k = 0; k < oszlopsz; k++)
  124.                   {
  125.                     if (tomb[l, k] == generalt)
  126.                     {
  127.                       talal = true;
  128.                     }
  129.                   }
  130.                 }
  131.               } while (talal);
  132.               tomb[j, i] = generalt;
  133.             }
  134.           }
  135.         }
  136.       }
  137.       else
  138.       {
  139.         for (int j = 0; j < sorsz; j++)
  140.         {
  141.           for (int i = 0; i < oszlopsz; i++)
  142.           {
  143.             tomb[j, i] = vel.Next(tombhatar1, tombhatar2);
  144.           }
  145.         }
  146.       }
  147.     }
  148.     static void Main(string[] args)
  149.     {
  150.       Random vel = new Random();
  151.       Console.Write("Sorok száma: ");
  152.       uint sorsz = Convert.ToUInt32(Console.ReadLine());
  153.       Console.Write("Oszlop száma: ");
  154.       uint oszlopsz = Convert.ToUInt32(Console.ReadLine());
  155.       int[,] tomb = new int[sorsz, oszlopsz];
  156.       tombfeltolt(tomb, sorsz, oszlopsz, vel, 3, false);
  157.       kiiras(tomb, sorsz, oszlopsz);
  158.       Console.Write("A bekért szám: ");
  159.       int bekert = Convert.ToInt32(Console.ReadLine());
  160.       elemkeres(tomb, sorsz, oszlopsz, bekert);
  161.       Console.Write("Sortoláshoz nyomjon s betűt, oszloptoláshoz o betűt! (s/o) ");
  162.       bool sortolas = false;
  163.       bool oszloptolas = false;
  164.       string a = Console.ReadLine();
  165.       if (Convert.ToChar(a) == 's')
  166.       {
  167.         sortolas = true;
  168.         Console.Write("Adja meg, hogy hány sorral szeretné eltolni a sorokat: ");
  169.       }
  170.       else if (Convert.ToChar(a) == 'o')
  171.       {
  172.         oszloptolas = true;
  173.         Console.Write("Adja meg, hogy hány oszloppal szeretné eltolni az oszlopokat: ");
  174.       }
  175.       uint tolasszam = Convert.ToUInt16(Console.ReadLine());
  176.       if(sortolas || oszloptolas)
  177.       {
  178.         eltolas(tomb, sorsz, oszlopsz, sortolas, oszloptolas, tolasszam);  
  179.       }
  180.       kiiras(tomb, sorsz, oszlopsz);
  181.       Console.ReadKey();
  182.     }
  183.   }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement