Advertisement
Guest User

Bitwise Calculator

a guest
Nov 23rd, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: James
  4.  *
  5.  * Created on November 23, 2014, 2:41 AM
  6.  */
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include <sstream>
  11. #include <cmath>
  12. using namespace std;
  13.  
  14. #define nl "\n"
  15.  
  16. /*
  17.  * This is a bitwise calculator
  18.  */
  19.  
  20. bool is_number(const string& astring)
  21. {
  22.     return !astring.empty() &&
  23.             astring.find_first_not_of("-0123456789") == string::npos;
  24. }
  25.  
  26. int badd (int a, int b)
  27. {
  28.     int c;
  29.    
  30.     do
  31.     {
  32.         c = a & b;
  33.         c = c << 1;
  34.         b = a ^ b;
  35.    
  36.         a = b & c;
  37.         a = a << 1;
  38.         c = b ^ c;
  39.            
  40.         b = c & a;
  41.         b = b << 1;
  42.         a = c ^ a;
  43.     } while (b != 0);
  44.     return a;
  45. }
  46.  
  47. int bsub (int a, int b)
  48. {
  49.     int c = 0;
  50.     b = badd(~b, 1); // generates the twos compliment of b
  51.     c = badd(a, b); // adds the twos compliment to a
  52.     return c;
  53. }
  54.  
  55. int bmul (int a, int b)
  56. {
  57.     int A = a;
  58.    
  59.     if ((a == 0) || (b == 0))
  60.         return 0;
  61.    
  62.     for (int b2 = (0 - b); (abs(b) > 1) || (abs(b2) > 1); b--, b2++)
  63.         A = badd(A, a);
  64.    
  65.     return A;
  66. }
  67.  
  68. int bmod (int a, int b)
  69. {  
  70.     int B = b;
  71.    
  72.     while (a <= B)
  73.     {
  74.         B = bsub(B, a);
  75.     }
  76.     return B;
  77. }
  78.  
  79. string bdiv (int a, int b)
  80. {
  81.     int i = 0;
  82.     stringstream conv;
  83.    
  84.     while (a >= b)
  85.     {
  86.         a = bsub(a, b);
  87.         i++;
  88.     }
  89.    
  90.     conv << i << ".";
  91.    
  92.     for (int c = 1; c <= 5; c++)
  93.     {
  94.         int r = 0;
  95.         a = bmul(10, a);
  96.        
  97.         while (a >= b)
  98.         {
  99.             a = bsub(a, b);
  100.             r++;
  101.         }
  102.         conv << r;
  103.     }
  104.     return conv.str();
  105. }
  106.  
  107. string op_select(string op,double a,double b)
  108. {
  109.     string out;
  110.     stringstream convert;
  111.    
  112.     if (op == "+")
  113.         convert << badd(a, b);
  114.     if (op == "-")
  115.         convert << bsub(a, b);
  116.     if (op == "*")
  117.         convert << bmul(a, b);
  118.     if (op == "%")
  119.         convert << bmod(a, b);
  120.     if (op == "/")
  121.         convert << bdiv(a, b);
  122.     if (!(op.find_first_not_of("+-*/%") == string::npos))
  123.         convert << "f";
  124.     out = convert.str();
  125.     return out;
  126. }
  127.  
  128. int main()
  129. {
  130.     double a, b;
  131.     string input, result;
  132.    
  133.     cout << "enter an integer\n";
  134.     getline (cin, input);
  135.    
  136.     if ( is_number(input) )
  137.         stringstream(input) >> a;
  138.     else
  139.     {
  140.         cout << "that's not a number...";
  141.         return 1;
  142.     }
  143.    
  144.     cout << "enter another integer\n";
  145.     getline (cin, input);
  146.    
  147.     if ( is_number(input) )
  148.         stringstream(input) >> b;
  149.     else
  150.     {
  151.         cout << "that's not a number...";
  152.         return 1;
  153.     }
  154.    
  155.     cout << "enter a simple math operator (+,-,*,/,%)\n";
  156.     getline (cin, input);
  157.    
  158.     result = op_select(input,a,b);
  159.     if (result == "f")
  160.     {
  161.         cout << "invalid operator " << input;
  162.         return 1;
  163.     }
  164.    
  165.     cout << "the answer to " << a << " " << input << " " << b
  166.             << " is: " << result << ".\n";
  167.    
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement