3 Replies - 164 Views - Last Post: 19 April 2013 - 07:58 AM Rate Topic: -----

#1 Gaurav_93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-April 13

can any one explain me this program

Posted 19 April 2013 - 12:42 AM

//ADDITION OF LARGE INTEGERS.

#include<iostream>
#include<cstdlib>
#define size 10

using namespace std;

class stack
{
     private:
     int a[size];
     int top;

     public:
     stack()                                          // constructor definition and declaration.
     {
        top=-1;
     }
     void push(int el);                               // functions declarations.
     int pop();
     bool isempty();
     bool isfull();
     void display();
     void clear();

};


void stack::push(int el)                              // definition of push function.
{
     if(isfull())
     {
        cout<<"\n STACK IS FULL.\n";
     }

     else
     {
          top++;
          a[top]=el;
     }

}


int stack::pop()                                      //  definition of pop function.
{   if(isempty())
    {
        cout<<"\n STACK IS EMPTY.\n";
        return -1;
    }

    else
    {
        int el=a[top];
        top--;
        return el;
    }

}


bool stack::isempty()                                 //  definition of isEmpty function.
{
    if(top==-1)
        return true;

    else
        return false;

}


bool stack::isfull()                                  //  definition of isFull function.
{
    if(top==size-1)
        return true;

    else
        return false;

}


void stack::display()                                 //  definition of display function.
{
    cout<<"\n THE NUMBERS ARE : ";
    for(int i=top;i>=0;i--)
    {
        cout<<a[i]<<"\n";
    }

}


void stack::clear()                                   //  definition of clear function.
{
    top=-1;
}


int main()
{
    stack s1,s2,s3;
    int n,m,element,carry=0,result=0,d1,d2,min,max,l;

    cout<<"\n ENTER NO OF ELEMENTS OF FIRST OPERAND : ";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cout<<"\n ENTER "<<i+1<<" DIGIT OF FIRST OPERAND : ";
        cin>>element;
        s1.push(element);
    }

    cout<<"\n ENTER NO. OF DIGIT OF SECOND OPERAND : ";
    cin>>m;
    for(int j=0;j<m;j++)
    {
        cout<<"\n ENTER "<<j+1<<" DIGIT OF SECOND OPERAND : ";
        cin>>element;
        s2.push(element);
    }

    if(n>m)
    {
        min=m;
        max=n;
    }
    else
    {
        min=n;
        max=m;
    }

    for(int k=0;k<min;k++)                  /*descrie this loop please*/
    {
        d1=s1.pop();
        d2=s2.pop();
        result=d1+d2+carry;
        carry=result/10;
        s3.push(result%10);
    }
    l=max-min;

    for(int m=0;m<l;m++)
    {
        if(!s1.isempty())
        {
            d1=s1.pop();
        }
        else
        {
            d1 = s2.pop();
        }
        result=d1+carry;
        carry=result/10;
        s3.push(result%10);
    }
    if(carry != 0)
    {
        s3.push(carry);
    }
    cout<<"\n THE RESULT IS : ";

    while(!s3.isempty())
    {
        cout<<s3.pop();
    }

    cout << "\n";

    return 0;
}


This post has been edited by Skydiver: 19 April 2013 - 06:21 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: can any one explain me this program

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: can any one explain me this program

Posted 19 April 2013 - 01:33 AM

Please use code tags to preserve formatting.

This post has been edited by Aphex19: 19 April 2013 - 01:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3163
  • View blog
  • Posts: 9,557
  • Joined: 05-May 12

Re: can any one explain me this program

Posted 19 April 2013 - 06:22 AM

So what is your question?
Was This Post Helpful? 0
  • +
  • -

#4 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 310
  • Joined: 12-January 11

Re: can any one explain me this program

Posted 19 April 2013 - 07:58 AM

This is not a program, per se, but is a class which creates the Stack data structure.

A Stack is a FILO (first in last out) data structure. Example:

Pseudocode:
stack = []
push 1
stack = [1]
push 2
stack = [2,1]
push 3
stack = [3,2,1]
int poppedval1 = pop //returns the value popped, in this case 3
stack = [2,1]
int popedval2 = pop //returns the value popped, in this case 2
stack = [1]

Unless you have a more specific question, that's all i have for you (although this class could be made better if it automatically increased the size of the array when the stack was full).

This post has been edited by Psyguy: 19 April 2013 - 08:03 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1