Advertisement
Guest User

nihaha

a guest
Nov 30th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <ctime>
  4. #include <fstream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. /* Helper method: given two unequal sized bit strings, converts them to
  10. same length by adding leading 0s in the smaller string. Returns the
  11. the new length*/
  12. int igualarLargo(string &str1, string &str2)
  13. {
  14. int len1 = str1.size();
  15. int len2 = str2.size();
  16. if (len1 < len2)
  17. {
  18. for (int i = 0 ; i < len2 - len1 ; i++)
  19. str1 = '0' + str1;
  20. return len2;
  21. }
  22. else if (len1 > len2)
  23. {
  24. for (int i = 0 ; i < len1 - len2 ; i++)
  25. str2 = '0' + str2;
  26. }
  27. return len1; // If len1 >= len2
  28. };
  29.  
  30. // The main function that adds two bit sequences and returns the addition
  31. string agregarCadena( string first, string second )
  32. {
  33. string result; // To store the sum bits
  34.  
  35. // make the lengths same before adding
  36. int length = igualarLargo(first, second);
  37. int carry = 0; // Initialize carry
  38.  
  39. // Add all bits one by one
  40. for (int i = length-1 ; i >= 0 ; i--)
  41. {
  42. int firstBit = first.at(i) - '0';
  43. int secondBit = second.at(i) - '0';
  44.  
  45. // boolean expression for sum of 3 bits
  46. int sum = (firstBit ^ secondBit ^ carry)+'0';
  47.  
  48. result = (char)sum + result;
  49.  
  50. // boolean expression for 3-bit addition
  51. carry = (firstBit&secondBit) | (secondBit&carry) | (firstBit&carry);
  52. }
  53.  
  54. // if overflow, then add a leading 1
  55. if (carry) result = '1' + result;
  56.  
  57. return result;
  58. };
  59.  
  60. // A utility function to multiply single bits of strings a and b
  61. int multiplyiSingleBit(string a, string b)
  62. { return (a[0] - '0')*(b[0] - '0'); }
  63.  
  64. // The main function that multiplies two bit strings X and Y and returns
  65. // result as long integer
  66. long int multiply(string X, string Y)
  67. {
  68. // Find the maximum of lengths of x and Y and make length
  69. // of smaller string same as that of larger string
  70. int n = igualarLargo(X, Y);
  71.  
  72. // Base cases
  73. if (n == 0) return 0;
  74. if (n == 1) return multiplyiSingleBit(X, Y);
  75.  
  76. int fh = n/2; // First half of string, floor(n/2)
  77. int sh = (n-fh); // Second half of string, ceil(n/2)
  78.  
  79. // Find the first half and second half of first string.
  80. // Refer http://goo.gl/lLmgn for substr method
  81. string Xl = X.substr(0, fh);
  82. string Xr = X.substr(fh, sh);
  83.  
  84. // Find the first half and second half of second string
  85. string Yl = Y.substr(0, fh);
  86. string Yr = Y.substr(fh, sh);
  87.  
  88. // Recursively calculate the three products of inputs of size n/2
  89. long int P1 = multiply(Xl, Yl);
  90. long int P2 = multiply(Xr, Yr);
  91. long int P3 = multiply(agregarCadena(Xl, Xr), agregarCadena(Yl, Yr));
  92.  
  93. // Combine the three products to get the final result.
  94. return P1*(1<<(2*sh)) + (P3 - P1 - P2)*(1<<sh) + P2;
  95. };
  96.  
  97. int main() {
  98. string entrada = "";
  99. cout << "\nPlease enter a valid sentence :\n";
  100. getline(cin, entrada);
  101. cout << "hinaha";
  102. return 0;
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement