Alx09

Untitled

Mar 26th, 2020
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. /** Implementarea formei poloneze cu evaluarea unei expresii numerice, in care:
  2. * - fiecare operand este o cifra;
  3. * - cei 4 operatori sunt cei aritmetici elementari.
  4. *
  5. * Functionalitatea ei se poate extinde pentru orice tipuri de numere si
  6. * alti operatori.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h> // Folosit pentru isdigit
  11.  
  12. int operatori[100];
  13. int cntOperatori;
  14.  
  15. int operanzi[100];
  16. int cntOperanzi;
  17.  
  18. int cuantificator = 0;
  19.  
  20. int pop_operand()
  21. {
  22. return operanzi[--cntOperanzi];
  23. }
  24.  
  25. int pop_operator()
  26. {
  27. return operatori[--cntOperatori];
  28. }
  29.  
  30. void push_operanzi(int val)
  31. {
  32. operanzi[cntOperanzi++] = val;
  33. }
  34.  
  35. void push_operator(int op)
  36. {
  37. operatori[cntOperatori++] = op;
  38. }
  39.  
  40. int peek_operatori()
  41. {
  42. return operatori[cntOperatori - 1];
  43. }
  44.  
  45. int compara_operator(int op1, int op2)
  46. {
  47. /* Daca diferenta intre operatori este -1 sau 1,
  48. * atunci pot fi (+,-), (-,*) sau (*,/).
  49. */
  50. /* Daca suma op1+op2 == 5, atunci combinatia este (-,*),
  51. * care este invalida.
  52. */
  53. if(abs(op1 - op2) == 1 && (op1 + op2) % 10 != 5)
  54. return 1;
  55. if(op2 > op1)
  56. return 0;
  57.  
  58. /* Este obligatoriu de pus un return final,
  59. * desi toate cazurile sunt deja acoperite
  60. */
  61. return 1;
  62. }
  63.  
  64. void calculeazaOperanzi(int operatorUrmator)
  65. {
  66. /* Compara operatorul urmator cu ultimul opeator din stiva. */
  67. int ultimOperator = peek_operatori();
  68.  
  69. while(cntOperatori > 0 && compara_operator(ultimOperator, operatorUrmator))
  70. {
  71. /* Se extrag operanzii in ordine inversa a stivei,
  72. * adica ordinea lor naturala.
  73. */
  74. int b = pop_operand();
  75. int a = pop_operand();
  76. int op = pop_operator();
  77.  
  78. switch(op % 10)
  79. {
  80. case 1: push_operanzi(a + b); break;
  81. case 2: push_operanzi(a - b); break;
  82. case 3: push_operanzi(a * b); break;
  83. case 4: push_operanzi(a / b); break;
  84. }
  85. }
  86. }
  87.  
  88. int main()
  89. {
  90. char exp[] = "1+2*3/4*(5+6-7)-8*9";
  91. int i, L = strlen(exp);
  92.  
  93. for(i = 0; i < L; i++)
  94. {
  95. if(isdigit(exp[i])) { // Doar o cifra ca numar
  96. operanzi[cntOperanzi] = exp[i] - '0';
  97. cntOperanzi++;
  98. } else if(exp[i] == '+') {
  99. calculeazaOperanzi(1 + cuantificator);
  100. operatori[cntOperatori] = 1 + cuantificator;
  101. cntOperatori++;
  102. } else if(exp[i] == '-') {
  103. calculeazaOperanzi(2 + cuantificator);
  104. operatori[cntOperatori] = 2 + cuantificator;
  105. cntOperatori++;
  106. } else if(exp[i] == '*') {
  107. calculeazaOperanzi(3 + cuantificator);
  108. operatori[cntOperatori] = 3 + cuantificator;
  109. cntOperatori++;
  110. } else if(exp[i] == '/') {
  111. calculeazaOperanzi(4 + cuantificator);
  112. operatori[cntOperatori] = 4 + cuantificator;
  113. cntOperatori++;
  114. } else if(exp[i] == '(') {
  115. cuantificator = cuantificator + 10;
  116. } else if(exp[i] == ')') {
  117. cuantificator = cuantificator - 10;
  118. }
  119. }
  120. /* Fortarea calcularii operanzilor ramasi cu
  121. * cel mai neprioritar operator (unul fictiv).
  122. */
  123. calculeazaOperanzi(0);
  124.  
  125. printf("%s = %d\n", exp, operanzi[0]);
  126.  
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment