MSzopa

C++ Numeric System

Dec 4th, 2020 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. #pragma region NWD
  8. // Algorytm Euklidesa
  9. unsigned int NWD_Euk(unsigned int a, unsigned int b) {
  10.     while (a != b) {
  11.         if (a > b)
  12.             a -= b;
  13.         else
  14.             b -= a;
  15.     }
  16.     return a;
  17. }
  18. // dzielenie z resztą
  19. // https://www.naukowiec.org/wiedza/matematyka/nwd--najwiekszy-wspolny-dzielnik_1183.html
  20. int NWD_Reszt(unsigned int a, unsigned int b) {
  21.     while (true) {
  22.         if (a > b)
  23.             a = a % b;
  24.         else
  25.             b = b % a;
  26.         if (a == 0 || b == 0)
  27.             // zwracamy większą
  28.             return max(a, b);
  29.     }
  30. }
  31. #pragma endregion
  32. #pragma region Silnia
  33. // rekurencyjnie
  34. int Silnia_Rek(unsigned int n) {
  35.     if (n < 2)
  36.         return 1;
  37.     return n * Silnia_Rek(n - 1);
  38. }
  39. // tablica (szybszy)
  40. vector<int> Silnie_Tab = { 1,1 };
  41. int Silnia_Tab(unsigned int n) {
  42.     int a = n - Silnie_Tab.size();
  43.     // dopełniamy tablicę z silniami aż do n
  44.     for (int i = 0; i <= a; i++) {
  45.         Silnie_Tab.push_back(Silnie_Tab.size() * Silnie_Tab[Silnie_Tab.size() - 1]);
  46.     }
  47.     return Silnie_Tab[n];
  48. }
  49. #pragma endregion
  50. #pragma region Fibonacci
  51. // rekurencyjnie
  52. int Fib_Rek(unsigned int n) {
  53.     if (n < 2)
  54.         return 1;
  55.     return Fib_Rek(n - 1) + Fib_Rek(n - 2);
  56. }
  57. // tablica (szybszy)
  58. vector<int> Fiby_Tab = { 1,1 };
  59. int Fib_Tab(unsigned int n) {
  60.     int a = n - Fiby_Tab.size();
  61.     // dopełniamy tablicę ciągiem Fibonacciego az do n
  62.     for (int i = 0; i <= a; i++)
  63.     {
  64.         Fiby_Tab.push_back(Fiby_Tab[Fiby_Tab.size() - 1] + Fiby_Tab[Fiby_Tab.size() - 2]);
  65.     }
  66.     return Fiby_Tab[n];
  67. }
  68. #pragma endregion
  69. #pragma region NWW
  70. // NWW z NWD
  71. int NWW(unsigned int a, unsigned int b) {
  72.     return (a * b) / NWD_Euk(a, b);
  73. }
  74. // NWW Z petla
  75. int NWW_Petla(unsigned int a, unsigned int b) {
  76.     // upewniamy sie ze w a jest wieksza liczba
  77.     if (a < b)
  78.         swap(a, b);
  79.     int i = 2;
  80.     // sprawdzamy kazda kolejna wielokrotnosc liczby a
  81.     while(true){
  82.         int w = a * i;
  83.         // az jest podzielna przez b, wtedy to NWW
  84.         if (w % b == 0)
  85.             return w;
  86.         i++;
  87.     }
  88. }
  89. #pragma endregion
  90. #pragma region SumaCyfr
  91. int SumaCyfr_Reszta(int liczba) {
  92.     int suma = 0;
  93.     while (liczba != 0) {
  94.         // reszta z dzielenie przez 10 to ostatnia cyfra liczby
  95.         suma += liczba % 10;
  96.         liczba /= 10;
  97.     }
  98.     return suma;
  99. }
  100. int SumaCyfr_String(int liczba) {
  101.     int suma = 0;
  102.     // to_string z biblioteki string - abs to wartośc bezwzględna
  103.     string liczba_text = to_string(abs(liczba));
  104.     for (char c : liczba_text) {
  105.         suma += c - '0';
  106.     }
  107.     return suma;
  108. }
  109. #pragma endregion
  110. #pragma region SystemyNumeryczne
  111. int ToDecimal(std::string number, unsigned int numeric_system) {
  112.     int ret = 0;
  113.     for (int i = 0; i < number.length(); i++) {
  114.         int digit;
  115.         // check if char is a letter
  116.         if (number[i] >= 'A' && number[i] <= 'Z')
  117.             // yep, so convert it into 2 digit number
  118.             digit = number[i] - 'A' + 10;
  119.         else
  120.             // nope, get single digit
  121.             digit = number[i] - '0';
  122.         // add represented number to return number
  123.         ret += pow(numeric_system, number.size() - 1 - i) * digit;
  124.     }
  125.     return ret;
  126. }
  127. string ChangeNumericSystem(unsigned int decimal, unsigned int numeric_system) {
  128.     string ret;
  129.     // reapeating till whole number is converted
  130.     while (decimal != 0) {
  131.         // get the rest of the division (modulo operator)
  132.         int rest = decimal % numeric_system;
  133.         // check if we must write it as a letter
  134.         if (rest > 9)
  135.             // yep, change it into letter and then add to return string
  136.             ret += (char)('A' + rest - 10);
  137.         else
  138.             // nope, just add it to return string
  139.             ret += to_string(rest);
  140.         decimal /= numeric_system;
  141.     }
  142.     // reverse the string, to get proper number
  143.     for (int i = 0; i < ret.length() / 2; i++) {
  144.         swap(ret[i], ret[ret.length() - 1 - i]);
  145.     }
  146.     return ret;
  147. }
  148. #pragma endregion
  149. #pragma region Inne
  150. bool CzyPierwsza(int liczba) {
  151.    
  152.     if (liczba <=2)
  153.         return liczba==2;
  154.     if (liczba % 2 == 0)
  155.         return false;
  156.     for (int i = 3; i < liczba / 2; i+=2) {
  157.         if (liczba % i == 0)
  158.             return false;
  159.     }
  160. }
  161. #pragma endregion
  162. int main()
  163. {
  164.     // Testy
  165.     // NWD
  166.     cout << endl << "NWD" << endl;
  167.     cout << NWD_Euk(10, 20) << " "<< NWD_Reszt(10,20) << endl;
  168.     cout << NWD_Euk(117649, 2401) << " " << NWD_Reszt(117649, 2401) << endl;
  169.     cout << NWD_Euk(441, 25) << " " << NWD_Reszt(441, 25) << endl;
  170.     cout << NWD_Euk(58935, 4242) << " " << NWD_Reszt(58935, 4242) << endl;
  171.     //Silnia
  172.     cout << endl << "Silnia" << endl;
  173.     for (int i = 0; i < 14; i++) {
  174.         cout << Silnia_Rek(i) << " " << Silnia_Tab(i) << endl;
  175.     }
  176.     //Fibonacci
  177.     cout << endl << "Liczby Fibonacciego" << endl;
  178.     for (int i = 0; i < 30; i++) {
  179.         cout << Fib_Rek(i) << " " << Fib_Tab(i) << endl;
  180.     }
  181.     //NWW
  182.     cout << endl << "NWW" << endl;
  183.     cout << NWW(5, 11) << " " << NWW_Petla(5,11) << endl;
  184.     cout << NWW(304, 23) << " " << NWW_Petla(304, 23) << endl;
  185.     cout << NWW(3, 17) << " " << NWW_Petla(3, 17) << endl;
  186.     cout << NWW(9, 83) << " " << NWW_Petla(9, 83) << endl;
  187.     //Suma Cyfr
  188.     cout << endl<<"Suma Cyfr" << endl;
  189.     cout << SumaCyfr_Reszta(245) << " " << SumaCyfr_String(245) << endl;
  190.     cout << SumaCyfr_Reszta(309) << " " << SumaCyfr_String(309) << endl;
  191.     cout << SumaCyfr_Reszta(13124141) << " " << SumaCyfr_String(13124141) << endl;
  192.     cout << SumaCyfr_Reszta(0) << " " << SumaCyfr_String(0) << endl;
  193.     cout << SumaCyfr_Reszta(312412) << " " << SumaCyfr_String(312412) << endl;
  194.     //Liczby Pierwsze
  195.     cout << endl << "Czy Liczba Pierwsza" << endl;
  196.     for (int i = 70; i <= 150; i++) {
  197.         cout << i << " " << (CzyPierwsza(i) ? "tak" : "nie") << endl;
  198.     }
  199.     // Systemy
  200.     cout << endl << "Systemy Liczbowe" << endl;
  201.     for (int i = 999; i < 2000; i+=9) {
  202.         cout << i << " (2," << ChangeNumericSystem(i, 2) << ", " << ToDecimal(ChangeNumericSystem(i, 2), 2) << ")" << " (8," << ChangeNumericSystem(i, 8) << ", " << ToDecimal(ChangeNumericSystem(i, 8), 8) << ")" << " (16," << ChangeNumericSystem(i, 16) << ", " << ToDecimal(ChangeNumericSystem(i, 16), 16) << ")" << endl;        
  203.     }
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment