fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. /*help funkcija kojom iz unesenog
  9. stringa punis bool kontejner */
  10. bool toBool(const char& s)
  11. {
  12. assert(s == '0' || s == '1');
  13. return s == '1';
  14. }
  15.  
  16. class Covjece {
  17. public:
  18. Covjece( const vector<bool>& binary )
  19. : m_binary(binary)
  20. { it = m_binary.begin();
  21. cout<< endl << "rezultat : " << boolalpha << check() << endl;
  22. }
  23.  
  24. bool check();
  25.  
  26. private:
  27. const vector<bool>& m_binary;
  28. vector<bool>::const_iterator it;
  29. };
  30.  
  31. /* check() ti je core cijele operacije.
  32. kako si postavio zadatak , navedeni
  33. ima samo jedno true rijesenje :: iza n nula moraju slijediti do kraja samo
  34. jedinice. prvi while chekira nule , drugi
  35. jedinice. u svim ostalim situacijama dobivas false*/
  36.  
  37. bool Covjece::check()
  38. {
  39. if(*it == 1)
  40. return false;
  41.  
  42. while( it != m_binary.end() )
  43. {
  44. if( *it == 0)
  45. it++;
  46.  
  47. else
  48. {
  49. while( it != m_binary.end() )
  50. {
  51. if( *it == 1 )
  52. it++;
  53.  
  54. else
  55. return false;
  56. }
  57. return true;
  58. }
  59.  
  60. }
  61. }
  62.  
  63. int main()
  64. {
  65. vector<bool> binary;
  66. string num;
  67. cout<< "Pucaj binarni broj : ";
  68. cin>> num;
  69. for(string::reverse_iterator it = num.rbegin() ; it != num.rend() ; it++)
  70. {
  71. binary.push_back( toBool( *it ) );
  72. }
  73.  
  74. /*odbaciti suvisnje prednje nule
  75. u binarnom broju*/
  76. while( *(binary.end() - 1) == 0 )
  77. {
  78. binary.erase( binary.end() - 1 );
  79. }
  80.  
  81. cout<< endl << "Provjera za broj : ";
  82. for(int i=binary.size()-1 ; i > -1 ; i--)
  83. cout<< binary[ i ];
  84.  
  85. Covjece provjeri_ovaj_broj(binary);
  86.  
  87. return 0;
  88. }
Success #stdin #stdout 0.01s 2820KB
stdin
1111
stdout
Pucaj binarni broj : 
Provjera za broj : 1111
rezultat : false