Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- #include <math.h>
- #include <string.h>
- #include <time.h>
- int Check=0;
- struct element
- {
- double daneX;
- double daneY;
- struct element* poprzedni;
- };
- void push(struct element** stos, double x,double y)
- {
- struct element* nowy;
- nowy = (struct element*)malloc(1 * sizeof(*nowy));
- nowy->daneX = x;
- nowy->daneY = y;
- nowy->poprzedni = *stos;
- *stos = nowy;
- Check++;
- }
- void Czypusty()
- {
- if (Check ==0)
- {
- printf("Stos jest pusty\n");
- }
- else
- {
- printf("Stos nie jest pusty\n");
- }
- }
- double* pop(struct element** stos)
- {
- double x,y;
- double *tab;
- struct element* temp;
- tab =(double*) calloc(2, sizeof(tab));
- if (tab == NULL)
- {
- exit(1);
- }
- if (Check == 0)
- {
- Czypusty();
- return 0;
- }
- else
- {
- x = (*stos)->daneX;
- y = (*stos)->daneY;
- temp = (*stos)->poprzedni;
- free(*stos);
- *stos = temp;
- Check--;
- tab[0] = x;
- tab[1] = y;
- return tab;
- }
- }
- void WyczyscStos(struct element** stos)
- {
- double* tab;
- tab =(double*) calloc(2, sizeof(tab));
- if (tab == NULL)
- {
- exit(1);
- }
- while (Check != 0)
- {
- tab = pop(stos);
- }
- }
- void WyswietlStos(struct element** stos)
- {
- if (*stos != NULL)
- {
- if ((*stos)->daneY < 0)
- {
- printf("%f %f'i'\n", (*stos)->daneX, (*stos)->daneY);
- }
- else
- {
- printf("%f '+' %f'i'\n", (*stos)->daneX, (*stos)->daneY);
- }
- WyswietlStos(&(*stos)->poprzedni);
- }
- }
- void Wczytywanie(struct element** stos, char **znaki)
- {
- double X;
- double Y;
- char tab[256];
- char znak;
- int loop=1;
- FILE* plik;
- gets(tab);
- plik=fopen("dane.txt","w+");
- if(plik==0)
- {
- exit(1);
- }
- fprintf(plik,"%s",tab);
- fclose(plik);
- plik=fopen("dane.txt","r");
- if(plik==0)
- {
- exit(1);
- }
- while(loop)
- {
- if(fscanf(plik,"%lf%lf",X,Y)!=2)
- {
- exit(1);
- }
- else
- {
- push(stos,X,Y);
- }
- znak=fgetc(plik);
- if(znak!=' ')
- {
- continue;
- }
- }
- }
- void Dodaj(struct element **stos)
- {
- double *taba;
- double *tabb;
- double x;
- double y;
- taba = pop(stos);
- tabb = pop(stos);
- x = taba[0] + tabb[0];
- y = taba[1] + tabb[1];
- push(stos, x, y);
- WyswietlStos(stos);
- }
- void Odejmij(struct element **stos)
- {
- double *taba;
- double *tabb;
- double x;
- double y;
- taba = pop(stos);
- tabb = pop(stos);
- x = tabb[0] - taba[0];
- y = tabb[1] - taba[1];
- push(stos, x, y);
- WyswietlStos(stos);
- }
- void Pomnoz(struct element **stos)
- {
- double *taba;
- double *tabb;
- double x;
- double y;
- taba = pop(stos);
- tabb = pop(stos);
- x = tabb[0] * taba[0] - taba[1] * tabb[1];
- y = tabb[0] * taba[1] + taba[0] * tabb[1];
- push(stos, x, y);
- WyswietlStos(stos);
- }
- void Podziel(struct element **stos)
- {
- double *taba;
- double *tabb;
- double x;
- double y;
- taba = pop(stos);
- tabb = pop(stos);
- x = (tabb[0] * taba[0] + taba[1] * tabb[1])/(taba[0]*taba[0]+taba[1]*taba[1]);
- y = (tabb[1]*taba[0]-tabb[0]*taba[1])/ (taba[0] * taba[0] + taba[1] * taba[1]);
- push(stos, x, y);
- WyswietlStos(stos);
- }
- void IleNaStosie()
- {
- printf("Na stosie jest %d elementów", Check);
- }
- int main()
- {
- struct element *stos = NULL;
- char* znaki;
- znaki =(char*) calloc(100, sizeof(znaki));
- setlocale(LC_ALL, "polish_poland");
- if (znaki == NULL)
- {
- exit(1);
- }
- Wczytywanie(&stos,&znaki);
- WyswietlStos(&stos);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement