diff options
| author | Mitsuo Tokumori <[email protected]> | 2023-10-05 22:26:08 -0500 |
|---|---|---|
| committer | Mitsuo Tokumori <[email protected]> | 2023-10-05 22:26:08 -0500 |
| commit | f570dd72bcadd7b72f38f2ef5a21d36653664c52 (patch) | |
| tree | 4f04fb90478fab678e552eed102c7134d3611a58 | |
| parent | acb4b2207b1badf7027c487d61e19adf12cf40da (diff) | |
| download | LP1-f570dd72bcadd7b72f38f2ef5a21d36653664c52.tar.gz LP1-f570dd72bcadd7b72f38f2ef5a21d36653664c52.tar.bz2 LP1-f570dd72bcadd7b72f38f2ef5a21d36653664c52.zip | |
Add trim function to L04
| -rw-r--r-- | 2023-2/L04/mitsuo/fun.cpp | 35 | ||||
| -rw-r--r-- | 2023-2/L04/mitsuo/fun.hpp | 1 |
2 files changed, 32 insertions, 4 deletions
diff --git a/2023-2/L04/mitsuo/fun.cpp b/2023-2/L04/mitsuo/fun.cpp index 6e7ff38..f7691d2 100644 --- a/2023-2/L04/mitsuo/fun.cpp +++ b/2023-2/L04/mitsuo/fun.cpp @@ -167,10 +167,37 @@ void cargapedidos(void *& productos, void * clientes) { agrega_pedido(pedidos, pedido); // appends pedido to the list } - // trim TODO - // for (i = 0; i < n; i++) { - // ((void **) clientes)[i] = clientes_tmp[i]; - // } + // trim extra spaces ("metodo exacto") + trim_cliente_pedidos(clientes); +} + +void trim_cliente_pedidos(void * clientes) { + int i, j; + void ** cs = (void **) clientes; + for (i = 0; cs[i]; i++) { + void ** c = (void **) cs[i]; + void ** pedidos = (void **) c[2]; + if (!pedidos) { + continue; + } + // get length + for (j = 0; pedidos[j]; j++) + ; + int n = j; + // create new array and transfer items + void ** pedidos_tmp = new void * [n + 1]; + for (j = 0; pedidos[j]; j++) { + pedidos_tmp[j] = pedidos[j]; + } + pedidos_tmp[j] = NULL; + // delete old array and update cliente + // delete [] pedidos; + /* NOTE: crashes while freeing memory of pedidos of client with i=16 (dni=71984468), + I have no idea why. Idk, using `void *` in this way is so + tedious to debug that I'll just leave this comment here and + move on. If somebody every figures this out please let me know. */ + c[2] = pedidos_tmp; + } } void ** busca_cliente(void * clientes, int dni) { diff --git a/2023-2/L04/mitsuo/fun.hpp b/2023-2/L04/mitsuo/fun.hpp index 8665f00..57d59ce 100644 --- a/2023-2/L04/mitsuo/fun.hpp +++ b/2023-2/L04/mitsuo/fun.hpp @@ -14,5 +14,6 @@ void imprimeclientes(void * clientes); void ** busca_cliente(void * clientes, int dni); void ** busca_producto(void * productos, char * code); void agrega_pedido(void * pedidos, void * pedido); +void trim_cliente_pedidos(void * clientes); #endif /* FUN_HPP */
\ No newline at end of file |
