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

Finished Project Code

parent 88f058d2
.vscode/
Output
\ No newline at end of file
Output
sampleInput.txt
sampleOutput.txt
\ No newline at end of file
......@@ -15,6 +15,9 @@ 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);
// debug print will print out the contents of AC Q q0
void debugPrint(vector<int> & AC, vector<int> & Q, const int q0);
// performs booths algorithm on the given binary numbers
string booths(vector<int> & q_multiplier, vector<int> & multiplicand, int & numAdd, int & numSub);
......@@ -33,7 +36,7 @@ int main()
cout<<"\nNote: when entering numbers, ensure you enter them in binary,\nand that there is no leading or trailing whitespace\n"<<endl;
// getting input of the multiplier
cout<<"Enter the 1st number, multiplier: ";
cout<<"Enter the 1st number, Q, multiplier: ";
cin>>input;
cout<<"";
numLength = input.length();
......@@ -42,7 +45,7 @@ int main()
checkInvalidNumber(q_multiplier);
// getting input of the multiplicand
cout<<"Enter the 2nd number, multiplicand: ";
cout<<"Enter the 2nd number, B, multiplicand: ";
cin>>input;
cout<<"";
for (int i = 0; i < numLength; i++)
......@@ -76,6 +79,35 @@ void checkInvalidNumber(vector<int> & inputNum)
}
}
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;
}
}
void debugPrint(vector<int> & AC, vector<int> & Q, const int q0)
{
/* Variable Declaration Section */
int numLength = AC.size();
for (int i = 0; i < numLength; i++)
cout<<AC[i];
cout<<" ";
for (int i = 0; i < numLength; i++)
cout<<Q[i];
cout<<" "<<q0<<endl;
}
string booths(vector<int> & q_multiplier, vector<int> & multiplicand, int & numAdd, int & numSub)
{
/* Variable Declaration Section */
......@@ -107,23 +139,23 @@ string booths(vector<int> & q_multiplier, vector<int> & multiplicand, int & numA
for (int i = 0; i < numLength; i++)
{
twoNum = to_string(q_multiplier.at(q_multiplier.back())) + to_string(q0);
twoNum = "" + to_string(q_multiplier[numLength-1]) + to_string(q0);
/* 00 = no action */
if (twoNum.compare("01") == 0) /* 01 = add multiplicand */
/* 00 = no action */
if (twoNum.compare("01") == 0) /* 01 = add multiplicand */
{
addBinary(AC, multiplicand, numLength);
numAdd++;
}
else if (twoNum.compare("10") == 0) /* 10 = sub multiplicand */
else if (twoNum.compare("10") == 0) /* 10 = sub multiplicand */
{
addBinary(AC, twosComp, numLength);
numSub++;
}
/* 11 = no action */
/* 11 = no action */
// shifting right after the appropriate action was taken
q0 = q_multiplier.at(q_multiplier.back());
q0 = q_multiplier[numLength-1];
for (int i = numLength-1; i > 0; i--)
q_multiplier[i] = q_multiplier[i-1];
q_multiplier[0] = AC[numLength-1];
......@@ -139,19 +171,3 @@ string booths(vector<int> & q_multiplier, vector<int> & multiplicand, int & numA
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