/**** MoRaD Taleeb ******* 1060074 **************************** Project#2 *****/ #include #include #include #include #include // stack 2 used to find the result template < class T > class Stack { public: Stack( ){ top = 0 ;} bool isEmpty( ) ; void push( T & ) ; T pop( ) ; Node< T > * getTop() const { return top ; } private: Node< T > * top ; }; template < class T > bool Stack< T >::isEmpty( ) { return( top == '\0' ); } template < class T > void Stack< T >::push( T & d ) { Node< T > * temp = new Node< T >( d, top ); if( temp != '\0' ) top = temp ; } template < class T > T Stack< T >::pop() { if( isEmpty( ) ) cout<< " Empty Stack \n" ; else { Node< T > * temp = top ; top = top -> next ; T popValue = temp -> data; delete temp ; return popValue ; } } /******************************************************************************/ // stack 2 used to find the postfix template < class T > class Stack2 : public Stack< T > { public: T stackTop() const; // check the top value }; template < class T > T Stack2< T >::stackTop() const { if( !isEmpty() ) return getTop() -> getData() ; } /******************************************************************************/ template < class B > class Node { friend class Stack< B >; public: Node( const B & = 0, Node * = 0 ); B getData() const{ return data; } void setNext( Node *n ) { next = n ; } Node *getNext() const { return next; } private: B data; Node *next; }; template < class B > Node< B >::Node( const B &d, Node< B > *ptr ) { data = d; next = ptr; } /******************************************************************************/ int menu( ); char * enter( ) ; int checkEqu( char * ) ; void inputData( ) ; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// 2 char * toPostfix( char * ) ; // 3 int isOperator( char * ) ; int preced( char * , char * ) ; void errors( ) ; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// 4 int findResult( char * ) ; // 5 int calculate( int , int , char * ) ; void exit(void) ; // 6 char * n = enter( ) ; char * p = toPostfix( n ) ; void(*options[])( ) = { enter( ), inputData( ), toPostfix( n ), checkEqu( n ), findResult( p ), exit( ) }; /******************************************************************************/ void main( ) { int i; char ch; for(;;) { clrscr(); i = menu( ); // get user's choice (*options[i])(); // execute it ch = getch(); } } int menu( ) { char ch; do { gotoxy(38,8); cout << "Main Menu "; gotoxy(37,10); cout << "1. Enter Expression "; gotoxy(37,11); cout << "2. Input Data "; gotoxy(37,12); cout << "3. Calculation "; gotoxy(37,13); cout << "4. Find Errors "; gotoxy(37,14); cout << "5. Find Result "; gotoxy(37,15); cout << "6. Exit. "; gotoxy(33,17); cout << " Select a number: "; gotoxy(55,17); ch = getch(); } while ( !strchr("1234",ch)); return ch-49; // convert to an integer equivalent } /////////////////////////////////////////////////////////////////////////////// char * enter( ) { const int SIZE = 100; char infix[ SIZE ] ; cout<<" Entar your exprision -put space between operator and number- plz: " ; cin>> infix ; return infix ; } /******************************************************************************/ int checkEqu( char *e ) // e is the infix { char * p ; char * n ; p = strtok( e , " " ) ; while( p != '\0' ) { if( ! isOperator(p) ) { n = strtok( '\0' , " " ) ; if( isOperator(n) ) return 1; else return 0; } else if( p == "(" ) ; { n = strtok( '\0' , " " ) ; if( ! isOperator(n) || ( n="(" ) ) return 1; else return 0; } else return 0; p = strtok( '\0' , " " ) ; } } //////////////////////////////////////////////// end of check equation function char * toPostfix( char * infix ) { Stack2< char* > Stack ; char * postfix ; bool flat ; char popValue ; char *token ; if( checkEqu( infix ) ) { // push '(' onto the stack and add ')' to infix Stack.push( "(" ); strcat( infix , ")" ); token = strtok( infix , " " ); // convert the infix expression to postfix while ( token != '\0' ) { if ( ! isOperator( token ) ) postfix = strcat( postfix , token ) ; else if ( token == "(" ) Stack.push( "(" ); else if ( isOperator( token ) ) { flat = true; while ( flat ) { if ( isOperator( Stack.stackTop() ) ) if ( preced( Stack.stackTop(), token ) ) postfix = strcat( postfix , Stack.pop() ); else flat = false; else flat = false; } Stack.push( token ); } else if ( token == ")" ) { while ( ( popValue = Stack.pop() ) != "(" ) postfix = strcat( postfix , popValue ); } token = strtok( '\0' , " " ); // get next token } // End while cout<< "the postfix is : " << postfix ; } else cout<< "\n invalid expression " ; return *postfix ; } ///////////////////////////////////////////////////// end of toPostfix function int isOperator( char * x ) { if ( x == "+" || x == "-" || x == "*" || x == "/" || x == "%" ) return 1 ; else return 0 ; } //////////////////////////////////////////////////// end of isOperator function int preced( char* op1, char* op2 ) { if ( op1 == "*" || op1 == "/" || op1 == "%" ) return 1 ; else if ( op1 == "+" || op1 == "-" ) if ( op2 == "*" || op2 == "/" ) return 0 ; else return 1 ; return 0 ; } //////////////////////////////////////////////////////// end of preced function int findResult( char *postfix ) { char * p ; char * n ; int popVal1, popVal2, pushVal; Stack< int > s ; strcat( postfix , ")" ); p = strtok( *postfix , " " ) ; while( p != '\0' ) { if( ! isOperator( p ) ) { pushVal = p - '0' ; s.push( pushVal ); } else { popVal2 = s.pop( ) ; popVal1 = s.pop( ) ; pushVal = calculate( popVal1, popVal2, p ); s.push( pushVal ); } p = strtok( '\0' , " " ) ; } cout<< "\nthe result is : " << s.pop(); return s.pop(); } //////////////////////////////////////////////////// end of findResult function int calculate( int op1, int op2, char * op ) { switch( * op ) { case '+': return op1 + op2; case '-': return op1 - op2; case '*': return op1 * op2; case '/': return op1 / op2; case '%': return op1 % op2; } } ///////////////////////////////////////////////////// end of calculate function void errors( ) { ifstream read( "data.txt", ios::in ); if ( !read ) { cerr << "File could not be opened" << endl; exit( 1 ); } char f[ 300 ]; read.getline( f , 300 , "\n" ) ; char *token = strtok( f , "\n" ) ; char *A[100] ; // 100 is the maximum equations number in the file int i = 0 ; while( token != '\0' ) { A[i]= token ; if( checkEqu(A[i]) ) cout<< A[i] << "\nvalid" ; else cout<< A[i] << "\ninvalid" ; token = strtok( '\0' , "\n" ) ; i++ ; } } //////////////////////////////////////////////////////// end of errors function void inputData( ) { ifstream read( "data.txt", ios::in ); if ( !read ) { cerr << "File could not be opened" << endl; exit( 1 ); } char f[ 300 ]; read.getline( f , 300 , "\n" ) ; char *token = strtok( f , "\n" ) ; char *A[100] ; // 100 is the maximum equations number in the file int i = 0 ; while( token != '\0' ) { if( checkEqu(token) ) A[i]= token ; token = strtok( '\0' , "\n" ) ; i++ ; } } ///////////////////////////////////////////////////// end of inputData function void exit( ) { exit(0); } ////////////////////////////////////////////////////////// end of exit function // tested @ 3:57 Mon