Advertisement
hamaXD

lab 8 arry

Nov 14th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4.  
  5. int stack[50],top=-1;
  6.  
  7. void push(int v);
  8. int pop();
  9. int cal(char v);
  10.  
  11. int main(){
  12.     int i=0,t;
  13.     char s[128];
  14.     printf("Enter: ");
  15.     gets(s); fflush(stdin);
  16.     while(i<strlen(s)){
  17.     if(s[i]<='9'&&s[i]>='0'){
  18.          push(s[i]-'0');
  19.     }
  20.     else{
  21.         t=cal(s[i]);
  22.     }
  23.     i++;
  24.    }
  25.    i=0;
  26.    
  27.    printf("=%d",t);
  28.     return 0;
  29. }
  30. void push(int v){
  31.     if(top>50){
  32.         printf("FULL\n");
  33.     }
  34.     top = top + 1 ;
  35.     stack[top]= v;
  36. }
  37. int pop(){
  38.     return stack[top--];
  39. }
  40. int cal(char v){
  41.     int x,y,temp,k;
  42.     y = pop();
  43.     //printf("%d ",y);
  44.     x = pop();
  45.     //
  46.     //printf("%d ",x);
  47.     if(v=='+'){
  48.         temp = x+y;
  49.     }else if(v=='-'){
  50.         temp = x-y;
  51.     }else if(v=='*'){
  52.         temp = x*y;
  53.     }else if(v=='/'){
  54.         temp = x/y;
  55.     }else if(v=='%'){
  56.         temp = x%y;
  57.     }else if(v=='^'){
  58.         temp = pow(x,y);
  59.     }else{
  60.         printf("empty\n");
  61.     }
  62.     push(temp);
  63.    
  64.     return temp;
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement