-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddintegersusingstack.cpp
More file actions
100 lines (99 loc) · 1.46 KB
/
Copy pathaddintegersusingstack.cpp
File metadata and controls
100 lines (99 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
using namespace std;
class stackdemo
{
int st[10];
int top;
public:
stackdemo()
{
top = -1;
}
void push(int);
int pop();
void display();
int isfull();
int isempty();
stackdemo add(stackdemo ob1);
};
void stackdemo::push(int e)
{
top++;
st[top] = e;
}
int stackdemo::pop()
{
int rem;
if (isempty())
{
cout << "\nlist is empty";
return(-1);
}
else
{
rem = st[top--];
return rem;
}
}
void stackdemo:: display()
{
cout << "\nthe stack is :";
for (int i = 0;i <= top;i++)
cout << "\n" << st[i];
}
stackdemo stackdemo::add(stackdemo ob2)
{
int carry = 0;
stackdemo ob;
int x, y;
while (!isempty() || !ob2.isempty())
{
if (!isempty())
x = pop();
else
x = 0;
if (!ob2.isempty())
y = ob2.pop();
else
y = 0;
carry = carry + x + y;
ob.push(carry % 10);
carry = carry / 10;
}
if (carry != 0)
ob.push(carry) ;
return ob;
}
int stackdemo::isempty()
{
if (top == -1)
return 1;
else
return 0;
}
int main()
{
stackdemo ob1, ob2, ob3;
int a, n, b;
cout << "\nenter n ";
cin >> n;
cout << " enter first " << n << " digits\n ";
for (int i = 0;i < n;i++)
{
cin >> a;
ob1.push(a);
}
ob1.display();
cout << "\n enter n";
cin >> n;
cout << "\nenter second " << n << " digits\n ";
for (int i = 0;i < n;i++)
{
cin >> b;
ob2.push(b);
}
ob2.display();
ob3 = ob1.add(ob2);
ob3.display();
return 0;
}