Program : 4.1
---------------------------------------------------------------------------------------------------------------------
Program name : stack.cpp
#include<iostream>
using namespace std;
template <class t>
class stack
{
public:
t data;
stack* prev;
};
template<class t>
class stacks
{
stack<t> *top;
public:
stacks()
{
top=NULL;
}
void push(t n)
{
stack<t> *temp;
temp=new stack<t>;
temp->data=n;
temp->prev=top;
top=temp;
}
t pop()
{
t a;
if(top==NULL)
{
cout<<"UNDERFLOW\n";
}
else
{
a=top->data;
top=top->prev;
return a;
}
}
}
---------------------------------------------
Program name : stack1.cpp
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include "stack.cpp"
using namespace std;
int main()
{
stacks<int> s1,s2,s3;
t temp;
int sum,n;
char ch;
do
{
sum=0;
temp=0;
cout<<"Enter the two large numbers to be added one by one:\n";
cout<<"\nEnter the first number digit by digit (-1 to terminate):\n";
for(int i=0;n!=-1;i++)
{
cin>>n;
if(n!=-1)
s1.push(n);
}
n=0;
cout<<"\nEnter the second number digit by digit (-1 to terminate):\n";
for(int i=0;n!=-1;i++)
{
cin>>n;
if(n!=-1)
s2.push(n);
}
temp=0;
while((s1.empty()!=-1)&&(s2.empty()!=-1))
{
temp=temp+s1.pop()+s2.pop();
s3.push(temp%10);
temp/=10;
} if(s1.empty()!=-1)
{
while(s1.empty()!=-1)
{
s3.push(temp+s1.pop());
temp=0;
}
}
if(s2.empty()!=-1)
while(s2.empty()!=-1)
{
s3.push(temp+s2.pop());
temp=0;
}
if(temp!=0)
s3.push(temp); cout<<"The resulting number is:\n";
s3.display();
cout<<"\n\n\nWant to enter more:\n";
cin>>ch;
}while(ch=='y');
}
---------------------------------------------------------------------------------------------------------------------
Program Finish............