karlangadas

polinomios.c

Oct 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "polinomios.h"
  4.  
  5. int isEmpty(TLista L){
  6.     return (L.numTerminos==0);
  7. }
  8. int tamanho(TLista L){
  9.     return L.numTerminos;
  10. }
  11. void init(TLista *L){
  12.     L->inicio=NULL;
  13.     L->fin=NULL;
  14.     L->numTerminos=0;
  15. }
  16. int BuscarCoef(int expoX,int expoY,TLista *L){
  17.     TNodo *crtnodo=L->inicio;
  18.     while(crtnodo!=NULL){
  19.         if (crtnodo->expoX==expoX && crtnodo->expoY==expoY)
  20.             return crtnodo->coef;
  21.         crtnodo=crtnodo->sig;
  22.     }
  23.     return 0;
  24. }
  25. void insert(TLista *L,int coef,int expoX,int expoY){
  26.     TNodo *nodo=malloc(sizeof(TNodo));
  27.    
  28.     nodo->coef=coef;
  29.     nodo->expoX=expoX;
  30.     nodo->expoY=expoY;
  31.    
  32.     if (isEmpty(*L)){
  33.         L->inicio=nodo;
  34.         L->fin=nodo;
  35.     }
  36.     else{
  37.         L->fin->sig=nodo;
  38.         L->fin=nodo;
  39.     }
  40.     (L->numTerminos)++;
  41. }
  42.  
  43. void suma(TLista *A, TLista *B,TLista *C){
  44.     TNodo *crtnodo=A->inicio;
  45.     int coefB,coefC;
  46.     while (crtnodo!=NULL){
  47.         coefB=BuscarCoef(crtnodo->expoX,crtnodo->expoY,B);
  48.         insert(C,crtnodo->coef+coefB,crtnodo->expoX,crtnodo->expoY);
  49.         crtnodo=crtnodo->sig;
  50.     }
  51.     crtnodo=B->inicio;
  52.     while(crtnodo!=NULL){
  53.         coefC=BuscarCoef(crtnodo->expoX,crtnodo->expoY,C);
  54.         if (coefC==0){
  55.             insert(C,crtnodo->coef,crtnodo->expoX,crtnodo->expoY);
  56.         }
  57.         crtnodo=crtnodo->sig;
  58.     }
  59. }
  60.    
  61. void pprint(TLista *L){
  62.     TNodo *crtnodo=L->inicio;
  63.     printf("%d",crtnodo->coef);
  64.     if (crtnodo->expoX==1){
  65.         printf("x");
  66.     }
  67.     if (crtnodo->expoX>1){
  68.         printf("x^%d",crtnodo->expoX);
  69.     }
  70.     if (crtnodo->expoY==1){
  71.         printf("y");
  72.     }
  73.     if (crtnodo->expoY>1){
  74.         printf("y^%d",crtnodo->expoY);
  75.     }
  76.     crtnodo=crtnodo->sig;
  77.     while(crtnodo!=NULL){
  78.         if (crtnodo->coef>0)
  79.             printf("+");
  80.         if (crtnodo->coef>1){
  81.             printf("%d",crtnodo->coef);
  82.         }
  83.         if (crtnodo->expoX==1){
  84.             printf("x");
  85.         }
  86.         if (crtnodo->expoX>1){
  87.             printf("x^%d",crtnodo->expoX);
  88.         }
  89.         if (crtnodo->expoY==1){
  90.             printf("y");
  91.         }
  92.         if (crtnodo->expoY>1){
  93.             printf("y^%d",crtnodo->expoY);
  94.         }
  95.         crtnodo=crtnodo->sig;
  96.     }
  97.     printf("\n");
  98. }
Add Comment
Please, Sign In to add comment