Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int bin1[254];
  12. int bin2[254];
  13. int obin1[254];
  14. int obin2[254];
  15. int soucet[254];
  16. char cbin1[253];
  17. char cbin2[253];
  18. bool isgood = false;
  19.  
  20. cout << "Zadejte dve binarni cisla:" << endl;
  21. cin >> cbin1;
  22. cout << "";
  23. cin >> cbin2;
  24.  
  25. for (int i = 0; i < strlen(cbin1) - 1; i++) {
  26.  
  27. if (cbin1[i] == '0' || cbin1[i] == '1') {
  28. }
  29. else { isgood = true; }
  30. }
  31.  
  32. for (int i = 0; i < strlen(cbin2) - 1; i++) {
  33. if (cbin2[i] == '0' || cbin2[i] == '1') {
  34. }
  35. else { isgood = true; }
  36. }
  37.  
  38. if (isgood == false) {
  39.  
  40. for (int i = 0; i < strlen(cbin1); i++) {
  41. bin1[i] = cbin1[i] - '0';
  42.  
  43. }
  44. for (int i = 0; i < strlen(cbin2); i++) {
  45. bin2[i] = cbin2[i] - '0';
  46. }
  47.  
  48. int y = strlen(cbin1);
  49. int z = strlen(cbin2);
  50. int k = 0;
  51. int l = 0;
  52.  
  53. for(int i = y - 1; i >= 0; i--) {
  54. obin1[k] = bin1[i];
  55. k++;
  56. }
  57. for(int i = z - 1; i >= 0; i--) {
  58. obin2[l] = bin2[i];
  59. l++;
  60. }
  61.  
  62. int q;
  63. int x;
  64. int rozdil = y - z;
  65. if(strlen(cbin1) >= strlen(cbin2)) {x = strlen(cbin1);}
  66. if(strlen(cbin1) < strlen(cbin2)) {x = strlen(cbin2);}
  67.  
  68. if(rozdil > 0) {
  69. for(int i = z; i < z + rozdil; i++) {
  70. obin2[i] = 0;
  71. }
  72. }
  73. else if(rozdil < 0) {
  74. rozdil = abs(rozdil);
  75. for(int i = y; i < y + rozdil; i++) {
  76. obin1[i] = 0;
  77. }
  78. }
  79.  
  80. q = x;
  81.  
  82. int zbytek = 0;
  83. for(int a = 0; a < x; a++) {
  84. if(obin1[a] + obin2[a] == 0) {
  85. soucet[a] = 0 + zbytek;
  86. zbytek = 0;
  87. }
  88. else if(obin1[a] + obin2[a] == 1) {
  89. if(zbytek == 0) {
  90. soucet[a] = 1;
  91. zbytek = 0;
  92. }
  93. else if(zbytek == 1) {
  94. soucet[a] = 0;
  95. zbytek = 1;
  96. }
  97. }
  98. else if(obin1[a] + obin2[a] == 2) {
  99. if(zbytek == 0) {
  100. soucet[a] = 0;
  101. zbytek = 1;
  102. }
  103. else if(zbytek == 1) {
  104. soucet[a] = 1;
  105. zbytek = 1;
  106. }
  107. }
  108. }
  109.  
  110. if (zbytek == 1) {q++;}
  111. cout << "Soucet: ";
  112. int b = 1;
  113.  
  114. if(q > x) {
  115. cout << b;
  116. for(int i = x - 1; i >= 0; i--) {
  117. cout << soucet[i];
  118. }
  119. }
  120.  
  121. if(q == x) {
  122. for(int i = x - 1; i >= 0; i--) {
  123. if(soucet[i] == 0) { continue; }
  124. else if (soucet[i] == 1) {
  125. for(int h = i; h >= 0; h--) {
  126. cout << soucet[h];
  127. }
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. else { cout << "Nespravny vstup."; }
  134.  
  135. cout << endl;
  136.  
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement