Herowaree

ChecarExpressão

Nov 19th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX 100
  4.  
  5. struct Pilha{
  6. char vetor[MAX];
  7. int topo;
  8. };
  9.  
  10. int iniciaPilha(struct Pilha *pilha){
  11. pilha->topo = -1;
  12. }
  13.  
  14. int vaziaPilha(struct Pilha *pilha){
  15. if(pilha->topo == -1){
  16. return 1;
  17. }else{
  18. return 0;
  19. }
  20. }
  21.  
  22. int cheiaPilha(struct Pilha *pilha){
  23. if(pilha->topo == MAX - 1){
  24. return 1;
  25. }else{
  26. return 0;
  27. }
  28. }
  29.  
  30. void push(struct Pilha *pilha, int caracter){
  31. //Encerra o programa em caso de overflow
  32. if(cheiaPilha(pilha)){
  33. printf("Expressao muito grande.\n");
  34. exit(0);
  35. }
  36.  
  37. pilha->vetor[ ++(pilha->topo) ] = caracter;
  38. }
  39.  
  40. char pop(struct Pilha *pilha){
  41. //O programa não tentará remover com a pilha vazia
  42. char caracter = pilha->vetor[ pilha->topo ];
  43. --(pilha->topo);
  44.  
  45. return caracter;
  46. }
  47.  
  48. char topoPilha(struct Pilha *pilha){
  49. //Se a pilha estiver vazia, a comparação vai dá diferente, e a expressão será considerada inválida pois existe parentese de fechamento sem um parentese de abertura, o retorno impede underflow
  50. if(vaziaPilha(pilha)){
  51. return '0';
  52. }else{
  53. return pilha->vetor[ pilha->topo ];
  54. }
  55. }
  56. -----------------------------------------------------------------
  57.  
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include "PILHA_SEQ.h"
  61.  
  62. int main(){
  63.  
  64. char string[1001];
  65. int count, tam, correto;
  66.  
  67. while(1){
  68.  
  69. gets(string);
  70. //Se a entrada for vazia, o laço é quebrado
  71. if(strlen(string) == 0) break;
  72.  
  73. //Inicializa a pilha que será utilizada
  74. struct Pilha pilha;
  75. iniciaPilha(&pilha);
  76. //Salva o tamanho da String
  77. tam = strlen(string);
  78. //A expressão é dada como valida até que se encontre um erro
  79. correto = 1;
  80. for(count = 0; count < tam; count++){
  81. if(string[count] == '(' || string[count] == '{' || string[count] == '['){
  82. push(&pilha, string[count]);
  83. }
  84.  
  85. if(string[count] == ')'){
  86. if(topoPilha(&pilha) != '('){
  87. //Encontrado a expressão logo é considerada inválida e o laço é quebrado
  88. correto = 0;
  89. break;
  90. }else{
  91. pop(&pilha);
  92. }
  93. }else if(string[count] == '}'){
  94. if(topoPilha(&pilha) != '{'){
  95. correto = 0;
  96. break;
  97. }else{
  98. pop(&pilha);
  99. }
  100. }else if(string[count] == ']'){
  101. if(topoPilha(&pilha) != '['){
  102. correto = 0;
  103. break;
  104. }else{
  105. pop(&pilha);
  106. }
  107. }
  108. }
  109.  
  110. //Checar se restou algum parenteses que não foi fechado na expressão
  111. if(vaziaPilha(&pilha)!=1) correto = 0;
  112.  
  113. if(correto == 1){
  114. printf("Expressao correta.\n");
  115. }else{
  116. printf("Expressao incorreta.\n");
  117. }
  118. }
  119.  
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment