fojtasd

Untitled

Nov 6th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string input1="", input2="";
  8. cout << "Zadejte dve binarni cisla:" << endl;
  9. cin >> input1;
  10. cin >> input2;
  11.  
  12. //ošetøení vstupu pro neciselne vstupy
  13. if (cin.fail())
  14. {
  15. cout << "Nespravny vstup." << endl;
  16. return 0;
  17. }
  18. //ošetøení vstupu pro input1 cisla
  19. for (int a=0; a<input1.size(); a++)
  20. {
  21. char ch1=input1[a];
  22. if ((ch1!='0') && (ch1!='1'))
  23. {
  24. cout << "Nespravny vstup." << endl;
  25. return 0;
  26. }
  27. }
  28. //ošetøení vstupu pro input2 cisla
  29. for (int b=0; b<input2.size(); b++)
  30. {
  31. char ch2=input2[b];
  32. if ((ch2!='0') && (ch2!='1'))
  33. {
  34. cout << "Nespravny vstup." << endl;
  35. return 0;
  36. }
  37. }
  38. //pøevod do desitkove soustavy prvniho cisla
  39.  
  40. long long output1 = 0;
  41. long long weight1 = (1ull << (input1.size() - 1));
  42.  
  43. for(int c=0; c < input1.size(); c++) {
  44. output1 += (input1[c] - '0') * weight1;
  45. weight1 = weight1 >> 1;
  46. }
  47. //pøevod do desitkove soustavy druheho cisla
  48.  
  49. long long output2 = 0;
  50. long long weight2 = (1ull << (input2.size() - 1));
  51.  
  52. for(int d=0; d < input2.size(); d++) {
  53. output2 += (input2[d] - '0') * weight2;
  54. weight2 = weight2 >> 1;
  55. }
  56.  
  57. //scitame desitkova cisla
  58.  
  59. unsigned long long result10=output1+output2;
  60.  
  61. //prevod desitkove do binarni
  62.  
  63. long long binar = 0;
  64. long long remainder, i = 1, step = 1;
  65.  
  66. while (result10!=0)
  67. {
  68. remainder = result10%2;
  69. result10 /= 2;
  70. binar += remainder*i;
  71. i *= 10;
  72. }
  73.  
  74. //vypisujeme vysledek
  75. cout << "Soucet: " << binar << endl;
  76.  
  77.  
  78.  
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment