karlangadas

bignum

Oct 23rd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "bignum.h"
  4.  
  5. void Bignum_init(TBignum *B){
  6.     B->inicio=NULL;
  7.     B->fin=NULL;
  8.     B->numElem=0;
  9. }
  10. int Bignum_isEmpty(TBignum B){
  11.     return (B.numElem==0);
  12. }
  13. void Bignum_insert(TBignum *B, TElemento e){
  14.     TNodo *nodo=malloc(sizeof(TNodo));
  15.     nodo->elem=e;
  16.     nodo->sig=NULL;
  17.     nodo->ant=NULL;
  18.     if (Bignum_isEmpty(*B)){
  19.         B->inicio=nodo;
  20.         B->fin=nodo;
  21.     }
  22.     else{
  23.         nodo->ant=B->fin;
  24.         B->fin->sig=nodo;
  25.         B->fin=nodo;
  26.  
  27.     }
  28.     (B->numElem)++;
  29. }
  30. void Bignum_addLeft(TBignum *B, TElemento e){
  31.     TNodo *nodo=malloc(sizeof(TNodo));
  32.     nodo->elem=e;
  33.     nodo->sig=NULL;
  34.     nodo->ant=NULL;
  35.     if (Bignum_isEmpty(*B)){
  36.         B->inicio=nodo;
  37.         B->fin=nodo;
  38.     }
  39.     else{
  40.         nodo->sig=B->inicio;
  41.         B->inicio->ant=nodo;
  42.         B->inicio=nodo;
  43.     }
  44.     (B->numElem)++;
  45. }
  46. int Bignum_length(TBignum B){
  47.     return B.numElem;
  48. }
  49. void Bignum_print(TBignum B){
  50.     TNodo *crtnodo=B.inicio;
  51.     while(crtnodo!=NULL){
  52.         printf("%d",crtnodo->elem);
  53.         crtnodo=crtnodo->sig;
  54.     }
  55.     printf("\n");
  56. }
  57.  
  58. void Bignum_finalize(TBignum *A){
  59.     TNodo *crtnodo=A->inicio;
  60.     TNodo *nodoAnt=A->inicio;
  61.     while (crtnodo!=NULL){
  62.         nodoAnt=crtnodo;
  63.         crtnodo=crtnodo->sig;
  64.         free(nodoAnt);
  65.         (A->numElem)--;
  66.     }
  67.     A->inicio=NULL;
  68.     A->fin=NULL;
  69. }
  70.  
  71. void Bignum_suma(TBignum A,TBignum B,TBignum *C){
  72.     Bignum_finalize(C);
  73.     TNodo *crtnodoA=A.fin;
  74.     TNodo *crtnodoB=B.fin;
  75.     int sum;
  76.     int carry=0;
  77.     while (crtnodoA!=NULL && crtnodoB!=NULL){
  78.         sum=(crtnodoA->elem)+(crtnodoB->elem)+carry;
  79.         if (sum<10)
  80.             carry=0;
  81.         else{
  82.             carry=sum / 10;
  83.             sum =sum % 10;
  84.         }
  85.         Bignum_addLeft(C,sum);
  86.         crtnodoA=crtnodoA->ant;
  87.         crtnodoB=crtnodoB->ant;    
  88.     }
  89.  
  90.     while (crtnodoA!=NULL){
  91.         Bignum_addLeft(C,crtnodoA->elem+carry);
  92.         crtnodoA=crtnodoA->ant;
  93.         carry=0;
  94.     }
  95.     while (crtnodoB!=NULL){
  96.         Bignum_addLeft(C,crtnodoB->elem+carry);
  97.         crtnodoB=crtnodoB->ant;
  98.         carry=0;
  99.     }
  100.     if (carry!=0){
  101.         Bignum_addLeft(C,carry);
  102.     }  
  103.  
  104. }
  105. void Bignum_resta(TBignum A,TBignum B,TBignum *C){
  106.     Bignum_finalize(C);
  107.     TNodo *crtnodoA=A.fin;
  108.     TNodo *crtnodoB=B.fin;
  109.     int rest;
  110.     int carry=0;
  111.     while (crtnodoA!=NULL && crtnodoB!=NULL){
  112.         rest=(crtnodoA->elem)-((crtnodoB->elem)+carry);
  113.         if (rest>0)
  114.             carry=0;
  115.         else{
  116.             carry=1;
  117.             rest =10+rest;
  118.         }
  119.         Bignum_addLeft(C,rest);
  120.         crtnodoA=crtnodoA->ant;
  121.         crtnodoB=crtnodoB->ant;    
  122.     }
  123.  
  124.     while (crtnodoA!=NULL){
  125.         Bignum_addLeft(C,crtnodoA->elem-carry);
  126.         crtnodoA=crtnodoA->ant;
  127.         carry=0;
  128.     }
  129.     while (crtnodoB!=NULL){
  130.         Bignum_addLeft(C,crtnodoB->elem-carry);
  131.         crtnodoB=crtnodoB->ant;
  132.         carry=0;
  133.     }
  134.    
  135. }
  136. void Bignum_multiplicacion(TBignum A,TBignum B,TBignum *C){
  137.     TBignum AUX;
  138.     Bignum_init(&AUX);
  139.    
  140.     TBignum AUX2;
  141.     Bignum_init(&AUX2);
  142.    
  143.     TBignum AUX3;
  144.     Bignum_init(&AUX3);
  145.  
  146.     TNodo *crtnodoA=A.fin;
  147.     TNodo *crtnodoB=B.fin;
  148.     int mult;
  149.     int carry=0;
  150.     int i=0;
  151.  
  152.     while (crtnodoB!=NULL){
  153.         for (int j=1;j<=i;j++)
  154.             Bignum_addLeft(&AUX,0);
  155.  
  156.         crtnodoA=A.fin;
  157.         while(crtnodoA!=NULL){
  158.             mult=(crtnodoA->elem)*(crtnodoB->elem)+carry;
  159.             crtnodoA=crtnodoA->ant;
  160.             if (mult<10){
  161.                 carry=0;
  162.             }
  163.             else{
  164.                 carry=mult / 10;
  165.                 mult= mult % 10;
  166.             }
  167.             Bignum_addLeft(&AUX,mult);
  168.         }
  169.         if (carry!=0)
  170.             Bignum_addLeft(&AUX,carry);
  171.  
  172.         Bignum_suma(*C,AUX,&AUX2);
  173.         Bignum_finalize(C);
  174.         Bignum_suma(AUX2,AUX3,C);
  175.        
  176.         Bignum_finalize(&AUX);
  177.         Bignum_finalize(&AUX2);
  178.         i++;
  179.         crtnodoB=crtnodoB->ant;
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment