33,317
社区成员
发帖
与我相关
我的任务
分享#include<stdio.h>
#include<math.h>
#define MAX_LARGE_DIGITS 1000
#define LARGE_INT_MODULE 10
typedef int* LargeInt ;
void Linit( LargeInt );
void LintToLarge( LargeInt , const int ) ;
int LtoInt( const LargeInt ) ;
void Lassign( LargeInt , const LargeInt ) ;
void Ladd( LargeInt , const LargeInt , const LargeInt ) ;
void Lscale( LargeInt , const LargeInt , const int ) ;
void Lprint( const LargeInt ) ;
int Lcompare( const LargeInt , const LargeInt ) ;
void Lcarry(LargeInt) ;
main()
{
LargeInt powerOf2 , temp ;
int i ;
/* Print table of powers of 2 */
Linit( powerOf2 );
Linit( temp );
LintToLarge( powerOf2 , 1 );
printf("Powers of 2 n 2^n\n");
for( i = 1 ; i <= 200 ; i++ )
{
printf(" %i ", i ) ;
Lprint( powerOf2 ) ;
printf("\n");
Ladd( temp, powerOf2, powerOf2 ) ;
Lassign( powerOf2 , temp );
}
return 0;
}
void Linit( LargeInt intVar )
{
int newArray[MAX_LARGE_DIGITS+2];
for(int i=0; i<=MAX_LARGE_DIGITS+1; i++)
newArray[i]=-1;
intVar=newArray;
}
void LintToLarge( LargeInt target , const int value )
{
int tempValue=value;
if(tempValue<0)
{
target[0]=-1;
tempValue*=-1;
}
else
target[0]=1;
target[1]=1;
target[2]=tempValue;
Lcarry(target);
}
int LtoInt( const LargeInt source )
{
if(source[1]>5)
{
printf("\nLarge Int too big to fit in int.");
return 0;
}
if(source[1]==5)
{
if(source[6]<=3)
{
if(source[5]<=2)
{
if(source[4]<=7)
{
if(source[3]<=6)
{
if(((source[2]<=7)&&(source[0]==1))||((source[2]<=8)&&(source[0]==-1))) ;
else
{
printf("\nLarge Int too big to fit in int.");
return 0;
}
}
}
}
}
}
int tempInt=0;
for(int i=source[1]+1; i>=2; i--)
tempInt*=LARGE_INT_MODULE+source[i];
return tempInt;
}
void Lassign( LargeInt target , const LargeInt source )
{
for(int i=0; i<=source[1]+1; i++)
target[i]=source[i];
}
void Ladd( LargeInt target , const LargeInt leftOp , const LargeInt rightOp )
{
target[0]=1;
int i;
for(i=2; i<=leftOp[1]+1; i++)
target[i]=leftOp[i]*leftOp[0];
for(i=2; i<=rightOp[1]+1; i++)
target[i]+=rightOp[i]*rightOp[0];
if(leftOp[1]>rightOp[1])
target[1]=leftOp[1];
else
target[1]=rightOp[1];
Lcarry(target);
}
void Lscale( LargeInt target , const LargeInt source , const int scaleBy )
{
target[0]=source[0];
target[1]=source[1];
for(int i=2; i<=source[1]+1; i++)
target[i]=source[i]*scaleBy;
Lcarry(target);
}
void Lprint( const LargeInt source )
{
if(source[0]==-1)
printf("-");
for(int i=source[1]+1; i>=2; i--)
printf ("%d", source[i]);
}
int Lcompare( const LargeInt leftOp , const LargeInt rightOp )
{
if(leftOp[0]>rightOp[0]) //if signs are different, the results are obvious
return 1;
else
{
if(leftOp[0]<rightOp[0])
return -1;
}
//Then, signs are the same, now look at absolute value
int abscompare=0;
if(leftOp[1]!=rightOp[1])
{
if(leftOp[1]>rightOp[1])
{
abscompare=1;
}
else
abscompare=-1;
}
else
{
for(int i=leftOp[1]+1; i>=2; i--)
{
if(leftOp[i]>rightOp[i])
{
abscompare=1;
break;
}
else
{
if(leftOp[i]<rightOp[i])
{
abscompare=-1;
break;
}
}
}
}
switch(abscompare)
{
case 0:
return 0;
case 1:
if(leftOp[0]>0)
return 1;
else
return -1;
case -1:
if(leftOp[0]>0)
return -1;
else
return 1;
}
}
void Lcarry (LargeInt source)
{
//Carry forward
int i,j;
if((source[2]>=LARGE_INT_MODULE)&&source[1]==1)
{
source[3]=source[2]/LARGE_INT_MODULE;
source[2]%=LARGE_INT_MODULE;
source[1]++;
}
for(i=2; i<=source[1]+1; i++)
{
j=i;
while(j<=source[1]+1)
{
source[j+1]+=source[j]/LARGE_INT_MODULE;
source[j]%=LARGE_INT_MODULE;
j++;
if(source[source[1]+1]>=LARGE_INT_MODULE)
{
source[source[1]+2]=source[source[1]+1]/LARGE_INT_MODULE;
source[source[1]+1]%=LARGE_INT_MODULE;
source[1]++;
if(source[1]>1000)
{
printf("\nCarry forward overflow!");
return;
}
}
}
}
//If top digit minus, flip all digits and sign
if(source[source[1]+1]<0)
{
for(i=2; i<=source[1]+1; i++)
source[i]*=-1;
source[0]*=-1;
}
//Carry backword
for(i=source[1]; i>=2; i--)
{
j=i;
while((source[j]<0)&&(j>=2))
{
source[j+1]-=(int)(log10(0-source[j])/(int)log10(LARGE_INT_MODULE)+1);
source[j]+=LARGE_INT_MODULE*(int)log10(0-source[j])/(int)log10(LARGE_INT_MODULE);
}
}
//Clear unused digits
i=source[1]+1;
while((source[i]==0)&&(i>=2))
source[1]--;
}