-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongInt.cpp
More file actions
executable file
·76 lines (74 loc) · 1.2 KB
/
LongInt.cpp
File metadata and controls
executable file
·76 lines (74 loc) · 1.2 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
#include <iostream>
#include <stack>
#include <queue>
#include "LongInt.h"
using namespace std;
LongInt LongInt::operator+(LongInt& y)
{
LongInt z;
int a, b, c=0, temp;
while(!this->digit.empty() || !y.digit.empty())
{
if(this->digit.empty())
{
temp=y.digit.top()+c;
y.digit.pop();
z.digit.push(temp);
c=0;
}
else if(y.digit.empty())
{
temp=this->digit.top()+c;
this->digit.pop();
z.digit.push(temp);
c=0;
}
else {
a=this->digit.top();
this->digit.pop();
b=y.digit.top();
y.digit.pop();
z.digit.push((a+b+c)%10);
if((a+b+c)/10==1)
c=1;
else
c=0;
}
}
if(c==1)
z.digit.push(c);
return z;
}
ostream& operator<<(ostream& os, LongInt& x)
{
std::queue <int> n;
while(!x.digit.empty()){
n.push(x.digit.top());
x.digit.pop();
}
while(!n.empty()){
os<<n.front();
n.pop();
}
return os;
}
istream& operator>>(istream& is, LongInt& x)
{
int y;
std::queue <int> n;
while(is) {
char t=is.get();
if(isdigit(t))
{
y=t-'0';
n.push(y);
}
else
break;
}
while(!n.empty()) {
x.digit.push(n.front());
n.pop();
}
return is;
}