Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- struct zespolona
- {
- float re;
- float im;
- };
- struct element
- {
- struct zespolona dane;
- struct element *poprzedni;
- };
- void push(struct element **element, float re, float im)
- {
- struct element *nowy;
- nowy = malloc(sizeof(struct element));
- if (nowy == NULL)
- {
- printf("Błąd! Nie udało się zaalokować pamięci na nowy element\n\n");
- return;
- }
- nowy->dane.re = re;
- nowy->dane.im = im;
- nowy->poprzedni = *element;
- *element = nowy;
- }
- struct zespolona pop(struct element **element)
- {
- struct zespolona tymczasowa;
- struct element *tymczasowy;
- tymczasowa = (*element)->dane;
- tymczasowy = (*element)->poprzedni;
- free(*element);
- *element = tymczasowy;
- return tymczasowa;
- }
- void drukuj()
- {
- printf("===] KALKULATOR LICZB ZESPOLONYCH [===\n\n");
- printf("Wprowadź liczbę w formacie RE IM, operator, lub X by wyjść z programu\n\n");
- }
- int main()
- {
- struct element *stos;
- struct zespolona a;
- struct zespolona b;
- struct zespolona wynik;
- struct zespolona wyczysc;
- char string[50];
- char znak;
- int w;
- float re;
- float im;
- setlocale(LC_ALL, "polish_poland");
- stos = malloc(sizeof(struct element));
- if (stos == NULL)
- {
- printf("Błąd! Nie udało się zaalokować pamięci\n\n");
- return;
- }
- stos->dane.re = 0;
- stos->dane.im = 0;
- stos->poprzedni = NULL;
- drukuj();
- while (1)
- {
- scanf("%[^\n]%*c", string);
- w = sscanf(string, "%f %f", &re, &im);
- if (w == 2) push(&stos, re, im);
- else if (w == 1) push(&stos, re, 0);
- else if (w == 0)
- {
- sscanf(string, "%c", &znak);
- if (znak == 'x' || znak == 'X')
- {
- while (stos->poprzedni != NULL) wyczysc = pop(&stos);
- break;
- }
- else
- {
- if (stos->poprzedni == NULL) printf("Błąd! Nie masz żadnego elementu na stosie\n\n");
- else
- {
- a = pop(&stos);
- if (stos->poprzedni == NULL)
- {
- printf("Błąd! Nie masz żadnego elementu na stosie\n\n");
- push(&stos, a.re, a.im);
- }
- else
- {
- b = pop(&stos);
- if (znak == '+')
- {
- wynik.re = a.re + b.re;
- wynik.im = a.im + b.im;
- push(&stos, wynik.re, wynik.im);
- }
- else if (znak == '-')
- {
- wynik.re = a.re - b.re;
- wynik.im = a.im - b.im;
- push(&stos, wynik.re, wynik.im);
- }
- else if (znak == '*')
- {
- wynik.re = (a.re*b.re - a.im*b.im);
- wynik.im = (a.re*b.im + b.re*a.im);
- push(&stos, wynik.re, wynik.im);
- }
- else if (znak == '/')
- {
- if ((b.re*b.re) + (b.im*b.im) != 0)
- {
- wynik.re = ((a.re*b.re) + (a.im*b.im)) / ((b.re*b.re) + (b.im*b.im));
- wynik.im = ((a.im*b.re) - (a.re*b.im)) / ((b.re*b.re) + (b.im*b.im));
- push(&stos, wynik.re, wynik.im);
- }
- else printf("Błąd! Nie dziel przez 0\n\n");
- }
- }
- }
- }
- }
- while (stos->poprzedni != NULL)
- {
- printf("%.2f%+.2f\n", stos->dane.re, stos->dane.im);
- wyczysc = pop(&stos);
- }
- }
- free(stos);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement