Advertisement
Guest User

Untitled

a guest
May 27th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. std::string binaryAddition(std::string a, std::string b) {
  2. std::string b1 = a;
  3. std::string b2 = b;
  4. std::string c;
  5.  
  6. bool z = false;
  7.  
  8. while (c.length() != a.length())
  9. {
  10. for (int i = 0; i < a.length(); i++)
  11. {
  12. std::cout << "A: " << b1[i] << ", " << "B: " << b2[i] << ", " << "Z: " << z << std::endl;
  13.  
  14. if ((b1[i] == 1) && (b2[i] == 0) && (z == false))
  15. {
  16. c.push_back(1);
  17. z = false;
  18. }
  19.  
  20. else if ((b1[i] == 0) && (b2[i] == 0) && (z == true))
  21. {
  22. c.push_back(1);
  23. z = false;
  24. }
  25.  
  26. else if ((b1[i] == 0) && (b2[i] == 0) && (z == false))
  27. {
  28. c.push_back(0);
  29. z = false;
  30. }
  31.  
  32. else if ((b1[i] == 1) && (b2[i] == 1) && (z == false))
  33. {
  34. c.push_back(0);
  35. z = true;
  36. }
  37.  
  38. else if ((b1[i] == 1) && (b2[i] == 0) && (z == true))
  39. {
  40. c.push_back(0);
  41. z = true;
  42. }
  43.  
  44. else if ((b1[i] == 1) && (b2[i] == 1) && (z == true))
  45. {
  46. c.push_back(1);
  47. z = true;
  48. }
  49. }
  50. }
  51. return c;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement