我试图用符号计算二元除法,但第二个补码是错误的,总是给出1 0 1 0.我需要纠正那个错误,但我不知道该怎么做。补码计算是主要的。此外,代码可以更干净或所有功能都很好。提前致谢。
#include<iostream>
#include<cmath>
using namespace std;
int numDigit;
//Function to convert the passed decimal number into binary and return the base address of the binary array
void DecimalToBinary(int *numArray, int num)
{
int neg, c = 0, d, temp;
//Checking for a negative number
if(num<0)
{
neg=1;
num=abs(num);
}// End of if condition
//Continuously dividing the number by 2 and storing the remainder
while(num!=0)
{
*(numArray + c++) = num % 2;
num /= 2;
}// End of while loop
for(d = c; d <= numDigit - 1; d++)
*(numArray + d) = 0;
//Reversing the binary array
for(c = 0; c <= (numDigit - 1) / 2; c++)
{
temp = *(numArray + c);
*(numArray + c) = *(numArray + numDigit - 1 - c);
*(numArray + numDigit - 1 - c) = temp;
}// End of for loop
if(neg == 1)
{
//Setting sign bit to 1 in case of a negative number
*numArray = 1;
//Taking 2's Complement to store the negative number
for(c = numDigit - 1; c >= 1; c--)
if(*(numArray + c) == 1)
break;
c--;
while(c >= 1)
*(numArray + c) =! *(numArray + c--);
}// End of if condition
}// End of function
//Function to convert a binary array to decimal by taking its base address as input and returning the decimal equivalent
int BinaryToDecimal(int *binArr, int num)
{
int sum = 0, c;
//Taking 2's Complement of the negative number to get back its original magnitude
if(*binArr == 1)
{
for(c = numDigit - 1; c >= 1; c--)
if(*(binArr + c) == 1)
break;
c--;
while(c >= 1)
*(binArr + c) =! *(binArr + c--);
}// End of if condition
//Repeatedly multiplying by increasing powers of 2 and storing the sum after each iteration
for(c = 1; c <= num - 1; c++)
sum += ((int)pow(2.0, num - 1 - c)*(*(binArr + c)));
if(*binArr == 1)
sum *= -1;
//Returning the equivalent decimal of the passed binary array
return sum;
}// End of function
// main function definition
int main()
{
int dividend, divisor, sc, ssor, send, s1, c, sum, allz, remainder, quotient;
char choice;
start:
cout<<"Enter dividend in decimal format\n";
cin>>dividend;
cout<<"Enter divisor in decimal format\n";
cin>>divisor;
if(dividend != 0 && divisor == 0)
//Handling Divide-by-Zero Exception
cout<<"Infinity\n";
else if(dividend == 0 && divisor == 0)
//Handling case when both dividendand divisor are zero
cout<<"Undeterminate\n";
else
{
if(dividend < 0 && divisor < 0)
{
dividend = abs(dividend);
divisor = abs(divisor);
cout<<"As both dividend and divisor are negative, -ve signcancels out\n";
}// End of if condition
if(dividend < 0 && divisor > 0)
{
dividend = abs(dividend);
divisor *= -1;
}// End of if condition
float td;
td = log((double)abs(dividend))/log(2.0);
//Finding out the number of digits required to store both dividend and divisor in binary format
numDigit = (int)ceil(td+1);
cout<<"\nSC is "<<numDigit<<endl;
sc = numDigit;
int Q[numDigit], A[numDigit], M1[numDigit], M2[numDigit], pra[numDigit], counter;
//Converting the dividend into binary as Q
DecimalToBinary(Q, dividend);
cout<<"\nQ is ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<Q[counter];
send = Q[0];
//Converting the divisor into binary as M1
DecimalToBinary(M1, divisor);
cout<<"\nM is ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<M1[counter];
ssor = M1[0];
//Taking the 2's Complement of M1 and storing it as M2
for(counter = numDigit - 1; counter >= 0; counter--)
{
if(M1[counter]==0)
M2[counter]=0;
else
break;
}// End of for loop
M2[counter--] = 1;
while(counter >= 0)
M2[counter] = !M1[counter--];
cout<<"\nM'+1 is ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<M2[counter];
//Creating a combined array of A and Q named AQ
int AQ[2 * numDigit];
for(counter = 0; counter < numDigit; counter++)
AQ[counter] = 0;
for(counter = numDigit; counter < 2 * numDigit; counter++)
AQ[counter] = Q[counter - numDigit];
cout<<"\nInitial AQ : ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<AQ[counter];
cout<<" ";
for(counter = numDigit; counter <= 2 * numDigit - 1; counter++)
cout<<AQ[counter];
cout<<endl;
//Repeating the 2's Complement DivisionAlgorithm SC number of times
while(sc-->0)
{
//Displaying current AQ
cout<<"\nAQ : ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<AQ[counter];
cout<<" ";
for(counter = numDigit; counter <= 2 * numDigit - 1; counter++)
cout<<AQ[counter];
cout<<" SC is "<<sc + 1;
//Shifting AQ Left by 1 bit
for(counter = 0; counter <= (2 * numDigit - 2); counter++)
AQ[counter] = AQ[counter + 1];
AQ[2 * numDigit - 1] = 0;
//Displaying Left Shifted AQ
cout<<"\nShift Left ";
cout<<"AQ : ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<AQ[counter];
cout<<" ";
for(counter = numDigit; counter <= 2 * numDigit - 1; counter++)
//Storing the original sign of A
cout<<AQ[counter];
s1 = AQ[0];
for(counter = 0; counter < numDigit; counter++)
//Storing the previous value of A to restore later if required
pra[counter] = AQ[counter];
//Checking if A and M have the same sign
if(AQ[0] == M1[0])
{
cout<<"\nAdd M'+1 ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<M2[counter];
c = 0;
for(counter = numDigit - 1; counter >= 0; counter--)
{
//Adding 2's Complement of M to A
sum = c + AQ[counter] + M2[counter];
switch(sum)
{
case 0:
{
c = 0;
AQ[counter] = 0;
break;
}
case 1:
{
c = 0;
AQ[counter] = 1;
break;
}
case 2:
{
c = 1;
AQ[counter] = 0;
break;
}
case 3:
{
c = 1;
AQ[counter] = 1;
break;
}
default:
{
cout<<"\nError Encountered!";
break;
}
}// End of switch - case
}// End of for loop
//Displaying AQ after the addition
cout<<"\nAQ: ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<AQ[counter];cout<<" ";
for(counter = numDigit; counter <= 2 * numDigit - 1; counter++)
cout<<AQ[counter];
}// End of if condition
//Executed if A and M have different signs
else
{
cout<<"\nAdd M ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<M1[counter];
c = 0;
for(counter = numDigit - 1; counter >= 0; counter--)
{
//Adding M to A
sum = c + AQ[counter] + M1[counter];
switch(sum)
{
case 0:
{
c = 0;
AQ[counter] = 0;
break;
}
case 1:
{
c = 0;
AQ[counter] = 1;
break;
}
case 2:
{
c = 1;
AQ[counter] = 0;
break;
}
case 3:
{
c = 1;
AQ[counter] = 1;
break;
}
default:
{
cout<<"\nError Encountered!";
break;
}
}// End of switch - case
}// End of for loop
//Displaying AQ after the addition
cout<<"\nAQ: ";
for(counter = 0; counter <= numDigit - 1; counter++)
cout<<AQ[counter];
cout<<" ";
for(counter = numDigit; counter <= 2 * numDigit - 1; counter++)
cout<<AQ[counter];
}// End of else
//Checking if A is equal to zero or not
allz = 1;
for(counter = numDigit - 1; counter >= 0; counter--)
if(AQ[counter] == 1)
{
allz = 0;
break;
}// End of if condition
//Executed if sign of A is the same before and after the operation OR if A is zero
if(s1 == AQ[0] || allz == 1)
{
//Setting Q0 equal to 1
AQ[2 * numDigit - 1] = 1;
cout<<"\nSet Q0=1 \n";
}// End of if condition
if(s1!=AQ[0] && allz==0) //Executed if sign of A is different before and after the operation AND A is non-zero
{
AQ[2 * numDigit - 1]=0; //Setting Q0 equal to 0
cout<<"\nSet Q0=0 and Restore A \n";
for(counter = 0; counter < numDigit; counter++) //Restoring the previous value of
AQ[counter] = pra[counter];
}// End of if condition
}// End of while loop
//Checking if the sign of dividend is different from the divisor's sign
if(send != ssor)
{
for(counter = 2 * numDigit - 1; counter >= numDigit; counter--)
if(AQ[counter] == 1)
break;
counter--;
while(counter >= numDigit)
AQ[counter]=!AQ[counter--];
}// End of if condition
for(counter = 0; counter < numDigit; counter++)
{
A[counter] = AQ[counter];
Q[counter] = AQ[counter + numDigit];
}// End of for loop
//Converting A from binary to decimal format, A is the remainder after division
remainder = BinaryToDecimal(A, numDigit);
//Converting Q from binary to decimal format, Q is the quotient of the division
quotient = BinaryToDecimal(Q, numDigit);
cout<<"\n Quotient is "<<quotient<<" and Remainder is "<<remainder;//Displaying the Quotient and Remainder
}// End of else
cout<<"\n Press Y to perform another division or any other key to exit!\n";
cin>>choice;
//Asking the user if he wants to carry out another division or exit the program
if(choice == 'Y' || choice == 'y')
goto start;
return 0;
}// End of main function
/*