-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
158 lines (138 loc) · 4.17 KB
/
array.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
/** @file Ficheiro que contém as funcionalidades relativas ao array */
#include "stack.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "string.h"
#include "parser.h"
char* get_array_content(char *array) {
array++;
char* content = malloc(sizeof(char) * 10240);
int number_of_left_square_brackets = 1, number_of_right_square_brackets = 0, i;
for (i = 0; number_of_left_square_brackets != number_of_right_square_brackets; array++, i++) {
if (*array == '[') number_of_left_square_brackets++;
else if (*array == ']') number_of_right_square_brackets++;
content[i] = *array;
}
content[i - 1] = '\0';
return content;
}
void create_array(STACK * s, int size) {
DATA a;
a.type = ARRAY;
a.array.size = size;
a.array.elems = malloc((sizeof(ARRAYS) + sizeof(struct data)) * size);
for (int i = size - 1; i >= 0; i--) {
a.array.elems[i] = pop(s);
}
push(s, a);
}
void range_array(STACK * s) {
if (has_type(s->stack[s->pos], INTEGER)) {
int size = pop_operand(s);
for (int i = 0; i < size; i++) {
push_INT(s, i);
}
create_array(s, size);
}
else if (has_type(s->stack[s->pos], ARRAY)) {
DATA a = pop(s);
push_INT(s, a.array.size);
}
}
void multiply_array(STACK * s) {
int n_times = pop_INT(s);
DATA a = pop(s);
a.array.elems = realloc(a.array.elems, sizeof(struct data) * a.array.size * n_times);
for (int i = 1; i < n_times; i++) {
for (int j = i * a.array.size, k = 0; j < (i+1) * a.array.size; j++, k++) {
a.array.elems[j] = a.array.elems[k];
}
}
a.array.size *= n_times;
push(s, a);
}
void array_element_by_index(STACK * s) {
int index = pop_INT(s);
DATA a = pop(s);
push(s, a.array.elems[index]);
}
void array_to_stack(STACK * s) {
DATA a = pop(s);
for (int i = 0; i < a.array.size; i++) {
push(s, a.array.elems[i]);
}
}
void concatenate_array_element(STACK *s) {
DATA elem = pop(s);
int old_size = top(s).array.size;
array_to_stack(s);
push(s, elem);
create_array(s, old_size + 1);
}
void concatenate_element_array(STACK *s) {
DATA a = pop(s);
DATA elem = pop(s);
a.array.size += 1;
a.array.elems = realloc(a.array.elems, (sizeof(ARRAYS) + sizeof(struct data)) * a.array.size);
for (int i = a.array.size - 1; i >= 1; i--) {
a.array.elems[i] = a.array.elems[i - 1];
}
a.array.elems[0] = elem;
push(s, a);
}
void concatenate_array_array (STACK *s) {
DATA a1 = pop(s);
DATA a2 = pop(s);
int size = a2.array.size + a1.array.size;
a2.array.elems = realloc(a2.array.elems, (sizeof(ARRAYS) + sizeof(struct data)) * size);
for (int i = a2.array.size, j = 0; i < size; i++, j++) {
a2.array.elems[i] = a1.array.elems[j];
}
a2.array.size = size;
push(s, a2);
}
void concatenate_array(STACK *s) {
if (!has_type(s->stack[s->pos], ARRAY) && has_type(s->stack[s->pos - 1], ARRAY)) {
concatenate_array_element(s); return;
}
if (has_type(s->stack[s->pos], ARRAY) && !has_type(s->stack[s->pos - 1], ARRAY)) {
concatenate_element_array(s); return;
}
if (has_type(s->stack[s->pos], ARRAY) && has_type(s->stack[s->pos - 1], ARRAY)) {
concatenate_array_array(s); return;
}
}
void remove_array_first_element(STACK *s) {
DATA a = pop(s);
DATA elem = a.array.elems[0];
for(int i = 1; i < a.array.size; i++) {
push(s, a.array.elems[i]);
}
create_array(s, a.array.size - 1);
push(s, elem);
}
void remove_array_last_element(STACK *s) {
DATA a = pop(s);
DATA elem = a.array.elems[a.array.size - 1];
a.array.size -= 1;
push(s, a);
push(s, elem);
}
void get_array_elements_by_first (STACK *s) {
int n_elems = pop_INT(s);
DATA a = pop(s);
for (int i = 0; i < n_elems; i++) {
push(s, a.array.elems[i]);
}
create_array(s, n_elems);
}
void get_array_elements_by_last (STACK *s) {
int n_elems = pop_INT(s);
DATA a = pop(s);
for (int i = a.array.size - n_elems; i < a.array.size; i++) {
push(s, a.array.elems[i]);
}
create_array(s, n_elems);
}