Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. // OLA206 BY student name, CSCI2170-sec, Due: mm/dd/yy
  2. // PROGRAM ID: binadd.cpp / Binary Number Addition Program
  3. // AUTHOR: student name
  4. // INSTALLATION: MIDDLE TENNESSEE STATE UNIVERSITY
  5. // REMARKS: Adds two or more binary numbers appearing as command-line
  6. // arguments. The resulting sum is sent to stdout. The arguments are
  7. // assumed to be well-formed (valid) binary numbers; no error checking
  8. // is performed on the arguments.
  9. //
  10. // For example: binadd 100 1 1101 0
  11. // should print 10010
  12. //
  13. // . 1 . 2 . 3 . 4 . 5 . 6 . 7 .
  14. //3456789012345678901234567890123456789012345678901234567890123456789012345
  15.  
  16. #include <iostream>
  17. #include <cstring>
  18.  
  19. using namespace std;
  20.  
  21. const int MAX_DIGITS = 36; // Maximum digits in (output) sum
  22.  
  23. bool Badd( const char augend[], const char addend[], char sum[] );
  24. // IN IN OUT
  25.  
  26.  
  27. int main( int argc, char* argv[] )
  28. // IN IN
  29. {
  30. char partialSum[MAX_DIGITS+1]; // Partial sum of the binary numbers
  31. char sum[MAX_DIGITS+1]; // Sum of the binary numbers
  32. bool noError; // No error flag
  33.  
  34. // Exit if insufficient arguments were supplied.
  35. if (argc < 3)
  36. {
  37. cout << "Error: insufficient arguments.\n";
  38. return 1;
  39. }
  40.  
  41. // Add together the first two binary numbers on the command-line.
  42. noError = Badd( argv[1], argv[2], sum );
  43.  
  44. // Add any additional binary numbers to the partial sum.
  45. for (int counter = 3; noError && counter < argc; counter++)
  46. {
  47. strcpy( partialSum, sum );
  48. noError = Badd( partialSum, argv[counter], sum );
  49. }
  50.  
  51. // Print answer on standard output.
  52. if (noError)
  53. cout << sum << endl;
  54. else
  55. cout << 'E' << endl;
  56.  
  57. return 0;
  58. } // end main
  59.  
  60.  
  61. bool Badd( const char augend[], const char addend[], char sum[] )
  62. // IN IN OUT
  63. // Pre: augend and addend are strings representing valid binary numbers.
  64. // Post: sum is a string representing the sum of augend + addend.
  65. // Returns true if successful in addition, false otherwise.
  66. {
  67. int x, carry = 0;
  68. for(int i = 0; i < argc-1; i++)
  69. {
  70. if(augend[i]==1 && addend[i]==1 && carry ==1)
  71. {
  72. sum[x]=1;
  73. carry=1;
  74. }
  75. else if(augend[i]==1 && addend[i]==1 && carry ==0)
  76. {
  77. sum[x]=0;
  78. carry=1;
  79. }
  80. else if(augend[i]==0 && addend[i]==1 && carry ==0)
  81. {
  82. sum[x]=1;
  83. carry=0;
  84. }
  85. else if(augend[i]==0 && addend[i]==1 && carry ==1)
  86. {
  87. sum[x]=0;
  88. carry=1;
  89. }
  90. else if(augend[i]==1 && addend[i]==0 && carry ==1)
  91. {
  92. sum[x]=0;
  93. carry=1;
  94. }
  95. else if(augend[i]==1 && addend[i]==0 && carry ==0)
  96. {
  97. sum[x]=1;
  98. carry=0;
  99. }
  100. else if(augend[i]==0 && addend[i]==0 && carry ==1)
  101. {
  102. sum[x]=1;
  103. carry=0;
  104. }
  105. else if(augend[i]==0 && addend[i]==0 && carry ==0)
  106. {
  107. sum[x]=0;
  108. carry=0;
  109. }
  110. x++;
  111. }
  112. sum[0] = '0';
  113. sum[1] = '\0';
  114.  
  115. return true;
  116. } // end Badd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement