Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <locale.h>
  6.  
  7. struct zespolona
  8. {
  9.     float re;
  10.     float im;
  11. };
  12.  
  13. struct element
  14. {
  15.     struct zespolona dane;
  16.     struct element *poprzedni;
  17. };
  18.  
  19. void push(struct element **element, float re, float im)
  20. {
  21.     struct element *nowy;
  22.  
  23.     nowy = malloc(sizeof(struct element));
  24.  
  25.     if (nowy == NULL)
  26.     {
  27.         printf("Błąd! Nie udało się zaalokować pamięci na nowy element\n\n");
  28.         return;
  29.     }
  30.  
  31.     nowy->dane.re = re;
  32.     nowy->dane.im = im;
  33.     nowy->poprzedni = *element;
  34.  
  35.     *element = nowy;
  36. }
  37.  
  38. struct zespolona pop(struct element **element)
  39. {
  40.     struct zespolona tymczasowa;
  41.     struct element *tymczasowy;
  42.  
  43.     tymczasowa = (*element)->dane;
  44.     tymczasowy = (*element)->poprzedni;
  45.  
  46.     free(*element);
  47.  
  48.     *element = tymczasowy;
  49.     return tymczasowa;
  50. }
  51.  
  52. void drukuj()
  53. {
  54.     printf("===] KALKULATOR LICZB ZESPOLONYCH [===\n\n");
  55.     printf("Wprowadź liczbę w formacie RE IM, operator, lub X by wyjść z programu\n\n");
  56. }
  57.  
  58. int main()
  59. {
  60.     struct element *stos;
  61.  
  62.     struct zespolona a;
  63.     struct zespolona b;
  64.     struct zespolona wynik;
  65.     struct zespolona wyczysc;
  66.  
  67.     char string[50];
  68.     char znak;
  69.  
  70.     int w;
  71.  
  72.     float re;
  73.     float im;
  74.  
  75.     setlocale(LC_ALL, "polish_poland");
  76.  
  77.     stos = malloc(sizeof(struct element));
  78.  
  79.     if (stos == NULL)
  80.     {
  81.         printf("Błąd! Nie udało się zaalokować pamięci\n\n");
  82.         return;
  83.     }
  84.  
  85.     stos->dane.re = 0;
  86.     stos->dane.im = 0;
  87.     stos->poprzedni = NULL;
  88.  
  89.     drukuj();
  90.  
  91.     while (1)
  92.     {      
  93.         scanf("%[^\n]%*c", string);
  94.         w = sscanf(string, "%f %f", &re, &im);
  95.  
  96.         if (w == 2) push(&stos, re, im);
  97.        
  98.         else if (w == 1) push(&stos, re, 0);
  99.  
  100.         else if (w == 0)
  101.         {
  102.             sscanf(string, "%c", &znak);
  103.  
  104.             if (znak == 'x' || znak == 'X')
  105.             {
  106.                 while (stos->poprzedni != NULL) wyczysc = pop(&stos);
  107.                 break;
  108.             }
  109.  
  110.             else
  111.             {
  112.                 if (stos->poprzedni == NULL) printf("Błąd! Nie masz żadnego elementu na stosie\n\n");
  113.                
  114.                 else
  115.                 {
  116.                     a = pop(&stos);
  117.  
  118.                     if (stos->poprzedni == NULL)
  119.                     {
  120.                         printf("Błąd! Nie masz żadnego elementu na stosie\n\n");
  121.                         push(&stos, a.re, a.im);
  122.                     }
  123.  
  124.                     else
  125.                     {
  126.                         b = pop(&stos);
  127.  
  128.                         if (znak == '+')
  129.                         {
  130.                             wynik.re = a.re + b.re;
  131.                             wynik.im = a.im + b.im;
  132.  
  133.                             push(&stos, wynik.re, wynik.im);
  134.                         }
  135.  
  136.                         else if (znak == '-')
  137.                         {
  138.                             wynik.re = a.re - b.re;
  139.                             wynik.im = a.im - b.im;
  140.  
  141.                             push(&stos, wynik.re, wynik.im);
  142.                         }
  143.  
  144.                         else if (znak == '*')
  145.                         {
  146.                             wynik.re = (a.re*b.re - a.im*b.im);
  147.                             wynik.im = (a.re*b.im + b.re*a.im);
  148.  
  149.                             push(&stos, wynik.re, wynik.im);
  150.                         }
  151.  
  152.                         else if (znak == '/')
  153.                         {
  154.                             if ((b.re*b.re) + (b.im*b.im) != 0)
  155.                             {
  156.                                 wynik.re = ((a.re*b.re) + (a.im*b.im)) / ((b.re*b.re) + (b.im*b.im));
  157.                                 wynik.im = ((a.im*b.re) - (a.re*b.im)) / ((b.re*b.re) + (b.im*b.im));
  158.  
  159.                                 push(&stos, wynik.re, wynik.im);
  160.                             }
  161.                            
  162.                             else printf("Błąd! Nie dziel przez 0\n\n");
  163.                         }
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.  
  169.         while (stos->poprzedni != NULL)
  170.         {
  171.             printf("%.2f%+.2f\n", stos->dane.re, stos->dane.im);
  172.             wyczysc = pop(&stos);
  173.         }
  174.     }
  175.  
  176.     free(stos);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement