-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathnumere mari.cpp
86 lines (80 loc) · 1.55 KB
/
numere mari.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
# include <fstream>
# include <cstring>
# define NR 100
using namespace std;
ifstream f("mari.in");
ofstream g("mari.out");
int i,j,n,m,l,la,lb,t,tip;
int a[NR], b[NR], c[NR];
char s[NR];
void tipar ()
{
for (i=c[0]; i>=1; --i)
g<<c[i];
g<<"\n";
}
void adunare ()
{
c[0]=max(a[0], b[0]);
for (i=1; i<=c[0]; ++i)
{
t=t+a[i]+b[i];
c[i]=t%10;
t=t/10;
}
while (t) c[++c[0]]=t%10, t=t/10;
}
void scadere ()
{
c[0]=max(a[0], b[0]);
for (i=1; i<=a[0]; ++i)
{
c[i]=t+a[i]-b[i];
if (c[i]<0) t=-1, c[i]+=10;
else t=0;
}
while (c[c[0]]==0) --c[0];
}
void inmultire ()
{
c[0]=a[0]+b[0]-1;
for (i=1; i<=a[0]; ++i)
for (j=1; j<=b[0]; ++j)
c[i+j-1]+=a[i]*b[j];
for (i=1; i<=c[0]; ++i)
{
t=t+c[i];
c[i]=t%10;
t=t/10;
}
while (t) c[++c[0]]=t%10, t=t/10;
}
void impartire (int x)
{
int R=0;
for (i=a[0]; i>=1; --i)
{
R=R*10+a[i];
c[i]=R/x;
R=R%x;
}
c[0]=a[0];
while (c[0]>0 && ! c[c[0]]) --c[0];
}
int main ()
{
f.getline (s+1, NR); la=strlen(s+1);
for (i=la; i>=1; --i)
a[la-i+1]=s[i]-'0';
f.getline (s+1, NR); lb=strlen(s+1);
for (i=lb; i>=1; --i)
b[lb-i+1]=s[i]-'0';
a[0]=la; b[0]=lb;
f>>tip;
if (tip==1) adunare (); //intre numere mari
if (tip==2) scadere (); //intre nr mari, pp a > b
if (tip==3) inmultire (); //intre numere mari
if (tip==4) impartire (5); //unui mare la un scalar
tipar ();
return 0;
}