Commit 88f058d2 authored by Yates, Jonah (S&T-Student)'s avatar Yates, Jonah (S&T-Student)
Browse files

o-o

parent 19249799
......@@ -8,8 +8,15 @@
using namespace std;
/* Function Prototype Section */
void checkInvalidNumber(vector<int> & inputNum); // exits program if invalid input
string booths(vector<int> & q_multiplier, vector<int> & multiplicand); // performs booths algorithm on the given binary numbers
// exits program if invalid input
void checkInvalidNumber(vector<int> & inputNum);
// will add 2 binary numbers stored in vectors and put result in store
void addBinary(vector<int> & store, vector<int> & source, const int size);
// performs booths algorithm on the given binary numbers
string booths(vector<int> & q_multiplier, vector<int> & multiplicand, int & numAdd, int & numSub);
/* Constant Declaration Section */
const int C_ASCII_NUM_START = 48;
......@@ -21,7 +28,7 @@ int main()
vector<int> AC;
vector<int> q_multiplier = {};
vector<int> multiplicand = {};
int numLength;
int numLength, numAdd = 0, numSub = 0;
cout<<"\nNote: when entering numbers, ensure you enter them in binary,\nand that there is no leading or trailing whitespace\n"<<endl;
......@@ -49,7 +56,7 @@ int main()
cout<<") * multiplicand (";
for (auto & pos : multiplicand)
cout<<pos;
cout<<") is: "<<booths(q_multiplier, multiplicand)<<"."<<endl;
cout<<") is: "<<booths(q_multiplier, multiplicand, numAdd, numSub)<<"."<<endl;
return 0;
}
......@@ -69,30 +76,49 @@ void checkInvalidNumber(vector<int> & inputNum)
}
}
string booths(vector<int> & q_multiplier, vector<int> & multiplicand)
string booths(vector<int> & q_multiplier, vector<int> & multiplicand, int & numAdd, int & numSub)
{
/* Variable Declaration Section */
int numLength = q_multiplier.size();
int q0 = 0;
string result = "";
string twoNum;
vector<int> AC = {numLength, 0};
int numLength = q_multiplier.size(), q0 = 0;
string twoNum, result = "";
vector<int> twosComp = multiplicand;
vector<int> AC = {};
vector<int> addOne = {};
AC.reserve(numLength);
addOne.reserve(numLength);
for (int i = 0; i < numLength; i++) /* filling the new vectors with all 0s */
{
AC.push_back(0);
addOne.push_back(0);
}
addOne[numLength-1] = 1;
// getting the 2s complement of multiplicand incase we need to perform a subtract
for (int i = 0; i < numLength; i++)
{
if (twosComp[i] == 0)
twosComp[i] = 1;
else
twosComp[i] = 0;
}
addBinary(twosComp, addOne, numLength);
for (int i = 0; i < numLength; i++)
{
twoNum = to_string(q_multiplier.at(q_multiplier.back())) + to_string(q0);
cout<<"twoNum: "<<twoNum<<endl;
/* 00 = no action */
if (twoNum.compare("01") == 0) /* 01 = add multiplicand */
{
cout<<"01"<<endl;
addBinary(AC, multiplicand, numLength);
numAdd++;
}
else if (twoNum.compare("10") == 0) /* 10 = sub multiplicand */
{
cout<<"10"<<endl;
addBinary(AC, twosComp, numLength);
numSub++;
}
/* 11 = no action */
......@@ -106,10 +132,26 @@ string booths(vector<int> & q_multiplier, vector<int> & multiplicand)
}
// turning the result into a string
for (auto & pos : AC)
result += pos;
for (auto & pos : q_multiplier)
result += pos;
for (int i = 0; i < numLength; i++)
result += to_string(AC.at(i));
for (int i = 0; i < numLength; i++)
result += to_string(q_multiplier.at(i));
return result;
}
void addBinary(vector<int> & store, vector<int> & source, const int length)
{
/* Variable Declaration Section */
int carry = 0, temp;
for (int i = length-1; i >= 0 ; i--)
{
temp = store[i] ^ source[i] ^ carry;
if (store[i] + source[i] + carry >= 2)
carry = 1;
else
carry = 0;
store[i] = temp;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment