-
Notifications
You must be signed in to change notification settings - Fork 0
/
10035.cpp
116 lines (90 loc) · 1.33 KB
/
10035.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* Primary Arithmetic - 10035 */
int maior,n1[10],n2[10],n3[11];
char str1[11],str2[11];
int justify(int t1,int t2){
int i;
maior=t1;
if(t1>t2)
{
for(i=1;i<=t2;i++)
{
n2[t1-i]=n2[t2-i];
n2[t2-i]=0;
}
}
if(t1<t2)
{
maior=t2;
for(i=1;i<=t1;i++)
{
n1[t2-i]=n1[t1-i];
n1[t1-i]=0;
}
}
return maior;
}
int convert(int t1,int t2){
int i;
char aux[2];
for(i=0;i<t1;i++)
{
aux[0]=str1[i];
n1[i]=atoi(aux);
}
for(i=0;i<t2;i++)
{
aux[0]=str2[i];
n2[i]=atoi(aux);
}
return 0;
}
int inicializa(){
int i;
for(i=0;i<102;i++)
{
n1[i]=0;
n2[i]=0;
n3[i]=0;
}
return 0;
}
int add(int maior){
int i,cont=0;
for(i=0;i<maior;i++)
n3[i]+=n1[i]+n2[i];
for(i=maior-1;i>0;i--)
if(n3[i]>=10)
{
cont++;
n3[i-1]+=n3[i]/10;
n3[i]=n3[i]%10;
}
if(n3[0]>=10)
cont++;
return cont;
}
main(){
int i,t1,t2,maior,cont;
while(true)
{
inicializa();
scanf("%s %s",str1,str2);
if(atoi(str1)==0 && atoi(str2)==0)
break;
t1=strlen(str1);
t2=strlen(str2);
convert(t1,t2);
maior=justify(t1,t2);
cont=add(maior);
if(cont==0)
printf("No carry operation.");
if(cont==1)
printf("%d carry operation.",cont);
if(cont>1)
printf("%d carry operations.",cont);
printf("\n");
}
}