Advertisement
apl-mhd

Bappy assignment3(prob 2)

Mar 28th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. using namespace std;
  6. #define MAXSIZE 5
  7. int number[MAXSIZE];
  8. int top=-1;
  9.  
  10. void Push(int x){
  11.        
  12.     if(top == MAXSIZE -1){
  13.         cout<<"Error: Overflow !"<<endl;
  14.         return;
  15.     }
  16.    
  17.     number[++top] = x;
  18.    
  19. }
  20.  
  21. void Pop(){
  22.    
  23.     if(top == -1)
  24.     cout<<"Error:Underflow."<<endl;
  25.    
  26.     else
  27.         top--;
  28.    
  29. }
  30.  
  31. int Top(){
  32.        
  33.     //printf("%d\n", number[top]);
  34.    
  35.     return number[top];
  36. }
  37.  
  38.  
  39. void Print(){
  40.    
  41.     for(int i=0; i<=top; i++){
  42.        
  43.         cout<<number[i]<<" ";
  44.         }
  45.     cout<<endl;
  46.    
  47.     }
  48.  
  49.  
  50. int main(int argc, char **argv)
  51. {
  52.     string expression;
  53.    
  54.     cin>>expression;
  55.    
  56.     int i, op1,op2, sum=0, x;
  57.     char ch;
  58.    
  59.     for(i=0; i<expression.size(); i++){
  60.        
  61.         ch = expression[i];
  62.        
  63.         if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){
  64.            
  65.  
  66.            
  67.             if(ch == '+'){
  68.                 op2 = Top(); Pop();
  69.                 op1 = Top(); Pop();
  70.                 sum = op2+op1;
  71.                 //cout<<sum<<endl;
  72.                 Push(sum);
  73.                 //Print();
  74.                
  75.                
  76.                 }
  77.             else if(ch == '-'){
  78.                 op2 = Top(); Pop();
  79.                 op1 = Top(); Pop();
  80.                 sum = op1-op2;
  81.                 //cout<<sum<<endl;
  82.                 Push(sum);
  83.                
  84.                 }
  85.             else if(ch == '*'){
  86.                 op2 = Top(); Pop();
  87.                 op1 = Top(); Pop();
  88.                 sum = op2*op1;
  89.                 //cout<<sum<<endl;
  90.                 Push(sum);
  91.                
  92.                 }
  93.                 else if(ch == '/'){
  94.                 op2 = Top(); Pop();
  95.                 op1 = Top(); Pop();
  96.                 sum = op1/op2;
  97.                 //cout<<sum<<endl;
  98.                 Push(sum);
  99.                
  100.                 }
  101.            
  102.            
  103.            
  104.         }
  105.        
  106.     else{
  107.            
  108.         x = (int) ch -'0';
  109.             //cout<<"push:"<<x<<endl;
  110.             Push(x);   
  111.         }
  112.                
  113. }
  114.  
  115. cout<<sum<<endl;
  116.  
  117.  
  118.        
  119.        
  120.    
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement