Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #define STACK struct stack
  5. #define STACK_M struct stack_m
  6.  
  7. const char *filenameout = "out.txt";
  8. const char *filenamein = "in.txt";
  9.  
  10. void writefile(const char* filename, char* content) // записывает в файл filename content
  11. {
  12. FILE *fout;
  13. fout = fopen(filename, "a");
  14. if (fout == NULL)
  15. {
  16. printf("file %s not opened",filenameout );
  17. return ;
  18. }
  19. if (content == "")
  20. fprintf(fout,"%s",content);
  21. else
  22. {
  23. fprintf(fout,"%s\n",content);
  24. fclose(fout);
  25. }
  26. }
  27.  
  28. int Checksintax(char* code) // проверка на синтаксические ошибки
  29. {
  30. if (code == NULL)
  31. return - 21;
  32. int flag1 = 0, i = 0, k = 0, j1 = 0, j2 = 0, len = strlen(code);
  33. for(i=0;i<len;i++)
  34. {
  35. if ((code[i]>='0')&&(code[i]<='9'))
  36. j1++;
  37.  
  38. if ((code[i] == '+')||(code[i] == '-')||(code[i] == '*')||(code[i] == '/'))
  39. {
  40. if(i == len-1)
  41. return -8;
  42. j2++;
  43. }
  44. if (code[i]=='(')
  45. {
  46. k++;
  47. if (code[i+1] == ')')
  48. return -5;
  49. }
  50. if (code[i]==')')
  51. {
  52. k--;
  53. if (code[i+1] == '(')
  54. return -9;
  55. }
  56. if (code[i]=='.')
  57. return -1;
  58. if (code[i]==' ')
  59. return -13;
  60. if ((code[i]>=65)&&(code[i]<=90))
  61. {
  62. flag1++;
  63. return -3;
  64. }
  65. if ((code[i]>=97)&&(code[i]<=122))
  66. {
  67. flag1++;
  68. return -4;
  69. }
  70. }
  71. if(flag1 > 0)
  72. return -20;
  73. if (j1 < j2+1)
  74. if (j2 != 0)
  75. return -65;
  76. if (k != 0)
  77. return -2;
  78. return 0;
  79. }
  80.  
  81. void writefileint(const char* filename, int content) // вывод результата
  82. {
  83. FILE *fout;
  84. fout = fopen(filename, "w");
  85. if (fout == NULL)
  86. {
  87. printf("file %s not opened",filename);
  88. return ;
  89. }
  90. fprintf(fout," %d",content);
  91. fclose(fout);
  92. }
  93.  
  94. STACK_M // лист содержащий указатель на голову стека
  95. {
  96. STACK* head;
  97. };
  98.  
  99. STACK // стэк
  100. {
  101. char data;
  102. STACK* next;
  103. };
  104.  
  105. void push(STACK_M* A, char *b) // новые эелменты стека
  106. {
  107. STACK* B;
  108. B = (STACK*)malloc(sizeof(STACK*));
  109. B->data = *b;
  110. B->next = A->head;
  111. A->head = B;
  112. }
  113. int tabl(char a) //приоритеты знаков
  114. {
  115. int b = -2;
  116. if (a == '(')
  117. b = 0;
  118. if (a == ')')
  119. b = 1;
  120. if ((a == '+')||(a == '-'))
  121. b = 2;
  122. if ((a == '*')||(a == '/'))
  123. b = 3;
  124. if (a == '^')
  125. b = 4;
  126. return b;
  127. }
  128.  
  129. void pop(STACK_M* A, char *b) // достаем значение из головы стека
  130. {
  131. STACK* B;
  132. B = A->head;
  133. *b = A->head->data;
  134. if (A->head->next)
  135. A->head = A->head->next;
  136. else
  137. A->head = NULL;
  138. free(B);
  139. }
  140.  
  141. void show(STACK_M* A) // выводит стек ( для отладки)
  142. {
  143. STACK* cur;
  144. cur = A->head;
  145. int i = 0;
  146. while(cur)
  147. {
  148. printf("\n data[%d] = %c",i,cur->data);
  149. cur = cur->next;
  150. i++;
  151. }
  152. printf("\n");
  153. }
  154.  
  155. char* charcat(char* code, char data) //аналог "strcat" только к строке добавляется 1 символ и пробел
  156. {
  157. int len = strlen(code);
  158. code[len]=data;
  159. code[len+1] = ' ';
  160. code[len+2] = '\0';
  161. return code;
  162. }
  163.  
  164. int tablstack(STACK_M*A) // приоритет знакак в стеке
  165. {
  166. STACK* cur;
  167. cur = A->head;
  168. char a;
  169. a = cur -> data;
  170. int tab;
  171. tab = tabl(a);
  172. return tab;
  173. }
  174.  
  175. int calc(char* code, int N,int* flag) // калькулятор
  176. {
  177. char* tmp;
  178. tmp = (char*)malloc(N*sizeof(char*));
  179. tmp[0] = '\0';
  180. int count = 0;
  181. int* stak;
  182. stak = (int*)malloc(N*sizeof(int*));
  183. int i = 0;
  184. int count11 = 0;
  185. int len = strlen(code);
  186. for (;;)
  187. {
  188. if ((code[i]>='0')&&(code[i]<='9'))
  189. {
  190. int g = 0;
  191. while (code[i]!=' ')
  192. {
  193. tmp[g] = code[i];
  194. tmp[g+1]='\0';
  195. g++;
  196. i++;
  197. }
  198. int celo = atoi(tmp);
  199. stak[count11] = celo;
  200. tmp[0]='\0';
  201. count11++;
  202. }
  203. else
  204. {
  205. if (code[i]=='+')
  206. {
  207. stak[count11-2]=stak[count11-1]+stak[count11-2];
  208. count11--;
  209. }
  210. if (code[i]=='-')
  211. {
  212. stak[count11-2]=stak[count11-2]-stak[count11-1];
  213. count11--;
  214. }
  215. if (code[i]=='*')
  216. {
  217. stak[count11-2]=stak[count11-1]*stak[count11-2];
  218. count11--;
  219. }
  220. if (code[i]=='/')
  221. {
  222. if (stak[count11-1] == 0)
  223. {
  224. writefile(filenameout,"division by zero");
  225. *flag = 0;
  226. return -1;
  227. }
  228. stak[count11-2]=stak[count11-2]/stak[count11-1];
  229. count11--;
  230. }
  231. i++;
  232. }
  233. if (i == len-1 )
  234. break;
  235. }
  236. return stak[0];
  237. }
  238.  
  239.  
  240. int main()
  241. {
  242. STACK_M* A;
  243. A = (STACK_M*)malloc(sizeof(STACK_M*));
  244. STACK* D;
  245. D = (STACK*)malloc(sizeof(STACK*));
  246. D = NULL;
  247. A->head = D;
  248. char* ex;
  249. ex = (char*)malloc(1000*sizeof(char*));
  250. ex = strcpy(ex,"");
  251. FILE *fin;
  252. fin = fopen(filenamein, "r");
  253. if (fin == NULL)
  254. {
  255. printf("file not opened");
  256. return -1;
  257. }
  258. char *code;
  259. code = (char*)malloc(2560*sizeof(char));
  260. if(code == NULL)
  261. return -2;
  262. fscanf(fin,"%[zxcvbnmasdfghjklqwertyuiopQWERTYUIOPLKJHGFDSAZXCVBNM1234567890+-*/ ().]",code);
  263. printf("cpde = %s\n",code);
  264. fclose(fin);
  265. int Check1;
  266. Check1 = Checksintax(code);
  267. printf("ch = %d",Check1);
  268. if (Check1 < 0)
  269. {
  270. writefile(filenameout,"syntax error");
  271. return 0;
  272. }
  273. int len = strlen(code);
  274. int i;
  275. for(i=0;i<len;i++) // перевод в обратную польскую запись
  276. {
  277. int tab = tabl(code[i]);
  278. if ((code[i]>='0')&&(code[i]<='9'))
  279. {
  280. while((code[i]>='0')&&(code[i]<='9'))
  281. {
  282. ex = charcat(ex,code[i]);
  283. int len1 = strlen(ex);
  284. ex[len1-1] = '\0';
  285. i++;
  286. }
  287. int len1 = strlen(ex);
  288. ex[len1] = ' ';
  289. ex[len1+1] = '\0';
  290. i--;
  291. }
  292. if (code[i] == '(')
  293. push(A,&code[i]);
  294. if (code[i] == ')')
  295. {
  296. char b=0;
  297. while (b != '(')
  298. {
  299. if (A->head->data != '(')
  300. {
  301. pop(A,&b);
  302. ex = charcat(ex,b);
  303. }
  304. else
  305. pop(A,&b);
  306. }
  307. }
  308. if (tab > 1)
  309. {
  310. int tab2;
  311. if((A->head) != NULL)
  312. {
  313. tab2 = tabl(A->head->data);
  314. while (tab2>=tab)
  315. {
  316. char a;
  317. pop(A,&a);
  318. ex = charcat(ex,a);
  319. if ((A->head) == NULL)
  320. break;
  321. tab2 = tabl(A->head->data);
  322. }
  323. }
  324. push(A,&code[i]);
  325. }
  326. if (i == len-1)
  327. {
  328. while((A->head) != NULL)
  329. {
  330. char a;
  331. pop(A,&a);
  332. ex = charcat(ex,a);
  333. }
  334. break;
  335. }
  336. }
  337. printf("\n ex = %s",ex);
  338. int res;
  339. int flag = 1;
  340. res = calc(ex,strlen(ex),&flag);
  341. printf("\n flag = %d",flag);
  342. printf("\n res = %d",res);
  343. if (flag == 1)
  344. writefileint(filenameout,res);
  345. return 0;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement