-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum.c
261 lines (232 loc) · 5.52 KB
/
sum.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 100001
#define DEBUG if(0)
typedef struct node
{
int data;
struct node* previous;
}NODE;
typedef struct head
{
int size;
NODE* top;
}STACK;
STACK* init_list()
{
STACK* new_head = (STACK*) malloc(sizeof(STACK));
new_head->top = NULL;
new_head->size = 0;
return new_head;
}
int isEmpty(STACK* top)
{
return(top->size == 0);
}
void push(STACK* stack, int item)
{
NODE* new_top = (NODE*) malloc(sizeof(NODE*));
new_top->data = item;
if(isEmpty(stack))
{
new_top->previous = NULL;
}
else
{
new_top->previous = stack->top;
}
stack->size++;
stack->top = new_top;
}
NODE* pop(STACK* stack)
{
NODE* popped = stack->top;
if(isEmpty(stack))
{
return NULL;
}
popped->previous == NULL;
stack->top = stack->top->previous;
stack->size--;
return popped;
}
void calculate(STACK* a, STACK* b, STACK* res)
{
NODE* pos1 = pop(a);
NODE* pos2 = pop(b);
while (pos1 != NULL && pos2 != NULL)
{
//DEBUG printf("entrou no while \n");
int left = pos1->data;
int right = pos2->data;
int previous = 0;
if (res->top != NULL) previous = res->top->data;
int ans = left+right+previous;
DEBUG printf("[%d] + [%d] + [%d] = %d\t",left,right, previous,ans);
int rest = ans%10;
int next = ans/10;
DEBUG printf("→ [%d] e [%d]\n", next, rest);
// DEBUG printf("## %d e next = %d ##\n", rest, next);
// DEBUG printf("bbbbbbb \n");
if (res->top == NULL)
{
//res->top->data = rest;
push(res, rest);
}
else
{
res->top->data = rest;
}
push(res, next);
//DEBUG printf("bbbbbbb \n");
free(pos2);
free(pos1);
pos1 = pop(a);
pos2 = pop(b);
}
while (!(pos1 == NULL && pos2 == NULL))
{
//DEBUG printf("entrou no while 2 \n");
if (pos1 == NULL)
{
//DEBUG printf("PRIMEIRO IF!!! \n");
int right = pos2->data;
int previous = 0;
if (res->top != NULL) previous = res->top->data;
int ans = right+previous;
int rest = ans%10;
int next = ans/10;
res->top->data = rest;
push(res, next);
free(pos2);
pos2 = pop(b);
}
else if (pos2 == NULL)
{
int right = pos1->data;
int previous = 0;
if (res->top != NULL) previous = res->top->data;
int ans = right+previous;
int rest = ans%10;
int next = ans/10;
res->top->data = rest;
push(res, next);
free(pos1);
pos1 = pop(a);
}
}
}
void inverter (STACK* stack)
{
NODE* current = stack->top;
NODE* next = NULL;
NODE* prev = NULL;
while(current != NULL)
{
next = current->previous;
current->previous = prev;
prev = current;
current = next;
}
stack->top = prev;
}
void printer(STACK* stack)
{
int counter = 0;
while (!isEmpty(stack))
{
int n;
NODE* aux = pop(stack);
n = aux->data;
if (n == 0 && counter == 0)
{
continue;
}
printf("%d ",n);
free(aux);
counter++;
}
printf("\n");
}
int main() {
STACK* x = init_list();
STACK* y = init_list();
STACK* z = init_list();
int len1= 0; //lenght -1
int len2 = 0;//lenght -1
char flusher;
char X[MAX];
char Y[MAX];
do //gets the first number
{
scanf("%c", &X[len1]);
if (X[len1] == ' ' || X[len1] == '\n'|| X[0] == '0') //X[LEN1] vai ser reescrito na proxima rodada e exclui zeros a esquerda
{
continue;
}
if (X[len1] == '+') //para o loop
{
DEBUG printf("X[%d] = [%c]\n",len1,X[len1]);
break;
}
DEBUG printf("X[%d] = [%c]\n",len1,X[len1]);
push(x,X[len1]-'0');
len1++;
} while (1);
scanf("%c", &flusher); //flushs the \n
DEBUG printf("\n################\n\n");
do //gets the second number
{
scanf("%c", &Y[len2]);
if (Y[len2] == ' ' || Y[len2] == '\n' || Y[0] == '0') //Y[LEN2] vai ser reescrito na proxima rodada e exclui zeros a esquerda
{
continue;
}
if (Y[len2] == '=') //para o loop
{
DEBUG printf("Y[%d] = [%c]\n",len2,Y[len2]);
break;
}
DEBUG printf("Y[%d] = [%c]\n",len2,Y[len2]);
push(y,Y[len2]-'0');
len2++;
} while (1);
if (len1 == 0 && len2 == 0) //checks if the list is null
{
printf("Lista vazia!\n");
return 0;
}
if(len2 == 0)
{
for(int i = 0; i < len1; i++)
{
printf("%c ", X[i]);
if(i == len1-1)
{
printf("\n");
break;
}
}
return 0;
}
if(len1 == 0)
{
for(int i = 0; i < len2; i++)
{
printf("%c ", Y[i]);
if(i == len2-1)
{
printf("\n");
break;
}
}
return 0;
}
calculate (x,y,z);
DEBUG printf("TERMINOU DE CALCULAR\n");
printer(z);
inverter(z);
return 0;
}