Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <time.h>
  7.  
  8. int Check=0;
  9. struct element
  10. {
  11. double daneX;
  12. double daneY;
  13. struct element* poprzedni;
  14. };
  15. void push(struct element** stos, double x,double y)
  16. {
  17. struct element* nowy;
  18. nowy = (struct element*)malloc(1 * sizeof(*nowy));
  19. nowy->daneX = x;
  20. nowy->daneY = y;
  21. nowy->poprzedni = *stos;
  22. *stos = nowy;
  23. Check++;
  24. }
  25. void Czypusty()
  26. {
  27. if (Check ==0)
  28. {
  29. printf("Stos jest pusty\n");
  30. }
  31. else
  32. {
  33. printf("Stos nie jest pusty\n");
  34. }
  35. }
  36. double* pop(struct element** stos)
  37. {
  38. double x,y;
  39. double *tab;
  40. struct element* temp;
  41. tab =(double*) calloc(2, sizeof(tab));
  42. if (tab == NULL)
  43. {
  44. exit(1);
  45. }
  46.  
  47. if (Check == 0)
  48. {
  49. Czypusty();
  50. return 0;
  51. }
  52. else
  53. {
  54. x = (*stos)->daneX;
  55. y = (*stos)->daneY;
  56. temp = (*stos)->poprzedni;
  57. free(*stos);
  58. *stos = temp;
  59. Check--;
  60. tab[0] = x;
  61. tab[1] = y;
  62. return tab;
  63. }
  64. }
  65. void WyczyscStos(struct element** stos)
  66. {
  67. double* tab;
  68. tab =(double*) calloc(2, sizeof(tab));
  69. if (tab == NULL)
  70. {
  71. exit(1);
  72. }
  73. while (Check != 0)
  74. {
  75. tab = pop(stos);
  76. }
  77. }
  78. void WyswietlStos(struct element** stos)
  79. {
  80. if (*stos != NULL)
  81. {
  82. if ((*stos)->daneY < 0)
  83. {
  84. printf("%f %f'i'\n", (*stos)->daneX, (*stos)->daneY);
  85. }
  86. else
  87. {
  88. printf("%f '+' %f'i'\n", (*stos)->daneX, (*stos)->daneY);
  89. }
  90. WyswietlStos(&(*stos)->poprzedni);
  91. }
  92. }
  93. void Wczytywanie(struct element** stos, char **znaki)
  94. {
  95. double X;
  96. double Y;
  97. char tab[256];
  98. char znak;
  99. int loop=1;
  100. FILE* plik;
  101. gets(tab);
  102. plik=fopen("dane.txt","w+");
  103. if(plik==0)
  104. {
  105. exit(1);
  106. }
  107. fprintf(plik,"%s",tab);
  108. fclose(plik);
  109. plik=fopen("dane.txt","r");
  110. if(plik==0)
  111. {
  112. exit(1);
  113. }
  114. while(loop)
  115. {
  116. if(fscanf(plik,"%lf%lf",X,Y)!=2)
  117. {
  118. exit(1);
  119. }
  120. else
  121. {
  122. push(stos,X,Y);
  123. }
  124. znak=fgetc(plik);
  125. if(znak!=' ')
  126. {
  127. continue;
  128. }
  129. }
  130. }
  131. void Dodaj(struct element **stos)
  132. {
  133. double *taba;
  134. double *tabb;
  135. double x;
  136. double y;
  137. taba = pop(stos);
  138. tabb = pop(stos);
  139. x = taba[0] + tabb[0];
  140. y = taba[1] + tabb[1];
  141. push(stos, x, y);
  142. WyswietlStos(stos);
  143. }
  144. void Odejmij(struct element **stos)
  145. {
  146. double *taba;
  147. double *tabb;
  148. double x;
  149. double y;
  150. taba = pop(stos);
  151. tabb = pop(stos);
  152. x = tabb[0] - taba[0];
  153. y = tabb[1] - taba[1];
  154. push(stos, x, y);
  155. WyswietlStos(stos);
  156. }
  157. void Pomnoz(struct element **stos)
  158. {
  159. double *taba;
  160. double *tabb;
  161. double x;
  162. double y;
  163. taba = pop(stos);
  164. tabb = pop(stos);
  165. x = tabb[0] * taba[0] - taba[1] * tabb[1];
  166. y = tabb[0] * taba[1] + taba[0] * tabb[1];
  167. push(stos, x, y);
  168. WyswietlStos(stos);
  169. }
  170. void Podziel(struct element **stos)
  171. {
  172. double *taba;
  173. double *tabb;
  174. double x;
  175. double y;
  176. taba = pop(stos);
  177. tabb = pop(stos);
  178. x = (tabb[0] * taba[0] + taba[1] * tabb[1])/(taba[0]*taba[0]+taba[1]*taba[1]);
  179. y = (tabb[1]*taba[0]-tabb[0]*taba[1])/ (taba[0] * taba[0] + taba[1] * taba[1]);
  180. push(stos, x, y);
  181. WyswietlStos(stos);
  182. }
  183. void IleNaStosie()
  184. {
  185. printf("Na stosie jest %d elementów", Check);
  186. }
  187. int main()
  188. {
  189. struct element *stos = NULL;
  190. char* znaki;
  191. znaki =(char*) calloc(100, sizeof(znaki));
  192. setlocale(LC_ALL, "polish_poland");
  193. if (znaki == NULL)
  194. {
  195. exit(1);
  196. }
  197. Wczytywanie(&stos,&znaki);
  198. WyswietlStos(&stos);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement