//простой калькулятор, использующий параметризованный стек

#include<iostream.h>

#include<string.h>

#include<stdlib.h>

 

#include<iostream.h>

#include<stdlib.h>

#include <math.h>

 

 

void calculator();

 

//параметризированный класс стека

 

template <class Stype> class stack

{

  Stype *stck;          //указатель на тип Stype

  int tos;    //переменные

  int length;

public:

  stack(int size);         //конструктор

  ~stack() {delete [] stck;}  //деструктор встроенный

 

  void push (Stype i);   //функции 

  Stype pop();

};

 

                     //конструктор стека

 

template <class Stype> stack <Stype>::stack(int size)

{

  stck = new Stype[size];    //создали массив в динамической памяти

  if(!stck)

  {

        cout<<"Nevozmoghno sozdaty stack. \n";

        exit(1);

  }

  length =size;  //длина массива на единицу больше чем длина очередиохранить в length

  tos=0;

}

 

//объект помещается в стек

 

template <class Stype> void stack <Stype>::push (Stype i)

{

      if(tos==length)

      {

            cout<<"Stack zapolnen.\n";

            return;

    }

      stck[tos]=i;

      tos++;

}

 

//извлечение объекта из стека

 

template <class Stype> Stype stack <Stype>::pop()

{

      if(tos==0)

      {

            cout<<"Stack pust.\n";

            return 0;

      }

      tos--;

      return stck[tos];

}

 

 

main()

{

      calculator();

      return 0;

}

 

 

//калькулятор использующий операции на стеке

 

void calculator()

{

      stack<double> calc(100); //создадим стек

      double a, b;

      char str[80];

    int n = 0;

    char c1[20], c2[20], c3[20], c4[20], c5[20], c6[20], c7[20], c8[20], c9[20], cz[20];

     

      strcpy(c1, "ADD");

    strcpy(c2, "DDD");

    strcpy(c3, "MUL");

    strcpy(c4, "DIV");

    strcpy(c5, "DUP");

    strcpy(c6, "SWAP");

    strcpy(c7, "OVER");

    strcpy(c8, "SIN");

    strcpy(c9, "COS");

 

    strcpy(cz, ".");

 

 

      cout << "Prosteishiy calculator \n";

      cout << " Dlia vyhoda nazhmite 'q' \n";

 

    cout << " Enter  if you need stack operation DUP \n";

      cout << " Enter  if you need stack operation MUL \n";

      cout << " Enter  if you need stack operation DIV \n";

      cout << " Enter  if you need stack operation SWAP \n";

      cout << " Enter  if you need stack operation ADD \n";

      cout << " Enter  if you need  vychitanie  stack operation DDD \n";

 

      cout << " Enter  if you need stack operation OVER \n";

      cout << " Enter  if you need stack operation SIN \n";

      cout << " Enter  if you need stack operation COS \n";

 

 

      do    {

            cout << ":";

            cin >> str;

 

//        cout << "  str " <<  str << "\n" ;

 

        if (strcmp(str,c1) == 0) n=1;

        else if (strcmp(str,c2) == 0) n=2;

        else if (strcmp(str,c3) == 0) n=3;

        else if (strcmp(str,c4) == 0) n=4;

        else if (strcmp(str,c5) == 0) n=5;

        else if (strcmp(str,c6) == 0) n=6;

        else if (strcmp(str,c7) == 0) n=7;

        else if (strcmp(str,c8) == 0) n=8;

        else if (strcmp(str,c9) == 0) n=9;

 

        else if (strcmp(str,cz) == 0) n=33;

 

//          cout << "  n " <<  n << "\n" ;

 

 

            switch(n){

 

            case 1 :

                 a = calc.pop();

                   b = calc.pop();

                   cout << a+b << endl;

                   calc.push(a+b);

                   n = 0;

                   break;

            case 2 :

                  a = calc.pop();

                  b = calc.pop();

                  cout << b-a << endl;

                  calc.push(b-a);

                   n = 0;

                   break;

 

 

            case 3 :

                  a = calc.pop();

                  b = calc.pop();

                  cout << a*b << endl;

                  calc.push(a*b);

                  n = 0;

                  break;

 

            case 4 :

                  a = calc.pop();

                  b = calc.pop();

                  if(!a) {

                        cout << " Delenie na 0 \n";

                   n = 0;

                        break;

                  }

 

                  cout << b/a << endl;

                  calc.push(b/a);

                   n = 0;

                  break;

 

           

            case 5 :

                 a = calc.pop();

            //    cout << " DUP " << endl;

                   calc.push(a);

                   calc.push(a);

                   n = 0;

                   break;

 

                 

            case 6 :

                  a = calc.pop();

                  b = calc.pop();

      //          cout << " SWAP " << endl;

                  calc.push(a);

                  calc.push(b);

                   n = 0;

                  break;

 

            case 7 :

                 a = calc.pop();

                   b = calc.pop();

//                cout << " OVER " << endl;

                   calc.push(b);

                   calc.push(a);

             calc.push(b);

                   n = 0;

                   break;

 

            case 8 :

                  a = calc.pop();

//                cout << " SIN " << endl;

                  a = sin(a);

                  calc.push(a);

                   n = 0;

                  break;

 

            case 9 :

                  a = calc.pop();

//                cout << " COS " << endl;

                  a = cos(a);

                  calc.push(a);

                   n = 0;

                  break;

 

 

           

            case 33 : // Pokazaty soderzhimoe vershiny steka

                  a = calc.pop();

                  calc.push(a);

                  cout << "Tekushee znachenie v vershine steka : " ;

                  cout << a << endl;

                  n = 0;

                  break;

 

            default:

                  calc.push( atof(str));

                  n = 0;

            }

 

      } while (*str != 'q');

 

}

 

 

 

 

Hosted by uCoz