Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp1
- {
- class Program
- {
- static string DecToBin(int liczba)
- {
- string Bin="";
- int val;
- int decVal;
- string a = "";
- decVal = liczba;
- while (decVal >= 1)
- {
- val = decVal / 2;
- a += (decVal % 2).ToString(); ;
- decVal = val;
- }
- int dlugosc = a.Length;
- for (int k = a.Length - 1; k >= 0; k--)
- {
- Bin = Bin + a[k];
- }
- return Bin;
- }
- static string Rotate(string k,int count)
- {
- string z = "";
- for(int i = 0; i < count; i++)
- {
- int dlugosc = k.Length;
- char tmp = k[dlugosc-1];
- z += tmp;
- dlugosc--;
- while (dlugosc > 0)
- {
- z += k[dlugosc-1];
- dlugosc--;
- }
- }
- return z;
- }
- //Once
- static string RotateRight(string k)
- {
- string z = "";
- int dlugosc = k.Length;
- char tmp = k[dlugosc - 1];
- z += tmp;
- dlugosc--;
- int w = 0;
- while (dlugosc > 0)
- {
- z += k[w];
- dlugosc--;
- w++;
- }
- return z;
- }
- static string RotateRightCounts(string k,int count)
- {
- string b = k;
- for(int i = 0; i < count; i++)
- {
- b = RotateRight(b);
- }
- return b;
- }
- static string RotateLeft(string k)
- {
- string z = "";
- int dlugosc = k.Length;
- int w = 0;
- while (w<dlugosc-1)
- {
- z += k[w + 1];
- w++;
- }
- z += k[0];
- return z;
- }
- static string RotateLeftCounts(string k, int count)
- {
- string b = k;
- for (int i = 0; i < count; i++)
- {
- b = RotateLeft(b);
- }
- return b;
- }
- static string RotateLogic(string k, int z,int count)
- {
- if (z == 0)
- {
- return RotateRightCounts(k, count);
- }
- else
- {
- if(z == 1)
- {
- return RotateLeftCounts(k, count);
- }
- else
- {
- return "Error";
- }
- }
- }
- static int potegowanie(int a,int b)
- {
- int wynik = 1;
- while (b > 0)
- {
- wynik *= a;
- b--;
- }
- return wynik;
- }
- static int BinToDec(string k)
- {
- int wartosc = 0;
- int dlugosc = k.Length;
- int b = 0;
- while (dlugosc > 0)
- {
- if (k[dlugosc-1].Equals('1')) {
- wartosc += potegowanie(2, b);
- }
- else
- {
- wartosc += 0;
- }
- b++;
- dlugosc--;
- }
- return wartosc;
- }
- static void Main(string[] args)
- {
- int[] tab = new int[] { 0, 1, 2, 3, 4 };
- string[] BinTab = new string[tab.Length] ;
- // Konwersja elementów Dec to Bin, dodatkowo zmiana długości tablicy w celu dodania nowych elementów
- for (int i =0;i<tab.Length; i++)
- {
- Console.Write(tab[i] + " po konwersji: " + DecToBin(tab[i]));
- Console.WriteLine();
- Array.Resize(ref BinTab, BinTab.Length + 1);
- BinTab[BinTab.Length-1] = DecToBin(tab[i]);
- }
- string k = "";
- //String z ciągiem 0 i 1
- for(int i = 0; i < BinTab.Length; i++)
- {
- k += BinTab[i];
- }
- Console.WriteLine(k);
- Console.WriteLine("Rotacja");
- Console.WriteLine(Rotate(k, 1));
- Console.WriteLine("rotacja w prawo o jedną pozycję");
- Console.WriteLine(RotateRight(k));
- Console.WriteLine("rotacja w prawo o kilka pozycji");
- Console.WriteLine(RotateRightCounts(k,3));
- Console.WriteLine("rotacja w lewo ");
- Console.WriteLine(RotateLeft(k));
- Console.WriteLine("rotacja w lewo o kilka pozycji");
- Console.WriteLine(RotateLeftCounts(k,3));
- //RotateLogic(string k, lewo/prawo (0 lub 1) , ilosc)
- Console.WriteLine("rotacja z logiką lewo/prawo");
- Console.WriteLine(RotateLogic(k, 0, 3));
- Console.WriteLine("Konwersja z Bin do Dec");
- Console.WriteLine(BinToDec(RotateLogic(k, 0, 3)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement