-
Notifications
You must be signed in to change notification settings - Fork 103
/
absys.cpp
131 lines (131 loc) · 2.26 KB
/
absys.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 2008-06-16
#include <iostream>
#include <cstring>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAXS 10000
using namespace std;
struct bignum
{
char* digits;
int length;
bignum(int size)
{
length=1;
digits=new char[size];
digits[0]=0;
}
bignum(int size,const bignum& x) //Copy constructor
{
length=x.length;
digits=new char[size];
memcpy(digits,x.digits,x.length);
}
bignum(int size,int x)
{
length=0;
digits=new char[size];
while (x>0)
{
digits[length++]=x%10;
x/=10;
}
if (length==0) {digits[0]=0; length=1;}
}
~bignum()
{
delete digits;
}
void add(bignum& x)
{
int l=MAX(length,x.length);
int d;
int carry=0;
x.digits[x.length]=0;
memset(digits+length,0,l-length+1);
for (d=0; d<=l; d++)
{
int sum=carry;
if (d<length)
sum+=digits[d];
if (d<x.length)
sum+=x.digits[d];
digits[d]=sum;
if (digits[d]>=10) {digits[d]-=10;carry=1;} else carry=0;
}
length=l;
if (digits[length]) length++;
if (length==0) {digits[0]=0; length=1;}
}
void sub(bignum& x)
{
int d;
int borrow=0;
digits[length]=0;
for (d=0; d<=length; d++)
{
digits[d]-=borrow;
if (d<x.length)
digits[d]-=x.digits[d];
if (digits[d]<0) {digits[d]+=10;borrow=1;} else borrow=0;
}
while (length>0&&!digits[length-1]) length--;
if (length==0) length=1;
}
void print()
{
int i;
for (i=length-1; i>=0; i--)
putchar(digits[i]+48);
}
void operator=(const bignum& y)
{
length=y.length;
memcpy(digits,y.digits,y.length);
}
void operator=(const char* s)
{
length=strlen(s);
int i=length;
while (i--)
digits[i]=s[length-i-1]-48;
}
};
int main()
{
int i,j,T;
scanf("%d",&T);
char s1[MAXS],s2[MAXS],s3[MAXS];
bignum n1(MAXS),n2(MAXS),n3(MAXS);
for (j=0; j<T; j++)
{
scanf("%s + %s = %s",&s1,&s2,&s3);
//one of the numbers has a "machula"
for (i=0; i<strlen(s1); i++)
if (s1[i]=='m')
{
n2=s2;
n3=s3;
n1=n3;
n1.sub(n2);
}
for (i=0; i<strlen(s2); i++)
if (s2[i]=='m')
{
n1=s1;
n3=s3;
n2=n3;
n2.sub(n1);
}
for (i=0; i<strlen(s3); i++)
if (s3[i]=='m')
{
n1=s1;
n2=s2;
n3=n1;
n3.add(n2);
}
n1.print(); printf(" + "); n2.print(); printf(" = "); n3.print(); printf("\n");
}
return 0;
}