Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. //Biblioteki
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include<locale.h>
  6. #include<conio.h>
  7. #include<ctype.h>
  8. //Prototypy Funkcji
  9. struct stos *Pop(struct stos *gora,int *suma,struct zespolone *);
  10. struct stos *Push(struct stos* gora,int*,struct zespolone );
  11. void wypisz(struct stos*,int);
  12. struct stos *usun(struct stos*,int*);
  13. struct stos *wczytaj(double,double);
  14. struct stos * dzialanie(struct stos *gora,char* ,int*);
  15. //Stos
  16. struct zespolone
  17. {
  18. double Re;
  19. double Im;
  20. };
  21. struct stos
  22. {
  23. struct zespolone dane;
  24. struct stos *poprzedni;
  25. };
  26.  
  27. struct stos *Push(struct stos* gora,int*suma,struct zespolone liczba)
  28. {
  29. struct stos *nowy;
  30. nowy=(struct stos *)malloc(sizeof(struct stos));
  31. gora=(struct stos*)realloc(gora,(*suma+1)*sizeof(struct stos));
  32. nowy->dane=liczba;
  33. nowy->poprzedni=gora;
  34. gora=nowy;
  35. nowy=NULL;
  36. free(nowy);
  37. *suma++;
  38. return gora;
  39.  
  40. }
  41. struct stos *Pop(struct stos *gora,int *suma,struct zespolone *bufor)
  42. {
  43. struct stos *nowy=(struct stos*)malloc(sizeof(struct stos));
  44. *bufor=gora->dane;
  45. nowy=gora->poprzedni;
  46. gora=NULL;
  47. free(gora);
  48. gora=(struct stos*)realloc(gora,(*suma-1)*sizeof(struct stos));
  49. gora=nowy;
  50. nowy=NULL;
  51. free(nowy);
  52. *suma--;
  53. return gora;
  54. }
  55.  
  56.  
  57. void wypisz(struct stos *gora,int suma)
  58. {
  59. if(suma==0)
  60. {
  61. printf("Nie ma czego wyswietlic.\n");
  62. _getch();
  63. }
  64. while(suma>0)
  65. {
  66.  
  67. printf("%d.Liczba zespolona:%lf i%lf \n",suma,gora->dane.Re,gora->dane.Im);
  68. gora=gora->poprzedni;
  69. suma--;
  70.  
  71. }
  72. }
  73.  
  74.  
  75. struct stos* usun(struct stos* pointer,int *suma)
  76. {
  77. pointer=NULL;
  78. free(pointer);
  79. *suma=0;
  80. return pointer;
  81. }
  82.  
  83. main()
  84. {
  85. struct zespolone liczba;
  86. int Ile_stosu=0;
  87. int bufor=0;
  88. char czy_sp;
  89. char tab[100]={0};
  90. struct stos*gora;
  91. gora=(struct stos*)malloc(sizeof(struct stos));
  92.  
  93. while(1)
  94. {
  95. tu:
  96. system("cls");
  97. printf(" >>>>>>KALKULATOR LICZB ZESPOLONYCH<<<<<<\nDostepne opcje:\n 1.Dzialania na kalkulatorze)\n 2.'Usun'-sciaga liczbe ze stosu\n 3.'Wyswietl'-wyswietla wszystkie wyniki\n 4.'Koniec'-konczy dzialanie kalkulatora\n");
  98. printf("Jezeli chcesz obliczac,wpisuj po prostu liczby Re i Im oddzielone spacja.\n");
  99. if(Ile_stosu!=0)
  100. wypisz(gora,Ile_stosu);
  101. gets(tab);
  102. //Opcje dodatkowe-poza obliczaniem
  103. if(strstr(tab,"Koniec")){
  104. printf("Koniec programu");
  105. _getch();
  106. break;
  107. }
  108.  
  109.  
  110. if(strstr(tab,"Usun")&&Ile_stosu>0)// sprawdz czy uzytkownik chce opcje nr.2
  111. {
  112. printf("Usuniete zostana te liczby zespolone:\n");
  113. wypisz(gora,Ile_stosu);
  114. _getch();
  115. usun(gora,&Ile_stosu);
  116. }
  117. else if(strstr(tab,"Usun")&&Ile_stosu==0)
  118. {
  119. printf("Nic się nie znajduje na stosie\n");
  120. getchar();
  121. }
  122.  
  123. if(strstr(tab,"Wyswietl"))
  124. {
  125. wypisz(gora,Ile_stosu);
  126.  
  127. }
  128.  
  129. //Operacje na liczbach
  130. if(*tab=='+' || *tab=='-' || *tab=='*' ||*tab=='/')
  131. {
  132. if(Ile_stosu==0)
  133. {
  134. printf("Najpierw wrzuc liczby na stos\n");
  135. }
  136. if(Ile_stosu==1)
  137. {
  138. printf("Muszą być conajmniej dwie liczby\n");
  139. }
  140.  
  141. else
  142. {
  143.  
  144. gora=dzialanie(gora,tab,&Ile_stosu);
  145. }
  146. }
  147. if(czy_sp=strchr(tab,32)!=NULL)
  148. {
  149. sscanf(tab,"%lf %lf",&liczba.Re,&liczba.Im);
  150. gora=Push(gora,&Ile_stosu,liczba);
  151. Ile_stosu++;
  152. }
  153.  
  154. if(czy_sp=strchr(tab,32)==NULL && (!strstr(tab,"Usun") && !strstr(tab,"Wyswietl"))) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TU SIE JEBIE A NIE POWINNO!!!!!!
  155. {
  156. sscanf(tab,"%lf",&liczba.Re);
  157.  
  158. liczba.Re=atof(tab);
  159. liczba.Im=0;
  160. gora=Push(gora,&Ile_stosu,liczba);
  161. Ile_stosu++;
  162.  
  163. }
  164.  
  165. }
  166. return 0;
  167.  
  168. }
  169.  
  170. //funkcje arytmetyczne
  171. struct stos* dzialanie(struct stos*gora,char tab[],int*sumar)
  172. {
  173. struct zespolone wynik,*pierwsza_liczba=0,*druga_liczba=0;
  174. pierwsza_liczba=(struct zespolone*)malloc(sizeof(struct zespolone));
  175. druga_liczba=(struct zespolone*)malloc(sizeof(struct zespolone));
  176. gora=Pop(gora,sumar,pierwsza_liczba);
  177. gora=Pop(gora,sumar,druga_liczba);
  178. //gora=NULL;
  179. if(*tab=='+')
  180. {
  181. wynik.Re=pierwsza_liczba->Re+druga_liczba->Re;
  182. wynik.Im=pierwsza_liczba->Im+druga_liczba->Im;
  183. }
  184. else if(*tab=='-')
  185. {
  186. wynik.Re=pierwsza_liczba->Re-druga_liczba->Re;
  187. wynik.Im=pierwsza_liczba->Im-druga_liczba->Im;
  188. }
  189.  
  190. else if(*tab=='/')
  191. {
  192. if(druga_liczba==0)printf("Ni da sie dzielic przez zirol\n");
  193. }else
  194. {
  195. wynik.Re=pierwsza_liczba->Re/druga_liczba->Re;
  196. wynik.Im=pierwsza_liczba->Im/druga_liczba->Im;
  197. }
  198.  
  199.  
  200. if(*tab=='*')
  201. {
  202. wynik.Re=pierwsza_liczba->Re*druga_liczba->Re;
  203. wynik.Im=pierwsza_liczba->Im*druga_liczba->Im;
  204. }
  205. gora=Push(gora,sumar,wynik);
  206. (*sumar)++;
  207. pierwsza_liczba=NULL;
  208. druga_liczba=NULL;
  209. free(pierwsza_liczba);
  210. free(druga_liczba);
  211. return gora;
  212. }
  213.  
  214.  
  215. void czy_pusty(int suma_stosu){
  216. if(suma_stosu==0)
  217. printf("Stos jest pusty\n");
  218. else
  219. printf("Stos nie jest pusty\n");
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement