fork(5) download
  1. #include <string>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int getIntFromBinaryText(const char* text)
  7. {
  8. int value = 0;
  9. while(*text)
  10. {
  11. value <<= 1;
  12. if(*text == '1') value |= 1;
  13. else if(*text == '0') ; // do nothing
  14. else return -1; // invalid input... return a negative number to indicate error
  15. ++text;
  16. }
  17.  
  18. return value;
  19. }
  20.  
  21. std::string getBinaryTextFromInt(int value)
  22. {
  23. std::string text;
  24. // note I'm assuming 'value' is positive here
  25.  
  26. while(value > 0)
  27. {
  28. if(value & 1) text += '1';
  29. else text += '0';
  30. value >>= 1;
  31. }
  32.  
  33. if(text.empty()) return "0";
  34. std::reverse( text.begin(), text.end() ); // #include <algorithm>
  35. return text;
  36. }
  37.  
  38. int main() // note: main... not that _tmain garbage.
  39. {
  40. int divisor = getIntFromBinaryText("1011"); // or just "divisor = 11;"
  41.  
  42. std::string text;
  43. cout << " dividend ?" << endl;
  44. cin >> text;
  45.  
  46. int dividend = getIntFromBinaryText(text.c_str());
  47. if(dividend < 0)
  48. {
  49. cout << "Invalid input";
  50. return 1;
  51. }
  52.  
  53. int result = dividend / divisor;
  54. int remainder = dividend % divisor;
  55.  
  56. cout << "dividend=" << getBinaryTextFromInt(dividend);
  57. cout << " divisor=" << getBinaryTextFromInt(divisor);
  58. cout << " result=" << getBinaryTextFromInt(result);
  59. cout << " remainder=" << getBinaryTextFromInt(remainder);
  60.  
  61. }
Success #stdin #stdout 0s 3480KB
stdin
1001
stdout
 dividend ?
dividend=1001    divisor=1011    result=0    remainder=1001