Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. // ProjektPalindrom.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8.  
  9.  
  10. string Dec2bin(int decPodniesionaLiczba, int decLiczba) {
  11.  
  12. string binLiczba;
  13. while (decPodniesionaLiczba) {
  14. binLiczba = (decPodniesionaLiczba % 2 ? "1" : "0") + binLiczba;
  15. decPodniesionaLiczba /= 2;
  16. }
  17. return binLiczba;
  18. }
  19.  
  20. string wynikOdpowiedniaDlugosc(string binLiczba, int decLiczba) {
  21.  
  22.  
  23. while (binLiczba.length() < decLiczba) {
  24. binLiczba = "0" + binLiczba;
  25. }
  26. return binLiczba;
  27. }
  28.  
  29. //string ifPalindrom(string binLiczba) {
  30. // if (binLiczba == string(binLiczba.rbegin(), binLiczba.rend())) {
  31. // cout << binLiczba;
  32. // }
  33. // return binLiczba;
  34. //}
  35.  
  36. //string ifPalindrom(string binLiczba, int decLiczba) {
  37. // string binLiczba2 = binLiczba;
  38. ///* for (binLiczba.length(); binLiczba.length() <= 1; binLiczba.length() - 1) {
  39. // cout << binLiczba[binLiczba.length];
  40. // }*/
  41. // int a = 0;
  42. // for (decLiczba; decLiczba >= 1; decLiczba--) {
  43. // binLiczba[a] = binLiczba[decLiczba - 1];
  44. // a++;
  45. // }
  46. // if(binLiczba2 == binLiczba){
  47. // cout << binLiczba;
  48. // }
  49. // //if (binLiczba == string(binLiczba.rbegin(), binLiczba.rend())) {
  50. // // cout << binLiczba;
  51. // //}
  52. // return binLiczba;
  53. //}
  54.  
  55.  
  56. void OdejmowanieBinarne(string binLiczba, int decLiczba, int decPodniesionaLiczba) {
  57. for (; decPodniesionaLiczba >= 0; decPodniesionaLiczba--) {
  58. string binLiczba = Dec2bin(decPodniesionaLiczba, decLiczba);
  59. binLiczba = wynikOdpowiedniaDlugosc(binLiczba, decLiczba);
  60. cout << binLiczba;
  61.  
  62. }
  63. }
  64.  
  65. int main() {
  66. bool odpowiedniaLiczba = false;
  67. int decLiczba, decPodniesionaLiczba;
  68. while (cin >> decLiczba) {
  69. while (!odpowiedniaLiczba) {
  70. if (decLiczba <= 100 && decLiczba > 0) {
  71. odpowiedniaLiczba = true;
  72. }
  73. else {
  74. cin >> decLiczba;
  75. }
  76.  
  77. }
  78.  
  79. decPodniesionaLiczba = (pow(2.0, decLiczba) - 1);
  80. string binLiczba = Dec2bin(decPodniesionaLiczba, decLiczba);
  81. OdejmowanieBinarne(binLiczba, decLiczba, decPodniesionaLiczba);
  82. cout << endl;
  83. }
  84.  
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement