Thursday 7 March 2013

Calculator in c++ using stack data structure



  /*Calculator =========================program*/
 
     /****************************************************************/
     /*****************************************************************/
           #include <iostream>
          #include <conio>
          #include <stdlib>
    /*****************************************************************/
    /*****************************************************************/

                                float  a[1000];
                                int top=0,i,r;
                                char u;
                                float  input();
                                 float d,p,q,w;
                                 void push(float x);
                                 void get();
               void show(float  arr[]);
 /***********************************************************************/
 /***********************************************************************/
               void main()

               {


                 get();
               getch();}

 /*-------------------------------------------------------------------------*/
 /*-------------------------------------------------------------------------*/

               void get()
              {

                cout<<"\t\tPlease Enter  number\t";
               for(i=0;i<1;i++)

               {
               cin>>a[i];}


               show(a); }


 /*_____________________________________________________________________*/
 /*_____________________________________________________________________*/
               void   show(float  arr[])


               {
                 float  c[33];
                 int v;
                 v=0;

                 for(i=0;i<1;i++)

                 {
                  c[v]=arr[i];v++;
                 }
                  float sum=0;
                  for(i=0;i<1;i++)

                  {
                   sum=sum+c[i];
                  }

                     push(sum);
                     while(1)
                  {
                     cout<<"\n\n";
                    cout<< "chose\n\t\t + \n \t\t - \n \t\t *\n \t\t /\n";
                     cout<<"\t\tpress C for clear screen\n\t\t\Press E for exit\n"<<endl;

                       cin>>u;





                    switch(u)

                    {
                    case '+':
                     d=a[top]+input();
                    cout<<"\t\t\t\t\t\tANS="<<d<<endl;
                    push(d);
                    break;
                    case '-':
                      p=a[top]-input();
                    cout<<"\t\t\t\t\t\tANS="<<p<<endl;
                    push(p);
                    break;
                    case '*':
                     q=a[top]*input();
                    cout<<"\t\t\t\t\t\tANS="<<q<<endl;
                    push(q);
                    break;
                    case '/':
                  w=a[top]/input();
                    cout<<"\t\t\t\t\t\tANS="<<w<<endl;
                    push(w);
                    break;




                    case 'e':
                    exit(0);
                    break;
                    case 'c':
                    clrscr();
                    get();

                    break; }



                    }

               }



/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/
                 void push(float  x)

                 {
                   top++;

                   a[top]=x;
                 }
/*______________________________________________________________________________*/
/*______________________________________________________________________________*/

                     float  input()

                    {

                      float  t;
                      cout<<"\t\tPlease Enter  number\t\t";
                      cin>>t;
                      cout<<endl<<endl;

                      return t;

                    }

/*______________________________________________________________________________*/

0 comments:

Post a Comment