Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_POLINOM_SIZE (1024)
- typedef struct _cvor* Position;
- typedef struct _cvor
- {
- int eksponent;
- int koeficijent;
- Position next;
- }cvor;
- int ProcitajDatoteke(char* imeDatoteke, Position pp, Position dp);
- int UcitajPolinomIzStringa(char* ulazniString, Position trenutniCvor);
- int DodajUListuSortirano(Position trenutniCvor, int koeficijent, int eksponent);
- int KreirajCvorIDodajIzaTrenutnog(Position trenutniCvor, int koeficijent, int eksponent);
- int DodajIzaTrenutnog(Position noviCvor, Position trenutniCvor);
- void IspisiPolinom(char* opis, Position trenutniCvor);
- int ZbrojiPolinome(Position pp, Position dp, Position sumaPolinoma);
- int PomnoziPolinome(Position pp, Position dp, Position umnozakPolinoma);
- void DealocirajMemoriju(Position trenutniCvor);
- int main(int argc, char* argv[])
- {
- cvor pp, dp;
- cvor sumaPolinoma, umnozakPolinoma;
- pp.next = NULL;
- dp.next = NULL;
- sumaPolinoma.next = NULL;
- umnozakPolinoma.next = NULL;
- ProcitajDatoteke("polinomi.txt", &pp, &dp);
- IspisiPolinom("\nPrvi polinom", pp.next);
- IspisiPolinom("Drugi polinom", dp.next);
- ZbrojiPolinome(&pp, &dp, &sumaPolinoma);
- IspisiPolinom("Zbroj polinoma", sumaPolinoma.next);
- PomnoziPolinome(&pp, &dp, &umnozakPolinoma);
- IspisiPolinom("Umnozak polinoma", umnozakPolinoma.next);
- DealocirajMemoriju(&pp);
- DealocirajMemoriju(&dp);
- DealocirajMemoriju(&sumaPolinoma);
- DealocirajMemoriju(&umnozakPolinoma);
- return 0;
- }
- int ProcitajDatoteke(char* imeDatoteke, Position prviPolinom, Position drugiPolinom)
- {
- FILE* f;
- char buffer[MAX_POLINOM_SIZE];
- f = fopen(imeDatoteke, "r");
- fgets(buffer, MAX_POLINOM_SIZE, f);
- UcitajPolinomIzStringa(buffer, prviPolinom);
- fgets(buffer, MAX_POLINOM_SIZE, f);
- UcitajPolinomIzStringa(buffer, drugiPolinom);
- return 0;
- }
- int UcitajPolinomIzStringa(char* string, Position position)
- {
- int koef, pot, offset;
- while (sscanf(string, " %d %d %n", &koef, &pot, &offset) != EOF)
- {
- DodajUListuSortirano(position, koef, pot);
- string += offset;
- }
- return 0;
- }
- int DodajUListuSortirano(Position position, int koeficijent, int eksponent)
- {
- while (position->next != NULL && position->next->eksponent > eksponent)
- position = position->next;
- if (position->next != NULL && position->next->eksponent == eksponent)
- position->next->koeficijent += koeficijent;
- else
- KreirajCvorIDodajIzaTrenutnog(position, koeficijent, eksponent);
- return 0;
- }
- int KreirajCvorIDodajIzaTrenutnog(Position currentPosition, int koeficijent, int eksponent)
- {
- Position q;
- q = (Position)malloc(sizeof(cvor));
- q->eksponent = eksponent;
- q->koeficijent = koeficijent;
- DodajIzaTrenutnog(q, currentPosition);
- return 0;
- }
- int DodajIzaTrenutnog(Position noviCvor, Position currentPosition)
- {
- noviCvor->next = currentPosition->next;
- currentPosition->next = noviCvor;
- return 0;
- }
- void IspisiPolinom(char* title, Position currentPosition)
- {
- bool first = true;
- printf("%s=", title);
- while (currentPosition != NULL)
- {
- if (first == true)
- {
- if (currentPosition->koeficijent == 1 && currentPosition->eksponent == 0)
- printf("1");
- if (currentPosition->koeficijent == -1 && currentPosition->eksponent == 0)
- printf("- 1");
- if (currentPosition->koeficijent == 1 && currentPosition->eksponent > 0)
- printf("x^%d", currentPosition->eksponent);
- if (currentPosition->koeficijent == 1 && currentPosition->eksponent < 0)
- printf("x^(%d)", currentPosition->eksponent);
- if (currentPosition->koeficijent > 0 && currentPosition->koeficijent != 1 && currentPosition->eksponent > 0)
- printf("%dx^%d", currentPosition->koeficijent, currentPosition->eksponent);
- if (currentPosition->koeficijent < 0 && currentPosition->koeficijent != -1 && currentPosition->eksponent > 0)
- printf("-%dx^%d", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
- if (currentPosition->koeficijent > 0 && currentPosition->koeficijent != 1 && currentPosition->eksponent < 0)
- printf("%dx^(%d)", currentPosition->koeficijent, currentPosition->eksponent);
- if (currentPosition->koeficijent < 0 && currentPosition->koeficijent != -1 && currentPosition->eksponent < 0)
- printf("-%dx^(%d)", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
- first = false;
- }
- else
- {
- if (currentPosition->koeficijent == 1 && currentPosition->eksponent == 0)
- printf(" + 1");
- if (currentPosition->koeficijent == -1 && currentPosition->eksponent == 0)
- printf(" - 1");
- if (currentPosition->koeficijent == 1 && currentPosition->eksponent > 0)
- printf(" + x^%d", currentPosition->eksponent);
- if (currentPosition->koeficijent == 1 && currentPosition->eksponent < 0)
- printf(" + x^(%d)", currentPosition->eksponent);
- if (currentPosition->koeficijent > 0 && currentPosition->eksponent > 0)
- printf(" + %dx^%d", currentPosition->koeficijent, currentPosition->eksponent);
- if (currentPosition->koeficijent < 0 && currentPosition->eksponent > 0)
- printf(" - %dx^%d", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
- if (currentPosition->koeficijent > 0 && currentPosition->eksponent < 0)
- printf(" + %dx^(%d)", currentPosition->koeficijent, currentPosition->eksponent);
- if (currentPosition->koeficijent < 0 && currentPosition->eksponent < 0)
- printf(" - %dx^(%d)", (-1) * currentPosition->koeficijent, currentPosition->eksponent);
- }
- currentPosition = currentPosition->next;
- }
- printf("\r\n");
- }
- int ZbrojiPolinome(Position pp, Position dp, Position sumaPolinoma)
- {
- Position temp;
- pp = pp->next;
- dp = dp->next;
- while (pp != NULL && dp != NULL)
- {
- if (pp->eksponent > dp->eksponent)
- {
- DodajUListuSortirano(sumaPolinoma, pp->koeficijent, pp->eksponent);
- pp = pp->next;
- }
- else if (pp->eksponent < dp->eksponent)
- {
- DodajUListuSortirano(sumaPolinoma, dp->koeficijent, dp->eksponent);
- dp = dp->next;
- }
- else
- {
- DodajUListuSortirano(sumaPolinoma, pp->koeficijent + dp->koeficijent, dp->eksponent);
- pp = pp->next;
- dp = dp->next;
- }
- }
- if (pp == NULL) temp = dp;
- else temp = pp;
- while (temp != NULL)
- {
- DodajUListuSortirano(sumaPolinoma, temp->koeficijent, temp->eksponent);
- temp = temp->next;
- }
- return 0;
- }
- int PomnoziPolinome(Position pp, Position dp, Position umnozakPolinoma)
- {
- Position pocetakD = dp->next;
- pp = pp->next;
- dp = dp->next;
- while (pp != NULL)
- {
- while (dp != NULL)
- {
- DodajUListuSortirano(umnozakPolinoma, pp->koeficijent * dp->koeficijent, pp->eksponent + dp->eksponent);
- dp = dp->next;
- }
- pp = pp->next;
- dp = pocetakD;
- }
- return 0;
- }
- void DealocirajMemoriju(Position trenutniCvor)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement