Advertisement
TheWhiteFang

MTQ1 yeo

Dec 18th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. //yeo punya
  2. //midtermq1
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. string AddBin(string num1, string num2);
  8. string TwosComp(string bin);
  9. string SubBin(string a, string b);
  10.  
  11. int main()
  12. {
  13.     string a, b,result;
  14.     cout << "Enter 2 number: ";
  15.     cin >> a;
  16.     cin >> b;
  17.     result = AddBin(a, b);
  18.     cout << result << endl;
  19.     result = TwosComp(a);
  20.     cout << result << endl;
  21.     result = SubBin(a, b);
  22.     cout << result << endl;
  23.     return 0;
  24. }
  25.  
  26. string AddBin(string num1, string num2){
  27.     string temp = "";
  28.     int carry = 0;
  29.     for (int i = num1.length() - 1; i >= 0; i--){
  30.         if (num1.at(i) == '1'&& num2.at(i) == '1' && carry == 0){
  31.             temp = '0' + temp;
  32.             carry = 1;
  33.             continue;
  34.         }
  35.         if (num1.at(i) == '0'&& num2.at(i) == '0' && carry == 0){
  36.             temp = '0' + temp;
  37.             carry = 0;
  38.             continue;
  39.         }
  40.         if (num1.at(i) == '1'&& num2.at(i) == '1' && carry == 1){
  41.             temp = '1' + temp;
  42.             carry = 1;
  43.             continue;
  44.         }
  45.         if (num1.at(i) == '0'&& num2.at(i) == '0' && carry == 1){
  46.             temp = '1' + temp;
  47.             carry = 0;
  48.             continue;
  49.         }
  50.         if (num1.at(i) == '1'&& num2.at(i) == '0' && carry == 0){
  51.             temp = '1' + temp;
  52.             carry = 0;
  53.             continue;
  54.         }
  55.         if (num1.at(i) == '0'&& num2.at(i) == '1' && carry == 0){
  56.             temp = '1' + temp;
  57.             carry = 0;
  58.             continue;
  59.         }
  60.         if (num1.at(i) == '1'&& num2.at(i) == '0' && carry == 1){
  61.             temp = '0' + temp;
  62.             carry = 1;
  63.             continue;
  64.         }
  65.         if (num1.at(i) == '0'&& num2.at(i) == '1' && carry == 1){
  66.             temp = '0' + temp;
  67.             carry = 1;
  68.             continue;
  69.         }
  70.     }
  71.     if (carry == 1)
  72.         temp = '1' + temp;
  73.     return temp;
  74. }
  75. string TwosComp(string bin){
  76.     int len = bin.length();
  77.     string temp = 0, one;
  78.     for (int i = 0; i < len; i++){
  79.         if (bin[i] == '1')
  80.             temp = temp + '0';
  81.         else
  82.             temp = temp + "1";
  83.     }
  84.     for (int j = 0; j < len - 1; j++)
  85.         one[j] = '0';
  86.     one += "1";
  87.     temp = AddBin(temp, one);
  88.     return temp;
  89. }
  90. string SubBin(string a, string b){
  91.     string mb = TwosComp(b);
  92.     string temp = AddBin(a, mb);
  93.     return temp;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement