Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "bigInt.h"
/**
* Description:
* Returns the total digits of the BIG_INT received by parameter
* Parameters:
* b - is the BIG_INT
* Return:
* the total digits of the BIG_INT received
*/
int big_size(const BIG_INT b)
{
return b[0];
}
/**
* Description:
* Returns the BIG_INT signal received by parameter
* Parameters:
* b - is the BIG_INT
* Return:
* POSITIVE or NEGATIVE
*/
int big_signal (const BIG_INT b)
{
return b[1];
}
/**
* Description:
* The function displays in the standard output (terminal) the BIG_INT received by parameter
* Parameters:
* big - is the BIG_INT to display
* Return:
* there is not
*/
void big_show (const BIG_INT big)
{
int pos = big_size(big) + 1; // Position the most significant digit
if ( big_signal(big) == BIG_NEGATIVE ) putchar('-');
while (pos > 1) {
putchar('0'+ big[ pos-- ]);
}
}
/**
* Description:
* Starts a BIG_INT with the value of a long integer
* Parameters:
* n - integer whose value must be placed in BIG_INT
* big - BIG_INT which will contain the value of the integer n
* Return:
* there is not
*/
void big_from_long (BIG_INT big, long n)
{
if ( n >= 0 ) big[1] = BIG_POSITIVE;
else {
big[1] = BIG_NEGATIVE;
n = -n;
}
int i=0;
// first, store all digits in the array, starting
// from index 2, from least significant digit to most significant
of {
big[i+2] = n% 10;
i++;
n=n/10;
} while (n > 0);
// now store the number of digits in position 0
big[0] = i;
}
/*----------------------------------------------- ----------------------------
* Start of functions to be performed
*------------------------------------------------ -------------------------*/
/**
* Description:
* The function fills a BIG_INT from a string with the representation of an integer value
* Parameters:
*str - string of numeric characters.
* big - is the BIG_INT to build from str
* Return:
* The function returns true if built the BIG_INT "big" successfully or false
* if any non-numeric character was found in the string.
*/
bool big_from_string(BIG_INT big, const char str[])
{
//convert all digit characters contained in str
//to the big digit array
int p; //this p is the index of traversing the array and will also serve as a counter
p=0;
big[1]=BIG_POSITIVE;
if( str[0]=='-' ) {
big[1]=BIG_NEGATIVE;
p=1;
}
if( str[0]=='+' ) {
big[1]=BIG_POSITIVE;
p=1;
}
//count total digits and validated
for( ; str[p]!='\0'; ++p ){
if( p>MAX_DIGITS || str[p]<'0' || str[p]>'9' ) return false;
}
/*
printf("TEST the p index at str=%d\n",p);
*/
big[0] = p;
if(str[0] == '-' ) {
big[0]=p-1;
}
if(str[0] == '+' ) {
big[0] = p-1;
}
// reverse copy str to big
--P;
/*
printf("AFTER TESTING p=%d"
" total Digits big=%d\n",p, big[0]);
*/
for( int i=2; p>=0; --p, i++ ) {
big[i] = str[p]-'0'; }//converts to int
/*
printf("TEST see the value in big\n");
big_show (big) ;
printf("\n");
*/
return true;
}
/**
* Description:
* The function compares two BIG_INT, in absolute terms, and returns a negative value, zero
* or a positive value depending on whether the first BIG_INT is less than, equal to or greater than the second BIG_INT
* Parameters:
* b1 - first BIG_INT
* b2 - according to BIG_INT
* Return:
negative value if b1 < b2, 0 if b1 == b2, positive value if b1 > b2
*/
int big_cmp_abs( const BIG_INT b1, const BIG_INT b2 )
{
int i,j;
for(i=b1[0]+1;i>2;i--){
for (j=b2[0]+1;j>2;j--){
if(b1[i]>b2[j]){
return 1;
}else if(b1[i]==b2[j]){
return 0;
}
}
}
return -1;
}
/**
* Description:
* The function compares two BIG_INT and returns a negative value, zero
* or a positive value depending on whether the first BIG_INT is less than, equal to or greater than the second BIG_INT
* Parameters:
* b1 - first BIG_INT
* b2 - according to BIG_INT
* Return:
negative value if b1 < b2, 0 if b1 == b2, positive value if b1 > b2
*/
int big_cmp( const BIG_INT b1, const BIG_INT b2 )
{
int result=big_cmp_abs(b1,b2);
if(b1[1]>b2[1]&& result==1 ){
return 1;
}else if(b1[1]==b2[1]&& result==0){
return 0;
}
return -1;
}
/**
* Description:
Function that subtracts two BIG_INT numbers, assuming the first is greater in absolute value
* Parameters:
* b1 - first operand BIG_INT
* b2 - second operand BIG_INT
* bm - BIG_INT which stores the result
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
bool big_sub_aux( const BIG_INT b1, const BIG_INT b2, BIG_INT bm )
{
int k=2;
int counter=0;
int result=0;
int i,j;
int carry=0;
for(i=b1[0]+1;i>2;i--){
for(j=b2[0]-1;i>2;i--){
if( b1[1]!=b2[1]&& big_cmp(b1,b2)==1){
result=b1[i]-b2[j];
bm[k]=result;
k++;
counter++;
bm[0]=counter;
bm[1]=1;
if(bm[0]<255){
return true;
}
}
else if(b1[1]!=b2[1]&& big_cmp(b1,b2)==-1){
result=b1[i]-b2[j];
carry=result/10;
result=carry%10;
bm[k]=result;
k++;
counter++;
bm[0]=counter;
bm[1]=0;
if(bm[0]<255){
return true;
}
}
}
}
return false;
}
/**
* Description:
The function calculates the sum of two BIG_INT and stores the result in the third BIG_INT,
regardless of the signal
* Parameters:
* b1 - first operand BIG_INT
* b2 - second operand BIG_INT
* bm - BIG_INT which stores the result
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
bool big_add_aux( const BIG_INT b1, const BIG_INT b2, BIG_INT bm )
{
int k=2;
int counter=0;
int result=0;
int i,j;
for(i=b1[0]+1;i>2;i--){
for(j=b2[0]-1;i>2;i--){
if(b1[1]==b2[1]){
result=b1[i]+b2[j];
bm[k]=result;
k++;
counter++;
bm[0]=counter;
if(bm[0]<255){
return true;
}
}
}
}
return false;
}
/**
* Description:
The function calculates the sum of two BIG_INT and stores the result in the third BIG_INT
* taking into account the BIG_INT sign
* Parameters:
* b1 - first operand BIG_INT
* b2 - second operand BIG_INT
* bm - BIG_INT which stores the result
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
bool big_add( const BIG_INT b1, const BIG_INT b2, BIG_INT bm )
{
if(b1[1]==b2[1]){
big_add_aux(b1,b2,bm);
while(bm[0]<255){
return true;
}
}else if(b1[1]!=b2[1]){
big_sub_aux(b1,b2,bm);
if (bm[0]<255){
return true;
}
}
return false;
}
/**
* Description:
* The function calculates the difference between two BIG_INT and stores the result in the third BIG_INT,
* taking into account the BIG_INT sign
* Parameters:
* b1 - first operand BIG_INT
* b2 - second operand BIG_INT
* bm - BIG_INT which stores the result
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
bool big_sub( const BIG_INT b1, const BIG_INT b2, BIG_INT bm )
{
if(big_cmp(b1,b2)==0){
bm[1]=1;
big_add_aux(b1,b2,bm);
while(bm[0]<255){
return true;
}
} else{
big_sub_aux(b1,b2,bm);
while(bm[0]<255){
return true;
}
}
return false;
}
/**
* Description:
* The function returns true if the BIG_INT parameter corresponds to zero value
* Parameters:
* b - the BIG_INT
* Return:
* The function returns false if nonzero, true otherwise.
*/
bool big_iszero(const BIG_INT b)
{
// MISSING IMPLEMENTATION
return true;
}
/**
* Description:
* The function performs BIG_INT multiplication by digit and this is done
* no successive sums
* Parameters:
* b1 - first operand BIG_INT
* n - second operand is an integer
* bm - BIG_INT which stores the result
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
bool big_mul_digit( const BIG_INT b1, int n, BIG_INT bm)
{
// MISSING IMPLEMENTATION
return false;
}
/**
* Description:
* The function that occupies the least significant positions with zeros and copies the digits to the
* most significant
* Parameters:
* b1 - first operand BIG_INT
* factor - second operand is an integer shift factor
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
bool big_mult_power10( BIG_INT b1, int factor )
{
// MISSING IMPLEMENTATION
return false;
}
/**
* Description:
* The function calculates the multiplication between two BIG_INT and stores the result in the third BIG_INT,
* taking into account the BIG_INT sign
* Parameters:
* b1 - first operand BIG_INT
* b2 - second operand BIG_INT
* bm - BIG_INT which stores the result
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
bool big_mul( const BIG_INT b1, const BIG_INT b2, BIG_INT bm )
{
// MISSING IMPLEMENTATION
return true;
}
/*----------------------------------------------- ----------------------------
* End of functions to be performed
*------------------------------------------------ --------------------------*/