Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. string Odwroc(string wyraz)
  7. {
  8. string result = "";
  9. for (int i = wyraz.length()-1; i>=0; i--)
  10. {
  11. result += wyraz[i];
  12. }
  13.  
  14. return result;
  15. }
  16.  
  17. string DecToBin(int liczba)
  18. {
  19. string result = "";
  20. while (liczba != 0)
  21. {
  22. if (liczba % 2 == 0)
  23. {
  24. result += "0";
  25. }
  26. else
  27. {
  28. result += "1";
  29. }
  30. liczba = liczba / 2;
  31. }
  32. return Odwroc(result);
  33. }
  34. string DecToOct(int liczba)//10 na 8
  35. {
  36. string result = "";
  37. while (liczba != 0)
  38. {
  39. result += char(int(liczba%8)+48);
  40. liczba = liczba / 8;
  41. }
  42. return Odwroc(result);
  43. }
  44. string DecToHex(int liczba)//10 na 16
  45. {
  46. string result = "";
  47. while (liczba != 0)
  48. {
  49. if (liczba%16<10)
  50. {
  51. result += char(int(liczba % 16) + 48);
  52. }
  53. else
  54. {
  55. result += char(int(liczba % 16) + 55);
  56. }
  57.  
  58. liczba = liczba / 16;
  59. }
  60. return Odwroc(result);
  61. }
  62.  
  63. int main()
  64. {
  65. int liczba;
  66. cout << "podaj liczba" << endl;
  67. cin >> liczba;
  68. cout << DecToBin(liczba) << endl;
  69. cout << DecToOct(liczba) << endl;
  70. cout << DecToHex(liczba) << endl;
  71.  
  72.  
  73.  
  74. system("PAUSE");
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement