Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "bignum.h"
- void Bignum_init(TBignum *B){
- B->inicio=NULL;
- B->fin=NULL;
- B->numElem=0;
- }
- int Bignum_isEmpty(TBignum B){
- return (B.numElem==0);
- }
- void Bignum_insert(TBignum *B, TElemento e){
- TNodo *nodo=malloc(sizeof(TNodo));
- nodo->elem=e;
- nodo->sig=NULL;
- nodo->ant=NULL;
- if (Bignum_isEmpty(*B)){
- B->inicio=nodo;
- B->fin=nodo;
- }
- else{
- nodo->ant=B->fin;
- B->fin->sig=nodo;
- B->fin=nodo;
- }
- (B->numElem)++;
- }
- void Bignum_addLeft(TBignum *B, TElemento e){
- TNodo *nodo=malloc(sizeof(TNodo));
- nodo->elem=e;
- nodo->sig=NULL;
- nodo->ant=NULL;
- if (Bignum_isEmpty(*B)){
- B->inicio=nodo;
- B->fin=nodo;
- }
- else{
- nodo->sig=B->inicio;
- B->inicio->ant=nodo;
- B->inicio=nodo;
- }
- (B->numElem)++;
- }
- int Bignum_length(TBignum B){
- return B.numElem;
- }
- void Bignum_print(TBignum B){
- TNodo *crtnodo=B.inicio;
- while(crtnodo!=NULL){
- printf("%d",crtnodo->elem);
- crtnodo=crtnodo->sig;
- }
- printf("\n");
- }
- void Bignum_finalize(TBignum *A){
- TNodo *crtnodo=A->inicio;
- TNodo *nodoAnt=A->inicio;
- while (crtnodo!=NULL){
- nodoAnt=crtnodo;
- crtnodo=crtnodo->sig;
- free(nodoAnt);
- (A->numElem)--;
- }
- A->inicio=NULL;
- A->fin=NULL;
- }
- void Bignum_suma(TBignum A,TBignum B,TBignum *C){
- Bignum_finalize(C);
- TNodo *crtnodoA=A.fin;
- TNodo *crtnodoB=B.fin;
- int sum;
- int carry=0;
- while (crtnodoA!=NULL && crtnodoB!=NULL){
- sum=(crtnodoA->elem)+(crtnodoB->elem)+carry;
- if (sum<10)
- carry=0;
- else{
- carry=sum / 10;
- sum =sum % 10;
- }
- Bignum_addLeft(C,sum);
- crtnodoA=crtnodoA->ant;
- crtnodoB=crtnodoB->ant;
- }
- while (crtnodoA!=NULL){
- Bignum_addLeft(C,crtnodoA->elem+carry);
- crtnodoA=crtnodoA->ant;
- carry=0;
- }
- while (crtnodoB!=NULL){
- Bignum_addLeft(C,crtnodoB->elem+carry);
- crtnodoB=crtnodoB->ant;
- carry=0;
- }
- if (carry!=0){
- Bignum_addLeft(C,carry);
- }
- }
- void Bignum_resta(TBignum A,TBignum B,TBignum *C){
- Bignum_finalize(C);
- TNodo *crtnodoA=A.fin;
- TNodo *crtnodoB=B.fin;
- int rest;
- int carry=0;
- while (crtnodoA!=NULL && crtnodoB!=NULL){
- rest=(crtnodoA->elem)-((crtnodoB->elem)+carry);
- if (rest>0)
- carry=0;
- else{
- carry=1;
- rest =10+rest;
- }
- Bignum_addLeft(C,rest);
- crtnodoA=crtnodoA->ant;
- crtnodoB=crtnodoB->ant;
- }
- while (crtnodoA!=NULL){
- Bignum_addLeft(C,crtnodoA->elem-carry);
- crtnodoA=crtnodoA->ant;
- carry=0;
- }
- while (crtnodoB!=NULL){
- Bignum_addLeft(C,crtnodoB->elem-carry);
- crtnodoB=crtnodoB->ant;
- carry=0;
- }
- }
- void Bignum_multiplicacion(TBignum A,TBignum B,TBignum *C){
- TBignum AUX;
- Bignum_init(&AUX);
- TBignum AUX2;
- Bignum_init(&AUX2);
- TBignum AUX3;
- Bignum_init(&AUX3);
- TNodo *crtnodoA=A.fin;
- TNodo *crtnodoB=B.fin;
- int mult;
- int carry=0;
- int i=0;
- while (crtnodoB!=NULL){
- for (int j=1;j<=i;j++)
- Bignum_addLeft(&AUX,0);
- crtnodoA=A.fin;
- while(crtnodoA!=NULL){
- mult=(crtnodoA->elem)*(crtnodoB->elem)+carry;
- crtnodoA=crtnodoA->ant;
- if (mult<10){
- carry=0;
- }
- else{
- carry=mult / 10;
- mult= mult % 10;
- }
- Bignum_addLeft(&AUX,mult);
- }
- if (carry!=0)
- Bignum_addLeft(&AUX,carry);
- Bignum_suma(*C,AUX,&AUX2);
- Bignum_finalize(C);
- Bignum_suma(AUX2,AUX3,C);
- Bignum_finalize(&AUX);
- Bignum_finalize(&AUX2);
- i++;
- crtnodoB=crtnodoB->ant;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment