Advertisement
Guest User

Postfiksna notacija

a guest
Dec 11th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4.  
  5. typedef struct{
  6.     int broj;
  7.     //char znak;
  8.     //int isChar=1;
  9.     struct node* next;
  10. }node;
  11.  
  12. node* Head=NULL;
  13. node* Tail=NULL;
  14. /*
  15. void pushC(char c)
  16. {
  17.     node* new=(node*)malloc(sizeof(node));
  18.     new->znak=c;
  19.     new->next=NULL;
  20.     if(Head==NULL)
  21.     {
  22.         Head=new;
  23.         Tail=new;
  24.         return;
  25.     }
  26.     Tail->next=new;
  27.     Tail=new;
  28. }*/
  29.  
  30. void push(int c)
  31. {
  32.     node* new=(node*)malloc(sizeof(node));
  33.     new->broj=c;
  34.     //new->isChar=0;
  35.     new->next=NULL;
  36.     if(Head==NULL)
  37.     {
  38.         Head=new;
  39.         Tail=new;
  40.         return;
  41.     }
  42.     Tail->next=new;
  43.     Tail=new;
  44. }
  45.  
  46. int pop()
  47. {
  48.     if(Head==Tail)
  49.     {
  50.         int a=Head->broj;
  51.         free(Head);
  52.         Head=Tail=NULL;
  53.         return a;
  54.     }
  55.     node* c=Head;
  56.     while(c->next!=Tail)
  57.     {
  58.         c=c->next;
  59.     }
  60.     int a=Tail->broj;
  61.     free(Tail);
  62.     Tail=c;
  63.     c->next=NULL;
  64.     return a;
  65. }
  66.  
  67. void report()
  68. {
  69.     node* tmp=Head;
  70.     int i=0;
  71.     while(tmp)
  72.     {
  73.         if(i) printf(" -> ");
  74.         printf("%i",tmp->broj);
  75.         tmp=tmp->next;
  76.         i++;
  77.     }
  78. }
  79.  
  80. void radi(char* s)
  81. {
  82.     int i=0;
  83.     int broj=0;
  84.     while(s[i])
  85.     {
  86.         if(s[i]>='0' && s[i]<='9')
  87.         {
  88.             while(s[i]!=' ')
  89.             {
  90.                 int k=s[i]-'0';
  91.                 broj=broj*10+k;
  92.                 i++;
  93.             }
  94.             push(broj);
  95.             //printf("%i ",broj);
  96.             broj=0;
  97.         }
  98.         if(s[i]=='+')
  99.         {
  100.             int a=pop();
  101.             int b=pop();
  102.             push(a+b);
  103.         }
  104.         if(s[i]=='-')
  105.         {
  106.             int a=pop();
  107.             int b=pop();
  108.             push(b-a);
  109.         }
  110.         if(s[i]=='*')
  111.         {
  112.             int a=pop();
  113.             int b=pop();
  114.             push(a*b);
  115.         }
  116.         if(s[i]=='/')
  117.         {
  118.             int a=pop();
  119.             int b=pop();
  120.             push(b/a);
  121.         }
  122.         i++;
  123.     }
  124.     printf("\t %i ",pop());
  125. }
  126.  
  127. int main()
  128. {
  129.     char c[50];
  130.     gets(c);
  131.     radi(c);
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement