Advertisement
RynkunPokemon

Zadanie1

Jun 12th, 2022
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static string DecToBin(int liczba)
  8.         {
  9.             string Bin="";
  10.             int val;
  11.             int decVal;
  12.            
  13.            
  14.                 string a = "";
  15.                 decVal = liczba;
  16.                 while (decVal >= 1)
  17.                 {
  18.                     val = decVal / 2;
  19.                     a += (decVal % 2).ToString(); ;
  20.                     decVal = val;
  21.                 }
  22.                 int dlugosc = a.Length;
  23.  
  24.  
  25.  
  26.             for (int k = a.Length - 1; k >= 0; k--)
  27.                 {
  28.                     Bin = Bin + a[k];
  29.                 }
  30.                
  31.                
  32.            
  33.  
  34.  
  35.             return Bin;
  36.         }
  37.  
  38.         static string Rotate(string k,int count)
  39.         {
  40.             string z = "";
  41.            
  42.             for(int i = 0; i < count; i++)
  43.             {
  44.                 int dlugosc = k.Length;
  45.                 char tmp = k[dlugosc-1];
  46.                 z += tmp;
  47.                 dlugosc--;
  48.                 while (dlugosc > 0)
  49.                 {
  50.                     z += k[dlugosc-1];
  51.                     dlugosc--;
  52.                 }
  53.             }
  54.  
  55.  
  56.  
  57.             return z;
  58.         }
  59.         //Once
  60.         static string RotateRight(string k)
  61.         {
  62.                 string z = "";
  63.                 int dlugosc = k.Length;
  64.                 char tmp = k[dlugosc - 1];
  65.                 z += tmp;
  66.                 dlugosc--;
  67.                 int w = 0;
  68.                 while (dlugosc > 0)
  69.                 {
  70.                     z += k[w];
  71.                     dlugosc--;
  72.                     w++;
  73.                 }
  74.            
  75.             return z;
  76.         }
  77.  
  78.         static string RotateRightCounts(string k,int count)
  79.         {
  80.             string b = k;
  81.             for(int i = 0; i < count; i++)
  82.             {
  83.                 b = RotateRight(b);
  84.             }
  85.             return b;
  86.         }
  87.  
  88.         static string RotateLeft(string k)
  89.         {
  90.             string z = "";
  91.             int dlugosc = k.Length;
  92.             int w = 0;
  93.             while (w<dlugosc-1)
  94.             {
  95.                 z += k[w + 1];
  96.                 w++;
  97.             }
  98.             z += k[0];
  99.  
  100.             return z;
  101.         }
  102.  
  103.         static string RotateLeftCounts(string k, int count)
  104.         {
  105.             string b = k;
  106.             for (int i = 0; i < count; i++)
  107.             {
  108.                 b = RotateLeft(b);
  109.             }
  110.             return b;
  111.         }
  112.  
  113.         static string RotateLogic(string k, int z,int count)
  114.         {
  115.             if (z == 0)
  116.             {
  117.                 return RotateRightCounts(k, count);
  118.             }
  119.             else
  120.             {
  121.                 if(z == 1)
  122.                 {
  123.                     return RotateLeftCounts(k, count);
  124.                 }
  125.                 else
  126.                 {
  127.                     return "Error";
  128.                 }
  129.             }
  130.  
  131.  
  132.         }
  133.  
  134.         static int potegowanie(int a,int b)
  135.         {
  136.             int wynik = 1;
  137.  
  138.             while (b > 0)
  139.             {
  140.                 wynik *= a;
  141.                 b--;
  142.             }
  143.  
  144.             return wynik;
  145.         }
  146.  
  147.         static int BinToDec(string k)
  148.         {
  149.             int wartosc = 0;
  150.             int dlugosc = k.Length;
  151.             int b = 0;
  152.             while (dlugosc > 0)
  153.             {
  154.                
  155.                 if (k[dlugosc-1].Equals('1')) {
  156.                 wartosc += potegowanie(2, b);
  157.                 }
  158.                 else
  159.                 {
  160.                     wartosc += 0;
  161.                 }
  162.                 b++;
  163.                 dlugosc--;
  164.                
  165.                
  166.             }
  167.            
  168.  
  169.             return wartosc;
  170.         }
  171.  
  172.  
  173.         static void Main(string[] args)
  174.         {
  175.             int[] tab = new int[] { 0, 1, 2, 3, 4 };
  176.             string[] BinTab = new string[tab.Length] ;
  177.  
  178.            
  179.            
  180.             // Konwersja elementów Dec to Bin, dodatkowo zmiana długości tablicy w  celu dodania nowych elementów
  181.             for (int i =0;i<tab.Length; i++)
  182.             {
  183.                 Console.Write(tab[i] + " po konwersji: " + DecToBin(tab[i]));
  184.                 Console.WriteLine();
  185.                 Array.Resize(ref BinTab, BinTab.Length + 1);
  186.                 BinTab[BinTab.Length-1] = DecToBin(tab[i]);
  187.             }
  188.  
  189.             string k = "";
  190.  
  191.             //String z ciągiem 0 i 1
  192.             for(int i = 0; i < BinTab.Length; i++)
  193.             {
  194.                 k += BinTab[i];
  195.             }
  196.  
  197.  
  198.             Console.WriteLine(k);
  199.  
  200.             Console.WriteLine("Rotacja");
  201.             Console.WriteLine(Rotate(k, 1));
  202.  
  203.  
  204.             Console.WriteLine("rotacja w prawo o jedną pozycję");
  205.             Console.WriteLine(RotateRight(k));
  206.  
  207.             Console.WriteLine("rotacja w prawo o kilka pozycji");
  208.             Console.WriteLine(RotateRightCounts(k,3));
  209.  
  210.  
  211.             Console.WriteLine("rotacja w lewo ");
  212.             Console.WriteLine(RotateLeft(k));
  213.  
  214.             Console.WriteLine("rotacja w lewo o kilka pozycji");
  215.             Console.WriteLine(RotateLeftCounts(k,3));
  216.  
  217.  
  218.             //RotateLogic(string k, lewo/prawo (0 lub 1) , ilosc)
  219.             Console.WriteLine("rotacja z logiką lewo/prawo");
  220.             Console.WriteLine(RotateLogic(k, 0, 3));
  221.  
  222.            
  223.             Console.WriteLine("Konwersja z Bin do Dec");
  224.             Console.WriteLine(BinToDec(RotateLogic(k, 0, 3)));
  225.  
  226.  
  227.            
  228.         }
  229.     }
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement