Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_POLINOM_SIZE (1024)
  4.  
  5. typedef struct _cvor* Position;
  6. typedef struct _cvor
  7. {
  8.     int eksponent;
  9.     int koeficijent;
  10.     Position next;
  11. }cvor;
  12.  
  13. int ProcitajDatoteke(char* imeDatoteke, Position pp, Position dp);
  14. int UcitajPolinomIzStringa(char* ulazniString, Position trenutniCvor);
  15. int DodajUListuSortirano(Position trenutniCvor, int koeficijent, int eksponent);
  16. int KreirajCvorIDodajIzaTrenutnog(Position trenutniCvor, int koeficijent, int eksponent);
  17. int DodajIzaTrenutnog(Position noviCvor, Position trenutniCvor);
  18. void IspisiPolinom(char* opis, Position trenutniCvor);
  19. int ZbrojiPolinome(Position pp, Position dp, Position sumaPolinoma);
  20. int PomnoziPolinome(Position pp, Position dp, Position umnozakPolinoma);
  21. void DealocirajMemoriju(Position trenutniCvor);
  22.  
  23. int main(int argc, char* argv[])
  24. {
  25.     cvor pp, dp;
  26.     cvor sumaPolinoma, umnozakPolinoma;
  27.     pp.next = NULL;
  28.     dp.next = NULL;
  29.     sumaPolinoma.next = NULL;
  30.     umnozakPolinoma.next = NULL;
  31.     ProcitajDatoteke("polinomi.txt", &pp, &dp);
  32.     IspisiPolinom("\nPrvi polinom", pp.next);
  33.     IspisiPolinom("Drugi polinom", dp.next);
  34.     ZbrojiPolinome(&pp, &dp, &sumaPolinoma);
  35.     IspisiPolinom("Zbroj polinoma", sumaPolinoma.next);
  36.     PomnoziPolinome(&pp, &dp, &umnozakPolinoma);
  37.     IspisiPolinom("Umnozak polinoma", umnozakPolinoma.next);
  38.  
  39.     DealocirajMemoriju(&pp);
  40.     DealocirajMemoriju(&dp);
  41.     DealocirajMemoriju(&sumaPolinoma);
  42.     DealocirajMemoriju(&umnozakPolinoma);
  43.     return 0;
  44.  
  45. }
  46. int ProcitajDatoteke(char* imeDatoteke, Position prviPolinom, Position drugiPolinom)
  47. {
  48.     FILE* f;
  49.     char buffer[MAX_POLINOM_SIZE];
  50.     f = fopen(imeDatoteke, "r");
  51.  
  52.     fgets(buffer, MAX_POLINOM_SIZE, f);
  53.     UcitajPolinomIzStringa(buffer, prviPolinom);
  54.  
  55.     fgets(buffer, MAX_POLINOM_SIZE, f);
  56.     UcitajPolinomIzStringa(buffer, drugiPolinom);
  57.  
  58.     return 0;
  59.  
  60. }
  61. int UcitajPolinomIzStringa(char* string, Position position)
  62. {
  63.     int koef, pot, offset;
  64.     while (sscanf(string, " %d %d %n", &koef, &pot, &offset) != EOF)
  65.     {
  66.         DodajUListuSortirano(position, koef, pot);
  67.         string += offset;
  68.     }
  69.     return 0;
  70.  
  71. }
  72. int DodajUListuSortirano(Position position, int koeficijent, int eksponent)
  73. {
  74.     while (position->next != NULL && position->next->eksponent > eksponent)
  75.         position = position->next;
  76.  
  77.     if (position->next != NULL && position->next->eksponent == eksponent)
  78.         position->next->koeficijent += koeficijent;
  79.     else
  80.         KreirajCvorIDodajIzaTrenutnog(position, koeficijent, eksponent);
  81.     return 0;
  82.  
  83. }
  84. int KreirajCvorIDodajIzaTrenutnog(Position currentPosition, int koeficijent, int eksponent)
  85. {
  86.     Position q;
  87.     q = (Position)malloc(sizeof(cvor));
  88.     q->eksponent = eksponent;
  89.     q->koeficijent = koeficijent;
  90.     DodajIzaTrenutnog(q, currentPosition);
  91.     return 0;
  92.  
  93. }
  94. int DodajIzaTrenutnog(Position noviCvor, Position currentPosition)
  95. {
  96.     noviCvor->next = currentPosition->next;
  97.     currentPosition->next = noviCvor;
  98.  
  99.     return 0;
  100. }
  101. void IspisiPolinom(char* title, Position currentPosition)
  102. {
  103.     bool first = true;
  104.     printf("%s=", title);
  105.     while (currentPosition != NULL)
  106.     {
  107.         if (first == true)
  108.         {
  109.             if (currentPosition->koeficijent == 1 && currentPosition->eksponent == 0)
  110.                 printf("1");
  111.             if (currentPosition->koeficijent == -1 && currentPosition->eksponent == 0)
  112.                 printf("- 1");
  113.             if (currentPosition->koeficijent == 1 && currentPosition->eksponent > 0)
  114.                 printf("x^%d", currentPosition->eksponent);
  115.             if (currentPosition->koeficijent == 1 && currentPosition->eksponent < 0)
  116.                 printf("x^(%d)", currentPosition->eksponent);
  117.             if (currentPosition->koeficijent > 0 && currentPosition->koeficijent != 1 && currentPosition->eksponent > 0)
  118.                 printf("%dx^%d", currentPosition->koeficijent, currentPosition->eksponent);
  119.             if (currentPosition->koeficijent < 0 && currentPosition->koeficijent != -1 && currentPosition->eksponent > 0)
  120.                 printf("-%dx^%d", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
  121.             if (currentPosition->koeficijent > 0 && currentPosition->koeficijent != 1 && currentPosition->eksponent < 0)
  122.                 printf("%dx^(%d)", currentPosition->koeficijent, currentPosition->eksponent);
  123.             if (currentPosition->koeficijent < 0 && currentPosition->koeficijent != -1 && currentPosition->eksponent < 0)
  124.                 printf("-%dx^(%d)", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
  125.             first = false;
  126.         }
  127.         else
  128.         {
  129.             if (currentPosition->koeficijent == 1 && currentPosition->eksponent == 0)
  130.                 printf(" + 1");
  131.             if (currentPosition->koeficijent == -1 && currentPosition->eksponent == 0)
  132.                 printf(" - 1");
  133.             if (currentPosition->koeficijent == 1 && currentPosition->eksponent > 0)
  134.                 printf(" + x^%d", currentPosition->eksponent);
  135.             if (currentPosition->koeficijent == 1 && currentPosition->eksponent < 0)
  136.                 printf(" + x^(%d)", currentPosition->eksponent);
  137.             if (currentPosition->koeficijent > 0 && currentPosition->eksponent > 0)
  138.                 printf(" + %dx^%d", currentPosition->koeficijent, currentPosition->eksponent);
  139.             if (currentPosition->koeficijent < 0 && currentPosition->eksponent > 0)
  140.                 printf(" - %dx^%d", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
  141.             if (currentPosition->koeficijent > 0 && currentPosition->eksponent < 0)
  142.                 printf(" + %dx^(%d)", currentPosition->koeficijent, currentPosition->eksponent);
  143.             if (currentPosition->koeficijent < 0 && currentPosition->eksponent < 0)
  144.                 printf(" - %dx^(%d)", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
  145.         }
  146.         currentPosition = currentPosition->next;
  147.     }
  148.     printf("\r\n");
  149. }
  150. int ZbrojiPolinome(Position pp, Position dp, Position sumaPolinoma)
  151. {
  152.     Position temp;
  153.     pp = pp->next;
  154.     dp = dp->next;
  155.     while (pp != NULL && dp != NULL)
  156.     {
  157.         if (pp->eksponent > dp->eksponent)
  158.         {
  159.             DodajUListuSortirano(sumaPolinoma, pp->koeficijent, pp->eksponent);
  160.             pp = pp->next;
  161.         }
  162.         else if (pp->eksponent < dp->eksponent)
  163.         {
  164.             DodajUListuSortirano(sumaPolinoma, dp->koeficijent, dp->eksponent);
  165.             dp = dp->next;
  166.         }
  167.         else
  168.         {
  169.             DodajUListuSortirano(sumaPolinoma, pp->koeficijent + dp->koeficijent, dp->eksponent);
  170.             pp = pp->next;
  171.             dp = dp->next;
  172.         }
  173.     }
  174.     if (pp == NULL) temp = dp;
  175.     else temp = pp;
  176.     while (temp != NULL)
  177.     {
  178.         DodajUListuSortirano(sumaPolinoma, temp->koeficijent, temp->eksponent);
  179.         temp = temp->next;
  180.     }
  181.     return 0;
  182.  
  183. }
  184. int PomnoziPolinome(Position pp, Position dp, Position umnozakPolinoma)
  185. {
  186.     Position pocetakD = dp->next;
  187.     pp = pp->next;
  188.     dp = dp->next;
  189.     while (pp != NULL)
  190.     {
  191.         while (dp != NULL)
  192.         {
  193.             DodajUListuSortirano(umnozakPolinoma, pp->koeficijent * dp->koeficijent, pp->eksponent + dp->eksponent);
  194.             dp = dp->next;
  195.         }
  196.         pp = pp->next;
  197.         dp = pocetakD;
  198.     }
  199.  
  200.     return 0;
  201. }
  202. void DealocirajMemoriju(Position trenutniCvor)
  203. {
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement