Help with Equation evaluation program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • X~coder
    New Member
    • Sep 2006
    • 14

    Help with Equation evaluation program

    hy i'm a noobie too i tried to make a calculator that imputs an ecuation
    it is a consol program. i hope it can help you :)
    ex: 3+5*564-342^33/22-(34343-(((34^4+2)-2-6))+4)=
    the part of the cod i have finds any mitakes and puts the ecuation in a txt file.
    ( it is interactiv for ex: you type 3+* it tels you that you entered 2 operatos and it asks you if you want to continue with the ec. if you give " Y" it deletes the last operator and it continue to input from 3+)




    here is the code


    Code:
    // david 2.cpp : Defines the entry point for the console application.
    //
    #include <iostream.h>
    #include <conio.h>
    #include "math.h"
    #include "stdafx.h"
    #include "david 2.h"
    #include <fstream>
    
    
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // The one and only application object
    
    CWinApp theApp;
    
    using namespace std;
    
    char expresie[1000];
    
     int lungime_expresie;
     int contin=1;
     int putere_repetata=0;
     int impartire_repetata=0;
     ofstream expresie_1("expresie_1");
    
    int intrare_gresita(int tip_eroare,char read_var_func)
    {
     int lungime_mesaj,retur;
     retur=6;
    
     switch(tip_eroare)
     {case 0:{cout<<" ecuatia nu poate incepe cu "<<read_var_func<<" continuati? Y/N ";
              lungime_mesaj=47;//cout<< lungime_mesaj;
    		  break;
             }
      case 1:{cout<<" nu pot aparea 2 semne consecutiv "<<"' "<<read_var_func<<read_var_func<<" '  continuati? Y/N ";
    	      lungime_mesaj=58;
    		  break;
    		 } 
      case 2:{cout<<" dupa '(' nu poate aparea ' "<<read_var_func<<" ' continuati? Y/N ";
    	      lungime_mesaj=48;
              break;
    		 }
      case 3:{cout<<" exprimare ambigua. folositi parantezele continuti? Y/N ";
    	      lungime_mesaj=56;
    		  break;
    		 }
      case 4:{cout<<" nu ati inchis toate parantezele continuati? Y/N ";
    	      lungime_mesaj=49;
    		  break;
    		 }
      case 5:{cout<<" "<<read_var_func<<" nu e recunoscut ca numar sau ca operatie continuati? Y/N ";
    	      lungime_mesaj=60;
    		  break;
    		 }
      case 6:{cout<<" ' ( ) ' nu se efectueaza nici o operatie  continuati? Y/N ";
    	      lungime_mesaj=59;
              break;
    		 }
      case 7:{cout<<"dupa o operatie nu poate urma ')' continuati? Y/N ";
    	      lungime_mesaj=50;
    		  break;
    		 }
      case 8:{cout<<"nu exista nici o paranteza dechisa corespunzatoare continuati? Y/N ";
    	     lungime_mesaj=67;
    		 break;
    		 }
     }
     //cout<<"\b\b\b\b";
     char ch;
     while(1){
      ch = tolower(_gettch());
      switch(ch)
      {case 'y':{for(int u=lungime_mesaj;u>0;u--)
                 {cout<<"\b";}
                 for(int u=lungime_mesaj;u>0;u--)
                 {cout<<" ";}
                 for(int u=lungime_mesaj;u>0;u--)
                 {cout<<"\b";}
                 retur=1;
                 break;
                 }
     
       case 'n':{retur=0;
    	         break;
    			}
       
       default:{1;}
      }
      if(retur!=6){return retur; break;}
    
     }
    }
    
    
    int test_erori(char read_var_te,int nr_paranteze,int putere_repetata) 
    {int eroare_break=1;
    if(!(strchr("0123456789+-*/^=()",read_var_te))  )
      {if(!(read_var_te=(char)8))
      {eroare_break=0;contin= intrare_gresita(5,read_var_te);//cout<<" eroare 5";  ///PRIMUL IF VERIFICA DACA CARACTERUL 
      }     
      }
    if( (lungime_expresie==1) && (!(strchr("0123456789(",read_var_te)))) ///DACA ESTE PRIMUL CARACTER SI NU E CIFRA SAU 
      {eroare_break=0;contin = intrare_gresita(0,read_var_te);//cout<<"eroare 0";
    }
    
    
    if (lungime_expresie!=1)
      
      {////////////////////////////
    
    if( (strchr("+-*/^",read_var_te)) && (strchr("+-*/^",expresie[lungime_expresie-1])) )
      {eroare_break=0;contin= intrare_gresita(1,read_var_te);/*cout<<" eroare 1";*/}
    if( (expresie[lungime_expresie-1]=='(')&&(read_var_te==')') )
      {eroare_break=0;contin=intrare_gresita(6,read_var_te);/*cout<<" eroare 6";*/}
    if( (expresie[lungime_expresie-1]=='(') && (strchr("*/^",read_var_te)) )
      {eroare_break=0;contin=intrare_gresita(2,read_var_te);/*cout<<" eroare 2";*/}
      if ( (read_var_te=='=') && (nr_paranteze!=0) )
      {eroare_break=0;contin=(intrare_gresita(4,read_var_te));/*cout<<" eroare 4";*/}
      if( (read_var_te=='^') && (putere_repetata==1) )
      {eroare_break=0;contin=intrare_gresita(3,read_var_te);/*cout<<" eroare 3";*/}
      if( (strchr("+-*/^",expresie[lungime_expresie-1]))&&(read_var_te==')') )
       {eroare_break=0;contin=intrare_gresita(7,read_var_te);/*cout<<" eroare 7";*/}
      if( (nr_paranteze==0)&& (read_var_te==')'))
      {eroare_break=0;contin=intrare_gresita(8,read_var_te);/*cout<<" eroare 8";*/}
      if( (read_var_te=='/')&& (impartire_repetata==1))
      {eroare_break=0;contin=intrare_gresita(3,read_var_te);/*cout<<" eroare 3";*/}
    }
    return eroare_break;
    }
    
    
    int citire_puteri(char read_var)
    {   //cout<<"  ggg "<<read_var<<" dss "<<putere_repetata<<" cc ";
    	if( (read_var=='^') && (putere_repetata==0) )
        {putere_repetata=1;
        expresie[lungime_expresie]='^'; expresie_1<<'^';
        lungime_expresie++;cout<<"^";
        return 1;
        }
        if( (!strchr("0123456789",read_var)) && (putere_repetata==1) )
        {putere_repetata=0;return 0;
        }
    	return 0;
    }
    
    
    int citire_impartire(char read_var)
    {   //cout<<"  ggg "<<read_var<<" dss "<<putere_repetata<<" cc ";
    	if( (read_var=='/') && (impartire_repetata==0) )
        {impartire_repetata=1;
        expresie[lungime_expresie]='/';    expresie_1<<'/';
        lungime_expresie++;cout<<"/";
        return 1;
        }
        if( (!strchr("0123456789",read_var)) && (impartire_repetata==1) )
        {impartire_repetata=0;return 0;
        }
    	return 0;
    }
    
    
    int read_function(void)
    { expresie[0]='(';lungime_expresie=1;  expresie_1<<(char)38<<" (";
     //int eroare_break=1;
     int nr_paranteze=0;
     int retur=1; 
     //int putere_repetata=0;
     char read_var; 
    
    while(2)    
     {//cout<<"   "<<putere_repetata<<"   ";
      read_var=_gettch(); //CITESTE CARACTERUL INTRAODUS ////FARA ECOU
    
     if(test_erori(read_var,nr_paranteze,putere_repetata))
     {
      if( !citire_puteri(read_var))
      {
      if( !citire_impartire(read_var))
      {
    
       if(read_var=='='){expresie[lungime_expresie]=')';expresie_1<<")="<<endl;expresie[lungime_expresie+1]='=';lungime_expresie+=2;cout<<"=";return 1;break;}
       else{  
    	   if(read_var==(char)8){cout<<"\b \b";lungime_expresie--;}
    	   else{
         expresie[lungime_expresie]=read_var;
    		   expresie_1<<read_var;
    	 if(read_var=='('){nr_paranteze++;}
    	 if(read_var==')'){nr_paranteze--;}
    	 cout<<expresie[lungime_expresie];
         lungime_expresie++;
    	   }}
      }}}
     else { if(!contin) {break;}}
    }return 1;
    }
     
    
    
    
    
    
    
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    	int nRetCode = 0;
    
    	// initialize MFC and print and error on failure
    	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    	{
    		// TODO: change error code to suit your needs
    		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
    		nRetCode = 1;
    	}
    	else
    	{int b;
    		cout<<"hg \n";
           read_function();
    	   cout<<"\n a iesit din functie";
    	   for(int i=0;i<=lungime_expresie;i++)
    		   cout<<expresie[i];
    	  
    	  //expresie_1<<;
    	  expresie_1.close();
    	   cin>>b;
    		// TODO: code your application's behavior here.
    	}
    
    	return nRetCode;
    }
    Last edited by Banfa; Oct 1 '06, 04:48 PM. Reason: Added [code] ... [/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Please don't hijack other peoples threads, if you have a new question start a new thread.

    In your post you have stated what the problem is.

    Comment

    Working...