Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5.  
  6. #define BUFFER_SIZE 1024
  7. #define NUMBER_SIZE 32
  8. #define PATH_SIZE 1024
  9.  
  10. struct _Cvor;
  11. typedef struct _Cvor *Pos;
  12. typedef struct _Cvor
  13. {
  14. int El;
  15. Pos Next;
  16. }_CVOR;
  17.  
  18. int Push(Pos, int);
  19. int Pop(Pos, int *);
  20. int Calc_Posfix(char *, int *);
  21. int Calculate(int, int,char, int *);
  22. int ReadFromFile(char *, char *, int);
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26. int error_code = 0;
  27. int result = 0;
  28. char ime_dat[PATH_SIZE]={0};
  29.  
  30. printf(" %c", 201);
  31. for(int i = 0; i< 39; i++)
  32. printf("%c", 205);
  33. printf("%c", 187);
  34. printf("\n %c\t***\t Vjezba 7\t ***\t %c", 186,186);
  35. printf("\n %c\t*** Racunanje posfix izraza ***\t %c\n",186,186);
  36. printf(" %c", 200);
  37. for(int i = 0; i< 39; i++)
  38. printf("%c", 205);
  39. printf("%c", 188);
  40. printf("\n Unesite ime datoteke s posfix izrazom:\n\t\t");
  41. scanf_s(" %s", ime_dat, PATH_SIZE);
  42.  
  43. if(Calc_Posfix(ime_dat, &result) == 0)
  44. printf("\n\n\n\tRezultat je : %d\n\n", result);
  45. else
  46. printf("\n\n\nGreska u izrazu!\n\n");
  47. }
  48. int Calc_Posfix(char *ime_dat, int *result)
  49. {
  50. int error_code = 0;
  51. char *data = NULL;
  52. char *fdata = NULL;
  53. Pos head = NULL;
  54.  
  55. data = (char*)malloc(sizeof(char) * BUFFER_SIZE);
  56. if(data == NULL)
  57. {
  58. printf("\nGRESKA!!!\nMemorija nije alocirana!");
  59. error_code = -2;
  60. }
  61. else
  62. {
  63. fdata = data;
  64. memset(fdata, '\0', BUFFER_SIZE);
  65. if(!ReadFromFile(ime_dat, data, BUFFER_SIZE))
  66. {
  67. head=(Pos)malloc(sizeof(_CVOR));
  68. if(head == NULL)
  69. {
  70. printf("\nGRESKA!!!\nMemorija nije alocirana!");
  71. error_code = -1;
  72. }
  73. else
  74. {
  75. head->Next = NULL;
  76. char *buff;
  77. buff = (char*)malloc(sizeof(char) * NUMBER_SIZE);
  78. if(buff == NULL)
  79. {
  80. printf("\nGRESKA!!!\nMemorija nije alocirana!");
  81. error_code = -2;
  82. }
  83. else
  84. {
  85. memset(buff, '\0', NUMBER_SIZE);
  86. sscanf_s(fdata, " %s", buff, NUMBER_SIZE);
  87. do
  88. {
  89. if(isdigit(*buff))
  90. {//operand - Push
  91. char operand[NUMBER_SIZE] = {0};
  92. strcpy_s(operand,NUMBER_SIZE, buff);
  93. error_code = Push(head, atoi(operand));
  94. if(error_code != 0)
  95. break;
  96. }
  97. else
  98. {//operacija - Pop x2 AND Calculate AND Push
  99. int x = 0;
  100. int y = 0;
  101. int r = 0;
  102.  
  103. error_code = Pop(head,&y);
  104. if(error_code != 0)
  105. break;
  106. error_code = Pop(head,&x);
  107. if(error_code != 0)
  108. break;
  109. error_code = Calculate(x,y,*buff,&r);
  110. if(error_code != 0)
  111. break;
  112. error_code = Push(head, r);
  113. if(error_code != 0)
  114. break;
  115. }
  116. memset(buff, '\0', NUMBER_SIZE);
  117. if(strlen(fdata) > 0)
  118. {
  119. fdata = strchr(fdata+1, ' ');
  120. if(fdata != NULL)
  121. sscanf_s(fdata, "%s", buff, NUMBER_SIZE);
  122. }
  123. }while(strlen(buff) > 0);
  124. }
  125. free(buff);
  126. }
  127. }
  128. }
  129. printf("\n\n\t\tPosfix izraz :\n");
  130. printf("\t%s", data);
  131. free(data);
  132. if(error_code == 0)
  133. *result = head->Next->El;
  134. else
  135. *result = 0;
  136. free(head->Next);
  137. free(head);
  138. return error_code;
  139. }
  140. int Push(Pos P, int x)
  141. {
  142. int error_code = 0;
  143. Pos q;
  144.  
  145. q=(Pos)malloc(sizeof(_CVOR));
  146. if(q == 0)
  147. {
  148. printf("\nGRESKA!!!\nMemorija nije alocirana!");
  149. error_code = -1;
  150. }
  151. else
  152. {
  153. q->El = x;
  154. q->Next = P->Next;
  155. P->Next = q;
  156. }
  157. return error_code;
  158. }
  159. int Pop(Pos P, int *x)
  160. {
  161. int error_code = 0;
  162. Pos tmp;
  163.  
  164. if(P->Next == NULL)
  165. {
  166. printf("\nGRESKA!!!\nStog je prazan!");
  167. error_code = -1;
  168. }
  169. else
  170. {
  171. tmp = P->Next;
  172. P->Next = tmp->Next;
  173. *x = tmp->El;
  174. free(tmp);
  175. }
  176. return error_code;
  177. }
  178. int Calculate(int x, int y,char op, int *r)
  179. {
  180. int error_code = 0;
  181. switch(op)
  182. {
  183. case '+':
  184. *r = x+y;
  185. break;
  186. case '-':
  187. *r = x-y;
  188. break;
  189. case '*':
  190. *r = x*y;
  191. break;
  192. case '/':
  193. if(y == 0)
  194. {
  195. printf("\nGRESKA!!!\nDijeljenje s nulom!\n");
  196. error_code = -2;
  197. }
  198. else
  199. *r = x/y;
  200. break;
  201. default:
  202. printf("\nGRESKA!!!\nOperacija nije definirana!\n");
  203. error_code = -1;
  204. }
  205. return error_code;
  206. }
  207. int ReadFromFile(char *ime_dat, char *Data, int Len)
  208. {
  209. int error_code = 0;
  210. FILE *fp;
  211.  
  212. fopen_s(&fp, ime_dat, "r");
  213. if(fp == NULL)
  214. {
  215. printf("\nGRESKA!!!\nDatoteka %s nije otvorena!\n", ime_dat);
  216. error_code = -1;
  217. }
  218. else
  219. {
  220. if(0 >= fread(Data, sizeof(char), Len, fp))
  221. error_code = -2;
  222. fclose(fp);
  223. }
  224. return error_code;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement