1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <QtCore/QCoreApplication>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int intAddition(int num1, int num2);
int intMultiply(int num1, int num2);
int getNumbers(int &num1, int &num2);
int getBinaryAnswer(int dividend, int divisor, int &binary, int &ans);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int dividend = 0, divisor = 0, counter = 1, binaryAns = 0;
getNumbers(dividend, divisor);
getBinaryAnswer(dividend, divisor, counter, binaryAns);
cout<<endl;
return a.exec();
}
int intAddition(int num1, int num2)
{
int carry = 0, sum = 0;
do
{
carry = num1 & num2;
sum = num1 ^ num2;
carry <<= 1;
}while(carry);
return(sum);
}
int intMultiply(int num1, int num2)
{
int product = 0, i = 0;
while(i < num2)
{
product = intAddition(num1, product);
i = intAddition(i, 1);
}
return product;
}
int getNumbers(int ÷nd, int &divisor)
{
cout<<"Please enter the dividend."<<endl;
cin>>dividend;
cout<<"Please enter the divisor."<<endl;
cin>>divisor;
return 0;
}
int getBinaryAnswer(int dividend, int divisor, int &binary, int &ans)
{
int divisorCounter = divisor;
while (divisorCounter < dividend)
{
binary <<= 1;
divisorCounter <<= 1;
}
while (divisorCounter > divisor)
{
// if (intAddition((intMultiply(ans, divisor)),divisorCounter) <= dividend)
if (intAddition((ans*divisor),divisorCounter) <= dividend)
ans |= binary;
binary >>= 1;
divisorCounter >>= 1;
}
cout<<ans;
return ans;
}
|