# malo programiranja u c-u

#### dopas

##### Banned
ako nekome tribaju gotovi kodovu za vježbi iz c evo im mala pomoæ

#### dopas

##### Banned
4. Napisati program za zbrajanje i množenje polinoma. Iz datoteke se èitaju koeficijenti složeni prema rastuæim eksponentima i maksimalni eksponent je 10 (implementacija s nizovima). Iz datoteke se èitaju koeficijenti i eksponenti (implementacija s vezanim listama).
# include<stdio.h>
#include<stdlib.h>
#define maximum 10
struct Poli;
typedef struct Poli * Polynom;

struct Poli
{
int Koef;
int Exp;
Polynom Next;
};
void citanje(Polynom, char *);
void ispis(Polynom);
void zbrajanje(Polynom, Polynom, Polynom);
void mnozenje(Polynom, Polynom, Polynom);

void main()
{
struct Poli P1, P2, S, P;
P1.Next = NULL;
P2.Next = NULL;
S.Next = NULL;
P.Next = NULL;
citanje(&P1, "P1.txt");
ispis(&P1);

citanje(&P2, "P2.txt");
ispis(&P2);

zbrajanje(P1.Next, P2.Next, &S);
printf("\n\nSuma je:");
ispis(&S);

mnozenje(P1.Next, P2.Next, &P);
printf("\n\nProdukt je:");
ispis(&P);

printf("\n");}
void citanje(Polynom P, char *ime_dat)
{
FILE *dat;
Polynom q, temp, prev;
dat = fopen(ime_dat, "r");
if (NULL == dat)
{
printf("Greska u otvaranju datoteke!!!");
exit(1);
}
else
{
while (0 == feof(dat))
{
q = (Polynom)malloc(sizeof(struct Poli));
fscanf(dat, " %d %d", &q->Koef, &q->Exp);
temp = P->Next;
prev = P;
while(temp != NULL && temp->Exp > q->Exp )
{
prev = temp;
temp = temp->Next;
}
prev->Next = q;
if (temp != NULL)
q->Next = temp;
else
q->Next = NULL;
}}}
void ispis(Polynom P)
{
printf("\n");
P = P->Next;
while(P != NULL)
{
if(P->Koef > 0)
printf("\t+%d*x^%d", P->Koef, P->Exp);
else
printf("\t%d*x^%d", P->Koef, P->Exp);
P = P->Next;
}
printf("\n");

}
void zbrajanje(Polynom P1, Polynom P2, Polynom S)
{
Polynom q, temp;
while(P1 != NULL && P2 != NULL)
{
q = (Polynom)malloc(sizeof(struct Poli));
q->Next = NULL;
if(P1->Exp > P2->Exp)
{
q->Exp = P1->Exp;
q->Koef = P1->Koef;
P1 = P1->Next;
}
else if(P1->Exp < P2->Exp)
{
q->Exp = P2->Exp;
q->Koef = P2->Koef;
P2 = P2->Next;
}
else // P1->Exp == P2->Exp
{
q->Exp = P1->Exp;
q->Koef = P1->Koef + P2->Koef;
P1 = P1->Next;
P2 = P2->Next;
}
S->Next = q;
S = q;
}
if(P1 != NULL)
temp = P1;
else
temp = P2;

while (temp != NULL)
{
q = (Polynom)malloc(sizeof(struct Poli));
q->Next = NULL;
q->Exp = temp->Exp;
q->Koef = temp->Koef;
S->Next = q;
S = q;
temp = temp->Next;
}}
void mnozenje(Polynom P1, Polynom P2, Polynom P)
{
Polynom q, first = P1, second = P2, temp, prev;
int n;
while (first != NULL)
{
second = P2;
while (second != NULL)
{
temp = P->Next;
prev = P;
n = first->Exp + second->Exp;
while(temp != NULL && temp->Exp > n)
{
prev = temp;
temp = temp->Next;
}
if (temp != NULL && temp->Exp == n)
temp->Koef += first->Koef * second->Koef;
else
{
q = (Polynom)malloc(sizeof(struct Poli));
q->Exp = n;
q->Koef = first->Koef* second->Koef;
prev->Next = q;
q->Next = temp;
}
second = second->Next;
}
first = first->Next;
}
while(P->Next != NULL)
{
if(P->Next->Koef == 0)
{
temp = P->Next;
P->Next = P->Next->Next;
free(temp);
}
P = P->Next;
}}

#### dopas

##### Banned
5. Napisati program koji simulira rad stoga pomoæu:
a) Niza,
#include<stdio.h>
#include<stdlib.h>
struct StackRecord
{
int Capacity;
int TopOfStack;
int *Array;
};
void Push(struct StackRecord *);
void Pop(struct StackRecord *);
void main()
{
struct StackRecord Stack;
int a=0;
while(Stack.Capacity<=0)
{
printf("\n Upisite kapacitet stoga (>0): ");
scanf(" %d", &Stack.Capacity);
}
Stack.Array = (int *)malloc(sizeof(int)*Stack.Capacity);
Stack.TopOfStack = 0;
while (a != 3)
{
printf("\n Unesi:" );
printf("\n\t 1 - Push");
printf("\n\t 2 - Pop");
printf("\n\t 3 - Kraj\n\t");
scanf(" %d", &a);
switch(a)
{
case 1:
Push (&Stack);
break;
case 2:
Pop (&Stack);
break;
case 3:
printf("\nKraj!\n");
break;
default:
printf("\nPogresan unos\n");
}}}
void Push(struct StackRecord *S)
{
if(S->TopOfStack == S->Capacity)
printf("Stog je pun");
else
{
printf("\n\tUnesi element: ");
scanf(" %d", &S->Array[S->TopOfStack]);
S->TopOfStack ++;
}}
void Pop(struct StackRecord *S)
{
if(S->TopOfStack == 0)
printf("Stog je prazan");
else
{
S->TopOfStack --;
printf("\n\tSa stoga se skida: %d\n", S->Array[S->TopOfStack]);
}}
b) Jednostruko vezanih listi.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h >
struct StackRecord
{
int El;
struct StackRecord * Next;
};
void Push(struct StackRecord *);
void Pop(struct StackRecord *);
void main()
{
struct StackRecord head = {0, NULL};
char a=0;
while (a != 27)
{
printf("\n Unesi:" );
printf("\n\t 1 - Push");
printf("\n\t 2 - Pop");
printf("\n\t ESC - Kraj\n\n\t");
a = getch();
switch(a)
{
case '1':
break;
case '2':
break;
case 27:
printf("\nKraj!\n");
break;
default:
printf("\nPogresan unos\n");
}}}
void Push(struct StackRecord *S)
{
struct StackRecord *q;
q = (struct StackRecord *)malloc(sizeof(struct StackRecord));
if( q==NULL)
printf("\nGreska kod alokacije memorije!!");
else
{
printf("\n\tUnesi element: ");
scanf(" %d", &q->El);
q->Next = S->Next;
S->Next = q;
}}
void Pop(struct StackRecord *S)
{
struct StackRecord *q;
if(NULL == S->Next)
printf("\nStog je prazan\n");
else
{
printf("\n\tSa stoga se skida: %d\n", S->Next->El);
q = S->Next;
S->Next = q->Next;
free(q);
}}

#### dopas

##### Banned
6. Napisati program koji simulira rad reda pomoæu:
a) Niza (red mora biti cirkularan),
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

struct _red{
int kapacitet;
int broj;
int glava;
int rep;
int *polje;};
typedef struct _red red;
void StvoriRed(red*);
void Stavi(red*);
void Ispis(red*);
void main (){
int izbor;
red *struktura = (red *)malloc(sizeof(red));
while(1){
printf("\n\nIzbornik:\n");
printf("\n\t1 - Za stvaranje reda");
printf("\n\t2 - Za stavljanje elementa u red");
printf("\n\t3 - Za vadenje elementa iz reda");
printf("\n\t4 - Za ispis reda");
printf("\n\t0 - Za izlaz");
printf("\n\tIzbor: ");
scanf_s("%d", &izbor);

switch (izbor){
case 0: exit(1); break;
case 1: StvoriRed(struktura); break;
case 2: Stavi(struktura); break;
case 3: Izvadi(struktura); break;
case 4: Ispis(struktura); break;
}}}
void StvoriRed(red *struktura){
int kap;
printf("Unesi broj elemenata polja: ");
scanf_s("%d", &kap);
struktura->polje = (int *)malloc(sizeof(int)*kap);
struktura->kapacitet = kap;
struktura->broj = 0;
struktura->glava = 0;
struktura->rep = 0;
}
void Stavi(red *struktura){
if(struktura->kapacitet == struktura->broj)
printf("Red je pun!\n");
else{
printf("Unesi broj: ");
scanf("%d",&struktura->polje[struktura->glava]);
struktura->broj++;
if(struktura->glava >= struktura->kapacitet-1)
struktura->glava = 0;
else
struktura->glava++;
}}
if(struktura->broj == 0)
printf("Red je prazan!\n");
else{
printf("Izvadeni element: %d\n", struktura->polje[struktura->rep]);
struktura->broj--;
if(struktura->rep >= struktura->kapacitet-1)
struktura->rep = 0;
else
struktura->rep++;
}}
void Ispis(red *struktura){
if (struktura->broj == 0){
printf("Red je prazan!\n");
}
else{
int g = struktura->glava, r = struktura->rep;

do{
printf(" %d", struktura->polje[r]);
if(r >= struktura->kapacitet-1)
r = 0;
else
r++;
}while(r != g); }}

b) Jednostruko vezanih listi.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

struct _element{
int value;
struct _element *next;
};
typedef struct _element element;
struct _red{
element *glava;
element *rep;
};
typedef struct _red red;
void Stavi(red*);
void Ispis(red*);
void main (){
int izbor;
red *struktura = (red *)malloc(sizeof(red));
struktura->glava = NULL;
struktura->rep = NULL;
while(1){
printf("\n\nIzbornik:\n");
printf("\n\t1 - Za stavljanje elementa u red");
printf("\n\t2 - Za vadenje elementa iz reda");
printf("\n\t3 - Za ispis reda");
printf("\n\t0 - Za izlaz");
printf("\n\tIzbor: ");
scanf_s("%d", &izbor);

switch (izbor){
case 0: exit(1); break;
case 1: Stavi(struktura); break;
case 2: Izvadi(struktura); break;
case 3: Ispis(struktura); break;
}}}
void Stavi(red *struktura){
element *temp = (element*)malloc(sizeof(element));
temp->next = NULL;
printf("Unesi vrijednost elementa: ");
scanf_s("%d",&temp->value);

if((struktura->glava == NULL)&&(struktura->rep == NULL)){
struktura->rep = temp;
struktura->glava = temp;
}
else{
struktura->glava->next = temp;
struktura->glava = temp;
}}
if((struktura->glava == NULL)&&(struktura->rep == NULL))
printf("Red je prazan!\n");
else{
printf("Izvadeni element je: %d\n", struktura->rep->value);
element *temp = struktura->rep;
if(struktura->glava == struktura->rep){
struktura->glava = NULL;
struktura->rep = NULL;
}
else
struktura->rep = struktura->rep->next;
free(temp);
}}
void Ispis(red *struktura){
if((struktura->glava == NULL)&&(struktura->rep == NULL))
printf("Red je prazan!\n");
else{
element *temp = struktura->rep;
while(1){
printf(" %d",temp->value);
if(temp->next != NULL)
temp = temp->next;
else
break;
}}}

#### dopas

##### Banned
7. Za dvije sortirane liste L1 i L2 (mogu se proèitati iz datoteke ili unijeti ruèno, bitno je samo da su sortirane), napisati program koji stvara novu vezanu listu tako da raèuna:
a) L1L2,
b) L1 L2.
Liste osim pokazivaèa na slijedeæu strukturu imaju i jedan cjelobrojni element, po kojem su sortirane.
#include<stdio.h>
#include<stdlib.h>

struct list
{
int Element;
struct list *Next;
};
void ReadList(struct list *, char *);
void PrintList(struct list *); //funkcije
void Unija(struct list *, struct list *, struct list *);
void Presjek(struct list *, struct list *, struct list *);

void main()
{
struct list L1, L2, U, P;

L1.Next = NULL;
L2.Next = NULL;
U.Next = NULL;
P.Next = NULL;

PrintList(&L1);

PrintList(&L2);

printf("\n \nUnija je: \n");
Unija(L1.Next, L2.Next, &U);
PrintList(&U);

printf("\n \nPresjek je: \n");
Presjek(L1.Next, L2.Next, &P);
PrintList(&P);
}
void ReadList(struct list *P, char *ime_dat)
{
FILE *dat;
struct list *q, *temp, *prev;
dat = fopen(ime_dat, "r");
if (NULL == dat)
{
printf("Greska u otvaranju datoteke!!!");
exit(1);
}
else
{
while (0 == feof(dat))
{
q = (struct list *)malloc(sizeof(struct list));
fscanf(dat, " %d", &q->Element);
temp = P->Next;
prev = P;
while(temp != NULL && temp->Element < q->Element )
{
prev = temp;
temp = temp->Next;
}
prev->Next = q;
if (temp != NULL)
q->Next = temp;
else
q->Next = NULL;
}}}
void PrintList(struct list *P)
{
printf("\n");
P = P->Next;
while(P != NULL)
{
printf("\t%d", P->Element);
P = P->Next;
}
printf("\n");}

void Unija(struct list *L1, struct list *L2, struct list *U)
{
struct list *q, *p;
while (L1 && L2)
{
q = (struct list *)malloc(sizeof(struct list));
U->Next = q;
q->Next = NULL;
if(L1->Element < L2->Element)
{
q->Element = L1->Element;
L1 = L1->Next;
}
else if(L2->Element < L1->Element)
{
q->Element = L2->Element;
L2 = L2->Next;
}
else
{
q->Element = L1->Element;
L1 = L1->Next;
L2 = L2->Next;
}
U = U->Next;
}
if(L1)
p = L1;
else
p = L2;
while(p)
{
q = (struct list *)malloc(sizeof(struct list));
U->Next = q;
q->Next = NULL;
q->Element = p->Element;
U = U->Next;
p = p->Next;
}}

void Presjek(struct list *L1, struct list *L2, struct list *P)
{
struct list *q, *p;
while (L1 && L2)
{
if(L1->Element < L2->Element)
L1 = L1->Next;
else if(L2->Element < L1->Element)
L2 = L2->Next;
else
{
q = (struct list *)malloc(sizeof(struct list));//alokacija
P->Next = q;
q->Next = NULL;
q->Element = L1->Element;
L1 = L1->Next;
L2 = L2->Next;
P = P->Next;
}}}

#### dopas

##### Banned
8. Napisati program koji omoguæava rad s binarnim stablom pretraživanja. Treba omoguæiti unošenje novog elementa u stablo, ispis elemenata, brisanje i pronalaženje nekog elementa.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct TreeNode;
typedef struct TreeNode * Node;
Node MakeEmpty(Node);
Node Find(int, Node);
Node FindMin(Node);
Node FindMax(Node);
Node Insert(int, Node);
Node Delete(int, Node);
void Print(Node);
struct TreeNode
{
int Element;
Node Left;
Node Right;
};
void main()
{
Node root = NULL, temp;
int odabir = 0, X;
while (odabir != 9)
{
system("cls");
printf("\n\tOdaberi akciju:");
printf("\n\t\t 1 - unos novog elementa");
printf("\n\t\t 2 - ispis stabla");
printf("\n\t\t 3 - trazenje elementa");
printf("\n\t\t 4 - brisanje elementa");
printf("\n\t\t 5 - trazenje min. elementa");
printf("\n\t\t 6 - trazenje max. elementa");
printf("\n\t\t 7 - brisanje stabla");
printf("\n\t\t 9 - kraj\n\t");
scanf(" %d", &odabir);
switch(odabir)
{
case 1:
printf("\n\tUnesi element: ");
scanf(" %d", &X);
root = Insert(X, root);
break;
case 2:
system("cls");
Print(root);
break;
case 3:
printf("\n\tKoji element trazis? ");
scanf(" %d", &X);
temp = Find(X, root);
if (temp != NULL)
printf ("Element %d se nalazi na adresi %d", temp->Element, temp);
break;
case 4:
printf("\n\tKoji element zelis izbrisat? ");
scanf(" %d", &X);
root = Delete(X, root);
break;
case 5:
temp = FindMin(root);
printf("Minimalni element je %d", temp->Element);
break;
case 6:
temp = FindMax(root);
printf("Maximalni element je %d", temp->Element);
break;
case 7:
root = MakeEmpty(root);
case 9:
break;
default:
printf("\n\tKrivi unos!!!");
}
printf("\nPress any key to continue!");
getch();
} }
Node Find(int X, Node T)
{
if (NULL == T)
{
printf("\n\tElement ne postoji!!");
return NULL;
}
else if(X < T->Element)
return Find(X, T->Left);
else if (X > T->Element)
return Find(X, T->Right);
return T;
}
Node FindMin(Node T)
{
while(T->Left != NULL)
T = T->Left;
return T;
}
Node FindMax(Node T)
{
while(T->Right != NULL)
T = T->Right;
return T;
}
Node Insert(int X, Node T)
{
if(NULL == T)
{
T = (Node)malloc(sizeof(struct TreeNode));
T->Left = NULL;
T->Right = NULL;
T->Element = X;
}
else if(X > T->Element)
T->Right = Insert(X, T->Right);
else if(X < T->Element)
T->Left = Insert(X, T->Left);
return T;
}
Node Delete(int X, Node T)
{
Node temp;
if(NULL == T)
printf("Nema ga!!!");
else if(X < T->Element)
T->Left = Delete(X, T->Left);
else if (X > T->Element)
T->Right = Delete(X, T->Right);
else
{
if(T->Left && T->Right)
{
temp = FindMin(T->Right);
T->Element = temp->Element;
T->Right = Delete(T->Element, T->Right);
}
else
{
temp = T;
if(NULL == T->Left)
{
T = T->Right;
}
else if(NULL == T->Right)
{
T = T->Left;
}
free(temp);
} }
return T;}
void Print(Node T)
{
static int n = 0;
int i;
if(-1 == n)
n = 0;
if(NULL != T)
{
n++;
Print(T->Right);
printf("\n");
for(i = 0; i < n; i++)
printf("\t");
printf(" %d", T->Element);
printf("\n");
n++;
Print(T->Left);
}
n--;}
Node MakeEmpty(Node T)
{
if(NULL != T)
{
MakeEmpty(T->Left);
MakeEmpty(T->Right);
free(T);
}
return NULL;}

#### Dzi

Staff member
Super. dobro si se to sjetio. budem i ja potrazio ak imam svoje programe sejvano jos. Kada pišeš post na napredni naèin meðu ikonama za ureðivanje ti je ikona CODE koju možeš koristiti za pejstanje koda. Onda ti se kod ne filtrira i može se lakše copy paste

#### dopas

##### Banned
sorry šta ovako kasno odgovaram ali jedno vrijeme nisam bio aktivan
a fala za taj CODE nisam zna pa ubuduæe æu vodit raèuna o tome

Thread starter Similar threads Forum Replies Date
Zabava 4
WWW 0
Kafe Bar 5