Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <winbgim.h>
  3. #include <cmath>
  4. #include <windows.h>
  5. #include <conio.h>
  6. #include "graphics.h"
  7. #include <stdio.h>
  8. #include <mmsystem.h>
  9. #include <string.h>
  10. #include <locale>
  11. #include <string>
  12. #include <cstdio>
  13. #include <stdlib.h>
  14.  
  15. using namespace std;
  16.  
  17. ///ecuatia postfixata
  18. #define BLANK ' '
  19. #define TAB '\t'
  20.  
  21. using namespace std;
  22. void scrie();
  23. void push(long int symbol);
  24. long int pop();
  25. void infix_to_postfix();
  26. long int eval_post();
  27. int priority(char symbol);
  28. int isEmpty();
  29. int white_space(char);
  30. char infix[100]; char postfix[100];
  31. long int stack[100];
  32. int top;
  33.  
  34. int nr_elem;
  35.  
  36. enum limbi { RO, EN, FR };
  37. limbi limba = RO;
  38.  
  39. const int h = getmaxheight();
  40. const int w = getmaxwidth();
  41.  
  42. char ec_postfixata[100][100];
  43.  
  44.  
  45. ///nr de nivele
  46. bool operand(char op[])
  47. {
  48. return !(strcmp(op, "+") == 0 || strcmp(op, "-") == 0 || strcmp(op, "*") == 0 || strcmp(op, "/") == 0);
  49. }
  50.  
  51. int nivelMaxim(char ec_postfixata[100][100], int nr_elem)
  52. {
  53. int nivel[256] = { 0 };
  54. int k = 0;
  55. for (int i = 1; i <= nr_elem; i++)
  56. if (operand(ec_postfixata[i]))
  57. nivel[++k] = 1;
  58. else
  59. {
  60. k--;
  61. nivel[k] = 1 + max(nivel[k], nivel[k + 1]);
  62. }
  63. return nivel[1];
  64. }
  65.  
  66. int nr_nivele = nivelMaxim(ec_postfixata, nr_elem);
  67.  
  68. ///nr de caractere a unui element
  69. int nr_digituri[100];
  70.  
  71. void digitNumar(char postfix[100], int nr_digituri[100], int& nr)
  72. {
  73. int k = 0;
  74. int i;
  75. for (i = 0; i < strlen(infix); i++)
  76. {
  77. if (infix[i] >= '0' && infix[i] <= '9')
  78. nr_digituri[k]++;
  79. else
  80. k++;
  81. }
  82. nr = k;
  83. }
  84.  
  85.  
  86. void transformaEcuatiaPostfixataInMatrice(char postfix[100])
  87. {
  88. for (int i = 0; i < nr_elem; i++)
  89. {
  90. int aux = nr_digituri[i];
  91. for (int j = 0; j < aux; j++)
  92. {
  93. ec_postfixata[i][j] = postfix[j];
  94. }
  95. int ok = 0;
  96. while (ok != aux)
  97. {
  98. for (int j = 0; j < nr_elem - 1; j++)
  99. postfix[j] = postfix[j + 1];
  100. ok++;
  101. }
  102.  
  103.  
  104. }
  105. }
  106. int i;
  107.  
  108. void paginaPrincipala(int w, int h);
  109.  
  110. void optiuni();
  111.  
  112. int culoare1 = RED;
  113. int culoare2 = YELLOW;
  114.  
  115. void schimbaPagOptiuni()
  116. {
  117. setbkcolor(BLACK);
  118. cleardevice();
  119. setcolor(LIGHTMAGENTA);
  120.  
  121. }
  122. void deseneazaNod(int x, int y, int r, string s, int dim)
  123. {
  124. setcolor(culoare2);
  125. setfillstyle(SOLID_FILL, culoare1);
  126. fillellipse(x, y, r, r);
  127. settextstyle(EUROPEAN_FONT, HORIZ_DIR, dim);
  128. setbkcolor(culoare1);
  129. // Crede-ma pe cuvant, murim incet altfel. Nu tb sa faci nimic la el, doar o sa mearga.
  130. char ptCaChar[s.length()];
  131. strcpy(ptCaChar, s.c_str());
  132. outtextxy(x - r / 2, y - r / 2, ptCaChar);
  133.  
  134. }
  135. void deschideNouaPagina(int w, int h)
  136. {
  137. setbkcolor(BLACK);
  138. cleardevice();
  139. readimagefile("Capture.JPG", 0, 0, w, h);
  140. }
  141.  
  142.  
  143. void butonIntermediarInapoi(int h)
  144. {
  145. setcolor(LIGHTGREEN);
  146. setbkcolor(GREEN);
  147. settextstyle(3, HORIZ_DIR, 27);
  148. if (limba == 0)
  149. outtextxy(100, h - 100, "INAPOI");
  150. else if (limba == 1)
  151. outtextxy(100, h - 100, "BACK");
  152. else if (limba == 2)
  153. outtextxy(100, h - 100, "RETOUR");
  154. }
  155.  
  156. void butonIntermediarAfisare(int w, int h)
  157. {
  158. setcolor(RED);
  159. setbkcolor(YELLOW);
  160. settextstyle(3, HORIZ_DIR, 27);
  161. if (limba == 0)
  162. outtextxy(w - 320, h - 100, "REZULTAT");
  163. else if (limba == 1)
  164. outtextxy(w - 320, h - 100, "RESULT");
  165. else if (limba == 2)
  166. outtextxy(w - 320, h - 100, "RESULTAT");
  167.  
  168. }
  169.  
  170. void schimbaCulori(int w, int h)
  171. {
  172. schimbaPagOptiuni();
  173.  
  174. settextstyle(4, HORIZ_DIR, 7);
  175. if (limba == 0)
  176. outtextxy(w / 2 - 200, 100, "CULORI");
  177. else if (limba == 1)
  178. outtextxy(w / 2 - 200, 100, "COLORS");
  179. else if (limba == 2)
  180. outtextxy(w / 2 - 200, 100, "COULEURS");
  181.  
  182. settextstyle(6, 0, 4);
  183. setcolor(WHITE);
  184. if (limba == 0)
  185. outtextxy(200, 400, "CULOARE NOD");
  186. else if (limba == 1)
  187. outtextxy(200, 400, "NODE COLOR");
  188. else if (limba == 2)
  189. outtextxy(200, 400, "COULEUR DU NOEUD");
  190.  
  191. setfillstyle(SOLID_FILL, COLOR(179, 255, 242));
  192. fillellipse(650, 400, 30, 30);
  193. setfillstyle(SOLID_FILL, COLOR(255, 255, 77));
  194. fillellipse(750, 400, 30, 30);
  195. setfillstyle(SOLID_FILL, COLOR(255, 153, 153));
  196. fillellipse(850, 400, 30, 30);
  197. setfillstyle(SOLID_FILL, COLOR(255, 166, 77));
  198. fillellipse(950, 400, 30, 30);
  199. setfillstyle(SOLID_FILL, COLOR(255, 153, 238));
  200. fillellipse(1050, 400, 30, 30);
  201.  
  202.  
  203.  
  204. if (limba == 0)
  205. outtextxy(200, 600, "CULOARE SCRIS");
  206. else if (limba == 1)
  207. outtextxy(200, 600, "WRITING COLOR");
  208. else if (limba == 2)
  209. outtextxy(200, 600, "COULEUR D'ECRITURE");
  210.  
  211. setfillstyle(SOLID_FILL, COLOR(102, 0, 153));
  212. fillellipse(650, 600, 30, 30);
  213. setfillstyle(SOLID_FILL, COLOR(26, 0, 153));
  214. fillellipse(750, 600, 30, 30);
  215. setfillstyle(SOLID_FILL, COLOR(128, 0, 85));
  216. fillellipse(850, 600, 30, 30);
  217. setfillstyle(SOLID_FILL, COLOR(9, 51, 0));
  218. fillellipse(950, 600, 30, 30);
  219. setfillstyle(SOLID_FILL, COLOR(102, 0, 0));
  220. fillellipse(1050, 600, 30, 30);
  221.  
  222.  
  223. butonIntermediarInapoi(h);
  224.  
  225. int ok = 0, x, y;
  226. while (ok == 0)
  227. {
  228. getmouseclick(WM_LBUTTONDOWN, x, y);
  229. if (getpixel(x, y) == GREEN || getpixel(x, y) == LIGHTGREEN)
  230. {
  231. ok = 1;
  232. optiuni();
  233.  
  234. }
  235. else if (getpixel(x, y) == COLOR(179, 255, 242) || getpixel(x, y) == COLOR(255, 255, 77) || getpixel(x, y) == COLOR(255, 153, 153) || getpixel(x, y) == COLOR(255, 166, 77) || getpixel(x, y) == COLOR(255, 153, 238))
  236. {
  237. ok = 1;
  238. culoare1 = getpixel(x, y);
  239. schimbaCulori(w, h);
  240. }
  241. else if (getpixel(x, y) == COLOR(102, 0, 153) || getpixel(x, y) == COLOR(26, 0, 153) || getpixel(x, y) == COLOR(128, 0, 85) || getpixel(x, y) == COLOR(9, 51, 0) || getpixel(x, y) == COLOR(102, 0, 0))
  242. {
  243. ok = 1;
  244. culoare2 = getpixel(x, y);
  245. schimbaCulori(w, h);
  246. }
  247.  
  248.  
  249.  
  250. }
  251. }
  252. void schimbalimba(int w, int h)
  253. {
  254. schimbaPagOptiuni();
  255.  
  256. settextstyle(4, HORIZ_DIR, 7);
  257. if (limba == 0)
  258. outtextxy(w / 2 - 150, 100, "LIMBA");
  259. else if (limba == 1)
  260. outtextxy(w / 2 - 150, 100, "LANGUAGE");
  261. else if (limba == 2)
  262. outtextxy(w / 2 - 150, 100, "LANGUE");
  263.  
  264. readimagefile("romania_0012.JPG", w / 2 - 350, h / 2 - 50, w / 2 - 200, h / 2 + 50);
  265. readimagefile("Flag_of_the_United_Kingdom.JPG", w / 2 - 100, h / 2 - 50, w / 2 + 50, h / 2 + 50);
  266. readimagefile("64619037drapeau-reunion-png.JPG", w / 2 + 150, h / 2 - 50, w / 2 + 300, h / 2 + 50);
  267.  
  268. butonIntermediarInapoi(h);
  269. int ok = 0, x, y;
  270. while (ok == 0)
  271. {
  272. getmouseclick(WM_LBUTTONDOWN, x, y);
  273. if (getpixel(x, y) == LIGHTGREEN || getpixel(x, y) == GREEN)
  274. {
  275. ok = 1;
  276. optiuni();
  277.  
  278. }
  279. else if (x > w / 2 - 350 && x< w / 2 - 200 && y>h / 2 - 50 && y < h / 2 + 50)
  280. {
  281. ok = 1;
  282. limba = RO;
  283. deschideNouaPagina(w, h);
  284. paginaPrincipala(w, h);
  285. }
  286. else if (x > w / 2 - 100 && x<w / 2 + 50 && y>h / 2 - 50 && y < h / 2 + 50)
  287. {
  288. ok = 1;
  289. limba = EN;
  290. deschideNouaPagina(w, h);
  291. paginaPrincipala(w, h);
  292. }
  293. else if (x > w / 2 + 150 && x<w / 2 + 300 && y>h / 2 - 50 && y < h / 2 + 50)
  294. {
  295. ok = 1;
  296. limba = FR;
  297. deschideNouaPagina(w, h);
  298. paginaPrincipala(w, h);
  299. }
  300.  
  301.  
  302. }
  303. }
  304.  
  305. void scrie()
  306. {
  307. infix[256] = { 0 };
  308. char tasta;
  309. int k = 0;
  310.  
  311. do
  312. {
  313. tasta = getch();
  314.  
  315. if (tasta == 8) /// tasta == backspace
  316. {
  317. infix[strlen(infix) - 1] = NULL;
  318. bar(w / 2 - 500, h / 2 - 50, w / 2 + 500, h / 2 + 50);
  319. k--;
  320.  
  321. }
  322.  
  323. else infix[k++] = tasta;
  324.  
  325. setcolor(BLACK);
  326. setbkcolor(WHITE);
  327. settextstyle(SIMPLEX_FONT, HORIZ_DIR, 3);
  328. settextjustify(CENTER_TEXT, CENTER_TEXT);
  329. outtextxy(w / 2, h / 2, infix);
  330.  
  331. } while (tasta != 27);
  332.  
  333. /// tasta != ESC
  334. }
  335.  
  336. void optiuni()
  337. {
  338. schimbaPagOptiuni();
  339.  
  340. settextstyle(4, HORIZ_DIR, 7);
  341. if (limba == 0)
  342. outtextxy(w / 2 - 200, 100, "OPTIUNI");
  343. else if (limba == 1)
  344. outtextxy(w / 2 - 200, 100, "OPTIONS");
  345. else if (limba == 2)
  346. outtextxy(w / 2 - 200, 100, "OPTIONS");
  347.  
  348.  
  349. settextstyle(6, 0, 4);
  350. setbkcolor(COLOR(0, 0, 1));
  351. setcolor(COLOR(255, 255, 255));
  352.  
  353. if (limba == 0)
  354. {
  355. outtextxy(250, 250, "CULORI");
  356. setbkcolor(COLOR(0, 0, 0));
  357. setcolor(WHITE);
  358.  
  359. outtextxy(300, 350, "CULOARE NOD:");
  360. setfillstyle(SOLID_FILL, culoare1);
  361. fillellipse(800, 360, 30, 30);
  362.  
  363. outtextxy(300, 450, "CULOARE SCRIS:");
  364. setfillstyle(SOLID_FILL, culoare2);
  365. fillellipse(800, 460, 30, 30);
  366.  
  367. setbkcolor(COLOR(0, 1, 0));
  368. setcolor(COLOR(255, 254, 255));
  369. outtextxy(250, 550, "LIMBA");
  370. }
  371.  
  372. else if (limba == 1)
  373. {
  374. outtextxy(250, 250, "COLORS");
  375. setbkcolor(COLOR(0, 0, 0));
  376. setcolor(WHITE);
  377.  
  378. outtextxy(300, 350, "NODE COLOR:");
  379. setfillstyle(SOLID_FILL, culoare1);
  380. fillellipse(800, 360, 30, 30);
  381.  
  382. outtextxy(300, 400, "WRITING COLOR:");
  383. setfillstyle(SOLID_FILL, culoare2);
  384. fillellipse(800, 460, 30, 30);
  385.  
  386. setbkcolor(COLOR(0, 1, 0));
  387. setcolor(COLOR(255, 254, 255));
  388. outtextxy(250, 550, "LANGUAGE");
  389. }
  390.  
  391. else if (limba == 2)
  392. {
  393. outtextxy(250, 250, "COULEURS");
  394. setbkcolor(COLOR(0, 0, 0));
  395. setcolor(WHITE);
  396.  
  397. outtextxy(300, 350, "COULEUR DU NOEUD:");
  398. setfillstyle(SOLID_FILL, culoare1);
  399. fillellipse(800, 360, 30, 30);
  400.  
  401. outtextxy(300, 400, "COULEUR D'ECRITURE:");
  402. setfillstyle(SOLID_FILL, culoare2);
  403. fillellipse(800, 460, 30, 30);
  404.  
  405. setbkcolor(COLOR(0, 1, 0));
  406. setcolor(COLOR(255, 254, 255));
  407. outtextxy(250, 550, "LANGUE");
  408. }
  409.  
  410.  
  411. butonIntermediarInapoi(h);
  412.  
  413. int ok = 0, x, y;
  414. while (ok == 0)
  415. {
  416. getmouseclick(WM_LBUTTONDOWN, x, y);
  417. if (getpixel(x, y) == LIGHTGREEN || getpixel(x, y) == GREEN)
  418. {
  419. ok = 1;
  420.  
  421. deschideNouaPagina(w, h);
  422. paginaPrincipala(w, h);
  423.  
  424.  
  425. }
  426. else if (getpixel(x, y) == COLOR(0, 0, 1) || getpixel(x, y) == COLOR(255, 255, 255))
  427. {
  428. ok = 1;
  429. schimbaCulori(w, h);
  430.  
  431. }
  432. else if (getpixel(x, y) == COLOR(0, 1, 0) || getpixel(x, y) == COLOR(255, 254, 255))
  433. {
  434. ok = 1;
  435. schimbalimba(w, h);
  436. }
  437.  
  438.  
  439. }
  440.  
  441.  
  442. }
  443.  
  444. void arbore()
  445. {
  446. schimbaPagOptiuni();
  447.  
  448. butonIntermediarAfisare(w, h);
  449. butonIntermediarInapoi(h);
  450.  
  451. setcolor(LIGHTRED);
  452. setbkcolor(BLUE);
  453.  
  454. if (limba == 0)
  455. outtextxy(100, 100, "LA FINAL APASATI ESC");
  456. else if (limba == 1)
  457. outtextxy(100, 100, "AT THE END PRESS ESC");
  458. else if (limba == 2)
  459. outtextxy(100, 100, "AU FINAL CLICK ESC");
  460.  
  461. setcolor(WHITE);
  462. setbkcolor(WHITE);
  463. bar(w / 2 - 500, h / 2 - 50, w / 2 + 500, h / 2 + 50);
  464. scrie();
  465.  
  466.  
  467. int ok = 0;
  468. int x2, y2;
  469. while (ok == 0)
  470. {
  471. getmouseclick(WM_LBUTTONDOWN, x2, y2);
  472. if (getpixel(x2, y2) == GREEN || getpixel(x2, y2) == LIGHTGREEN)
  473. {
  474. ok = 1;
  475. deschideNouaPagina(w, h);
  476. paginaPrincipala(w, h);
  477. }
  478. else if (getpixel(x2, y2) == RED || getpixel(x2, y2) == YELLOW)
  479. {
  480. ok = 1;
  481. schimbaPagOptiuni();
  482. setbkcolor(WHITE);
  483.  
  484. int dimensiune = 5;
  485. int r = 30;
  486. int x = w / 2;
  487. int ynou;
  488. int u[100];
  489. int y = h / nr_nivele;
  490. string valNod = "+";
  491. deseneazaNod(x, y, r, valNod, dimensiune);
  492.  
  493. ynou = y + h / nr_nivele;
  494.  
  495. ///la fiecare niv se incrementeaza y cu h impartit la nr de nivele pt ca s -a impartit ecranul intr0un nr de nivele
  496.  
  497. int xs = x - x / 2;
  498. int xd = x + x / 2;
  499.  
  500. line(x - r, y, xs, ynou - r);
  501. line(x + r, y, xd, ynou - r);
  502.  
  503.  
  504.  
  505. for (i = 2; i <= nr_nivele; i++)
  506. {
  507. y = ynou;
  508.  
  509. ynou = ynou + h / nr_nivele;
  510.  
  511. int j = 1;
  512.  
  513. int coordonataX = x / (pow(2, i - 1));
  514.  
  515. while (j < i * i)
  516. {
  517. deseneazaNod(coordonataX, y, r, valNod, dimensiune);
  518. if (i != nr_nivele)
  519. {
  520. if (coordonataX < x / 2)
  521. {
  522.  
  523. line(coordonataX - r, y, coordonataX - coordonataX / 2, ynou - r);
  524. line(coordonataX + r, y, coordonataX + coordonataX / 2, ynou - r);
  525.  
  526.  
  527. }
  528. else
  529. {
  530. line(coordonataX - r, y, coordonataX - x / (2 * pow(2, i - 1)), ynou - r);
  531. line(coordonataX + r, y, coordonataX + x / (2 * pow(2, i - 1)), ynou - r);
  532. }
  533.  
  534. }
  535. coordonataX = coordonataX + 2 * x / pow(2, i - 1);
  536. j = j + 2;
  537. }
  538.  
  539.  
  540. }
  541.  
  542. }
  543. }
  544. }
  545.  
  546.  
  547. void nivelNod()
  548. {
  549. schimbaPagOptiuni();
  550.  
  551. butonIntermediarAfisare(w, h);
  552. butonIntermediarInapoi(h);
  553.  
  554. setcolor(LIGHTRED);
  555. setbkcolor(BLUE);
  556. if (limba == 0)
  557. outtextxy(100, 100, "LA FINAL APASATI ESC");
  558. else if (limba == 1)
  559. outtextxy(100, 100, "AT THE END PRESS ESC");
  560. else if (limba == 2)
  561. outtextxy(100, 100, "AU FINAL CLICK ESC");
  562.  
  563. setcolor(BLACK);
  564. setbkcolor(WHITE);
  565. bar(w / 2 - 500, h / 2 - 50, w / 2 + 500, h / 2 + 50);
  566. scrie();
  567.  
  568. int ok = 0, x, y;
  569. while (ok == 0)
  570. {
  571. getmouseclick(WM_LBUTTONDOWN, x, y);
  572.  
  573. if (getpixel(x, y) == LIGHTGREEN || getpixel(x, y) == GREEN)
  574. {
  575. ok = 1;
  576. deschideNouaPagina(w, h);
  577. paginaPrincipala(w, h);
  578. }
  579. else if (getpixel(x, y) == RED || getpixel(x, y) == YELLOW)
  580. {
  581. ok = 1;
  582. schimbaPagOptiuni();
  583. setbkcolor(WHITE);
  584. infix_to_postfix();
  585.  
  586.  
  587. ///int rezultat=getLevel(root,infix);
  588. ///char s[100];
  589. /// itoa(rezultat,s,10);
  590. /// outtextxy(w/2,h/2,s);
  591.  
  592. }
  593.  
  594.  
  595. }
  596.  
  597. }
  598.  
  599. void paginaPrincipala(int w, int h)
  600. {
  601. setcolor(COLOR(255, 3, 3));
  602. setbkcolor(COLOR(255, 238, 3));
  603. settextstyle(1, HORIZ_DIR, 28);
  604.  
  605. if (limba == 0)
  606. outtextxy(w / 2 - 400, h / 2 - 300, "ARBORELE ECUATIEI");
  607. else if (limba == 1)
  608. outtextxy(w / 2 - 240, h / 2 - 300, "BINARY TREE");
  609. else if (limba == 2)
  610. outtextxy(w / 2 - 300, h / 2 - 300, "ARBRE BINAIRE");
  611.  
  612. setcolor(COLOR(254, 3, 3));
  613. setbkcolor(COLOR(254, 238, 3));
  614. settextstyle(1, HORIZ_DIR, 28);
  615.  
  616. if (limba == 0)
  617. outtextxy(w / 2 - 350, h / 2, "NIVELUL NODULUI");
  618. else if (limba == 1)
  619. outtextxy(w / 2 - 250, h / 2, "NODE LEVEL");
  620. else if (limba == 2)
  621. outtextxy(w / 2 - 350, h / 2, "NIVEAU DU NOEUD");
  622.  
  623.  
  624. setcolor(COLOR(255, 4, 3));
  625. setbkcolor(COLOR(255, 237, 3));
  626. settextstyle(1, HORIZ_DIR, 28);
  627.  
  628. if (limba == 0)
  629. outtextxy(w / 2 - 150, h / 2 + 300, "OPTIUNI");
  630. else if (limba == 1)
  631. outtextxy(w / 2 - 150, h / 2 + 300, "OPTIONS");
  632. else if (limba == 2)
  633. outtextxy(w / 2 - 150, h / 2 + 300, "OPTIONS");
  634.  
  635. int xx, yy;
  636. int okk = 0;
  637. while (okk == 0)
  638. {
  639. getmouseclick(WM_LBUTTONDOWN, xx, yy);
  640. if (getpixel(xx, yy) == COLOR(255, 3, 3) || getpixel(xx, yy) == COLOR(255, 238, 3))
  641. {
  642. okk = 1;
  643. arbore();
  644. }
  645. else if (getpixel(xx, yy) == COLOR(255, 237, 3) || getpixel(xx, yy) == COLOR(255, 4, 3))
  646. {
  647. okk = 1;
  648. optiuni();
  649.  
  650. }
  651. else if (getpixel(xx, yy) == COLOR(254, 3, 3) || getpixel(xx, yy) == COLOR(254, 238, 3))
  652. {
  653. okk = 1;
  654. nivelNod();
  655. }
  656.  
  657. }
  658.  
  659. }
  660.  
  661.  
  662. int main()
  663. {
  664. initwindow(w, h);
  665. readimagefile("Capture.JPG", 0, 0, w, h);
  666. paginaPrincipala(w, h);
  667. long int value;
  668. top = -1;
  669. gets(infix);
  670. infix_to_postfix();
  671. value = eval_post();
  672.  
  673. getch();
  674. closegraph();
  675.  
  676. return 0;
  677. }
  678. void infix_to_postfix()
  679. {
  680. unsigned int i, p = 0;
  681. char next;
  682. char symbol;
  683. for (i = 0; i < strlen(infix); i++)
  684. {
  685. symbol = infix[i];
  686. if (!white_space(symbol))
  687. {
  688. switch (symbol)
  689. {
  690. case '(':
  691. push(symbol);
  692. break;
  693. case ')':
  694. while ((next = pop()) != '(')
  695. postfix[p++] = next;
  696. break;
  697. case '+':
  698. case '-':
  699. case '*':
  700. case '/':
  701. case '%':
  702. case '^':
  703. while (!isEmpty() && priority(stack[top]) >= priority(symbol))
  704. postfix[p++] = pop();
  705. push(symbol);
  706. break;
  707. default: /*if an operand comes*/
  708. postfix[p++] = symbol;
  709. }
  710. }
  711. }
  712. while (!isEmpty())
  713. postfix[p++] = pop();
  714. postfix[p] = '\0'; /*End postfix with'\0' to make it a string*/
  715. }/*End of infix_to_postfix()*/
  716.  
  717. ///prioritatea operatorilor
  718. int priority(char symbol)
  719. {
  720. switch (symbol)
  721. {
  722. case '(':
  723. return 0;
  724. case '+':
  725. case '-':
  726. return 1;
  727. case '*':
  728. case '/':
  729. case '%':
  730. return 2;
  731. case '^':
  732. return 3;
  733. default:
  734. return 0;
  735. }
  736. }
  737.  
  738. void push(long int symbol)
  739. {
  740. if (top > 100)
  741. {
  742. printf("Stack overflow\n");
  743. exit(1);
  744. }
  745. stack[++top] = symbol;
  746. }/*End of push()*/
  747.  
  748. long int pop()
  749. {
  750. if (isEmpty())
  751. {
  752. printf("Stack underflow\n");
  753. exit(1);
  754. }
  755. return (stack[top--]);
  756. }
  757. ///stivaVida
  758. int isEmpty()
  759. {
  760. if (top == -1)
  761. return 1;
  762. else
  763. return 0;
  764. }
  765.  
  766. int white_space(char symbol)
  767. {
  768. if (symbol == BLANK || symbol == TAB)
  769. return 1;
  770. else
  771. return 0;
  772. }///daca e spatiu
  773.  
  774. long int eval_post()
  775. {
  776. long int a, b, temp, result;
  777. unsigned int i;
  778.  
  779. for (i = 0; i < strlen(postfix); i++)
  780. {
  781. if (postfix[i] <= '9' && postfix[i] >= '0')
  782. push(postfix[i] - '0');
  783. else
  784. {
  785. a = pop();
  786. b = pop();
  787. switch (postfix[i])
  788. {
  789. case '+':
  790. temp = b + a; break;
  791. case '-':
  792. temp = b - a; break;
  793. case '*':
  794. temp = b * a; break;
  795. case '/':
  796. temp = b / a; break;
  797. case '%':
  798. temp = b % a; break;
  799. case '^':
  800. temp = pow(b, a);
  801. }
  802. push(temp);
  803. }
  804. }
  805. result = pop();
  806. return result;
  807. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement