Advertisement
alvsjo

C (7.3., ned.4.)

Mar 7th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. //typedef int StackElement;
  7. typedef float StackElement;
  8.  
  9. struct Stack
  10. {
  11. StackElement* data;
  12. int top;
  13. int cap;
  14. };
  15.  
  16. typedef struct Stack Stack;
  17.  
  18.  
  19. void initStack(Stack* s, int cap)
  20. {
  21.     s->data=malloc(cap*sizeof(StackElement));
  22.     s->top=-1;
  23.     s->cap=cap;
  24. }
  25.  
  26. void destroyStack(Stack* s)
  27. {
  28.     free(s->data);
  29. }
  30.  
  31. void push(Stack* s, StackElement el)
  32. {
  33.     if(s->top==s->cap-1)
  34.     {
  35.         printf("Stek je pun\n");
  36.         exit(1);
  37.     }
  38.     s->top++;
  39.     s->data[s->top]=el;
  40. }
  41.  
  42. int isEmpty( Stack* s)
  43. {
  44.  if(s->top==-1)
  45.     return 1;
  46.  return 0;
  47. }
  48.  
  49. StackElement pop(Stack* s)
  50. {
  51.     if (isEmpty(s))
  52.     {
  53.         printf("Stek je prazan\n");
  54.         exit(2);
  55.     }
  56.     s->top--;
  57.     return s->data[s->top+1];
  58. }
  59.  
  60. StackElement top(Stack* s)
  61. {
  62.     if (isEmpty(s))
  63.     {
  64.         printf("Stek je prazan\n");
  65.         exit(2);
  66.     }
  67.     return s->data[s->top];
  68. }
  69.  
  70. //napisati funkciju koja vrace true ako je dat pravilan niz zagrada
  71.  
  72. int zagrade(char * str)
  73. {
  74. int n=strlen(str);
  75.  Stack s;
  76.  initStack(&s,n);
  77.  int i;
  78.  for(i=0;i<n;i++)
  79.  {
  80.      if(str[i]==' ')
  81.         continue;
  82.      if(str[i]=='(' || str[i]=='[' || str[i]=='{')
  83.         push(&s,str[i]);
  84.      else
  85.      {
  86.          if(isEmpty(&s))
  87.             {
  88.                 destroyStack(&s);
  89.                 return 0;
  90.             }
  91.          char t=pop(&s);
  92.          if(str[i]==')')
  93.             if(t!='(')
  94.             {
  95.                 destroyStack(&s);
  96.                 return 0;
  97.             }
  98.         if(str[i]==']')
  99.             if(t!='[')
  100.             {
  101.                 destroyStack(&s);
  102.                 return 0;
  103.             }
  104.         if(str[i]=='}')
  105.             if(t!='{')
  106.             {
  107.                 destroyStack(&s);
  108.                 return 0;
  109.             }
  110.      }
  111.  }
  112.  
  113.  
  114. if(isEmpty(&s))
  115.     {
  116.         destroyStack(&s);
  117.         return 1;
  118.     }
  119. destroyStack(&s);
  120. return 0;
  121.  
  122. }
  123.  
  124. void validneZagrade(char * str)
  125. {
  126.     if(zagrade(str))
  127.         printf("Izraz je validan\n");
  128.     else printf("Izraz nije validan\n");
  129. }
  130.  
  131. float postfixValue(char* str)
  132. {
  133. int n=strlen(str);
  134.  Stack s;
  135.  initStack(&s,n);
  136.  int i;
  137.  for(i=0;i<n;i++)
  138.  {
  139.      if(str[i]>='0'&& str[i]<='9')
  140.         push(&s,(float)str[i]-'0');
  141.      else
  142.      {
  143.          float op1=pop(&s);
  144.          float op2=pop(&s);
  145.          switch(str[i])
  146.          {
  147.             case '+':
  148.                 push(&s,op1+op2);
  149.                 break;
  150.             case '-':
  151.                 push(&s,op2-op1);
  152.                 break;
  153.             case '*':
  154.                 push(&s,op1*op2);
  155.                 break;
  156.             case '/':
  157.                 push(&s,op2/op1);
  158.                 break;
  159.          }
  160.      }
  161.  }
  162.  
  163.  float rez=pop(&s);
  164.  destroyStack(&s);
  165.  return rez;
  166.  }
  167.  
  168.  
  169.  
  170. int main()
  171. {
  172.  
  173.  Stack s;
  174. initStack(&s,5);
  175.  
  176.  
  177. push(&s,5);
  178. push(&s,31);
  179. push(&s,2);
  180. push(&s,53);
  181.  
  182. //printf("%d\n",pop(&s));
  183. char str[100];
  184. gets(str);
  185. //validneZagrade(str);
  186. printf("%f",postfixValue(str));
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.       return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement