Advertisement
SabirSazzad

Arithmetic Expression cheak using Stack

Feb 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include<iostream>
  2. #include <stack>
  3. #include <stdio.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     char exp[100];
  9.     char valid[] = "1234567890+-*/()^";
  10.     int i,x=0,y=0;
  11.     stack <char> myStack;
  12.     cout << "Input the Expression: ";
  13.     gets(exp);
  14.     for(i=0; exp[i] != '\0'; i++)
  15.     {
  16.         myStack.push(exp[i]);
  17.     }
  18.     while(!myStack.empty())
  19.     {
  20.         for(i=0; valid[i] != '\0'; i++)
  21.         {
  22.             if(myStack.top()==valid[i])
  23.             {
  24.                x =1;
  25.             }
  26.         }
  27.         if(x==1)
  28.         {
  29.             myStack.pop();
  30.             continue;
  31.         }
  32.         else
  33.         {
  34.             cout << "Invalid Arithmetic Expression"<<endl;
  35.             y=1;
  36.             break;
  37.         }
  38.  
  39.     }
  40.     if(y==0)
  41.     {
  42.        cout << "Valid Arithmetic Expression"<<endl;
  43.     }
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement