3x5w4rup

Calculator

Apr 19th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define SIZE 50
  4.  
  5. struct stack
  6. {
  7.   private: int head; int A[SIZE];
  8.   public: stack()
  9.           {
  10.               head=0;
  11.           }
  12.           int top(void){
  13.                 return A[head];
  14.           }
  15.           int limit(void){
  16.                 return head;
  17.           }
  18.           void push(int data)
  19.           {
  20.               A[head++]=data;
  21.           }
  22.           int pop()
  23.           {
  24.               return A[--head];
  25.           }
  26.           bool is_full()
  27.           {
  28.               if(head==SIZE) return true;
  29.               else return false;
  30.           }
  31.           bool is_empty()
  32.           {
  33.               if(head == 0) return true;
  34.               else return false;
  35.           }
  36. };
  37.  
  38. int main(int argc, char *argv[]){
  39.     stack s,s1;
  40.     char line[100];
  41.     int i=0,hold;
  42.     printf("Enter The Arithmetic Expression : ");
  43.     gets(line);
  44.     while(line[i]!='\0'){
  45.         if(line[i]>='0' && line[i]<='9') s.push(line[i]-48);
  46.         else if(line[i]=='+' || line[i]=='-' || line[i]=='*' || line[i]=='/'){
  47.             switch(line[i]){
  48.                 case '+':
  49.                 s.push(s.pop()+s.pop());
  50.                 break;
  51.                 case '-':
  52.                 s.push(s.pop()-s.pop());
  53.                 break;
  54.                 case '*':
  55.                 s.push(s.pop()*s.pop());
  56.                 break;
  57.                 case '/':
  58.                 hold = s.pop();
  59.                 s.push(s.pop()/hold);
  60.                 break;
  61.             }
  62.            
  63.         }
  64.         i++;
  65.     }
  66.     printf("%d",s.pop());
  67.     return 0;
  68. }
Add Comment
Please, Sign In to add comment