c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

jumbo-int.c (3547B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 struct jumboInt{
      5     short* revValue;
      6     unsigned long long digits; // technically not arbitrary length, but like, really...
      7     short sign;
      8 };
      9 
     10 struct jumboInt* makeJumboInt(int input){
     11     struct jumboInt* retVal = malloc(sizeof(struct jumboInt));
     12 
     13     unsigned long long digits = 0;
     14     unsigned long long check = input;
     15 
     16     if(input < 0){
     17         retVal->sign = -1;
     18         input *= -1;
     19         check *= -1;
     20     }
     21     else{
     22         retVal->sign = 1;
     23     }
     24 
     25     for(int i = 0; check > 0; ++i){
     26         check /= 10;
     27         digits += 1;
     28     }
     29     if(input == 0){
     30         digits = 1;
     31     }
     32 
     33     short* inp = malloc(sizeof(short) * digits);
     34 
     35     int itr = 0;
     36     while(itr < digits){
     37         short current = input % 10;
     38         input /= 10;
     39         inp[itr] = current;
     40         itr += 1;
     41     }
     42 
     43     short* res = malloc(sizeof(short) * digits);
     44     for(int i = 0; i < digits; ++i){
     45         res[i] = inp[(digits-1) - i];
     46     }
     47     free(inp);
     48 
     49     retVal->revValue = res;
     50     retVal->digits = digits;
     51 
     52 
     53     return retVal;
     54 }
     55 
     56 void printJumbo(struct jumboInt* result){
     57     if (result->digits != 0){
     58         if (result->sign == -1){
     59             printf("-");
     60         }
     61         for(unsigned long long i = 0; i < result->digits ; ++i){
     62             printf("%hd" , (result->revValue[i]));
     63         }
     64     }
     65 }
     66 
     67 struct jumboInt* addJumbo(struct jumboInt* in1, struct jumboInt* in2){
     68 
     69     struct jumboInt* shorter;
     70     struct jumboInt* longer;
     71 
     72     if(in1->digits < in2->digits){
     73         shorter = in1;
     74         longer = in2;
     75         
     76     }
     77     else{
     78         shorter = in2;
     79         longer = in1;
     80     }
     81 
     82     short* shorterReverse = malloc(sizeof(short) * shorter->digits);
     83     short* longerReverse = malloc(sizeof(short) * longer->digits);
     84 
     85     short* resultReverse = malloc(sizeof(short) * (longer->digits + 1));
     86 
     87     for(int i = 0; i < shorter->digits; ++i){
     88         shorterReverse[i] = shorter->revValue[(shorter->digits-1) - i];
     89     }
     90 
     91     for(int i = 0; i < longer->digits; ++i){
     92         longerReverse[i] = longer->revValue[(longer->digits-1) - i];
     93     }
     94 
     95     short carry = 0;
     96     for(int i = 0; i < longer->digits; ++i){
     97         short sum = longerReverse[i] + carry;
     98         if (i < shorter->digits) {
     99             sum += shorterReverse[i];
    100         }
    101         resultReverse[i] = sum % 10;
    102         carry = sum / 10;
    103     }
    104 
    105     short* finalResult;
    106     unsigned long long finalDigits;
    107     short sign = longer->sign;
    108 
    109     if(carry == 1){
    110         resultReverse[longer->digits] = 1;
    111         finalResult = malloc(sizeof(short) * (longer->digits + 1));
    112         finalDigits = longer->digits + 1;
    113     }
    114     else{
    115         finalResult = malloc(sizeof(short) * (longer->digits));
    116         finalDigits = longer->digits;
    117     }
    118 
    119     for(int i = 0 ; i < finalDigits; ++i){
    120         finalResult[i] = resultReverse[(finalDigits - 1) - i];
    121     }
    122 
    123 
    124     free(shorterReverse);
    125     free(longerReverse);
    126     free(resultReverse);
    127 
    128     struct jumboInt* result = malloc(sizeof(struct jumboInt));
    129     result->revValue = finalResult;
    130     result->sign = sign;
    131     result->digits = finalDigits;
    132 
    133     return result;
    134 }
    135 
    136 
    137 // NOTE: This is not really tested for negative numbers. Addition of two negatives should work, but addition of a negative and positive
    138 // does not.
    139 
    140 int main(){ 
    141     struct jumboInt* sum = makeJumboInt(10);
    142 
    143     for(int i = 0 ; i < 1000000; ++i){
    144         struct jumboInt* newSum = addJumbo(sum, sum);
    145         free(sum->revValue);
    146         free(sum);
    147         sum = newSum;
    148     }
    149 
    150     printJumbo(sum);
    151     printf("\n");
    152 
    153     return 0;
    154 }