Ir ao conteúdo

Matheus Maldi

Membro Pleno
  • Posts

    541
  • Cadastrado em

  • Última visita

Tudo que Matheus Maldi postou

  1. Desculpe, gostaria de dizer headphone
  2. Gostaria sim com microfone.
  3. Acabei comprando um JBL Quantum 400, veio 2 cabos, um USB e outro cabo de áudio(este extremamente curto), o fone funciona apenas com cabo de áudio, o USB provavelmente funcione junto com o software deles, mas quero apenas um plug and play, nada de programas.
  4. Gostaria de recomendações de headphone com os requisitos: Somente 1 cabo e tem que ser USB Sem compativel com Linux. Valor até 1.000,00 reais
  5. Você pode fazer mas não deve, é muito provável que por ser um jogo o loop infinito faça o aplicativo comer a bateria do celular. O Android Studio também pode "desenhar" em vez de apenas codificar. Mas aprenda uma coisa, você vai passar a maior parte do tempo programando, e não desenhando, aprenda mesmo a programar, e aprenda o código que a IDE escreve para "desenhar".
  6. Bom, segue algumas recomendações: Use o compilador Clang, funciona em todas as plataformas nunca terá problema com licença. Não use funções que possam causar overflow. Aprenda usar algum sistema de build, eu gosto do meson+ninja, mas aprenda o make e quando perceber que precisa de algo a mais, vá para o meson. Prefira usar calloc em vez de malloc. Use valgrind para verificar se tem algum leak. Prefira este Style Guide: https://cs50.readthedocs.io/style/c/ Veja este video: https://www.youtube.com/watch?v=443UNeGrFoM
  7. Domine a linguagem, e você saberá o que deve ou não usar, apesar de obsoleta ninguém conseguiu se livrar dela. É uma linguagem que vale a pena aprender. No momento acredito que talvez Rust consiga tomar seu lugar.
  8. Segue minha implementação pessoa da Stack(pilha), e é genérica. #ifndef _STACK_H_ #define _STACK_H_ #if __cplusplus extern "C" { #endif #include <stdbool.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define INCREMENT (256) typedef int stack_status; struct stack { void* base; size_t capacity; size_t element_size; size_t length; }; stack_status stack_ctor_sized (struct stack* stack, size_t element_size, size_t capacity); stack_status stack_ctor (struct stack* stack, size_t element_size); void stack_dtor (struct stack* stack); bool stack_empty (struct stack* stack); stack_status stack_push (struct stack* stack, void* element); void* stack_peek (struct stack* stack); void* stack_pop (struct stack* stack); stack_status stack_ctor_sized(struct stack* stack, size_t element_size, size_t capacity) { stack->base = calloc(1, element_size * capacity); if (stack->base == NULL) return ENOMEM; stack->capacity = capacity; stack->element_size = element_size; stack->length = 0; return EXIT_SUCCESS; } stack_status stack_ctor(struct stack* stack, size_t element_size) { return stack_ctor_sized(stack, element_size, INCREMENT); } void stack_dtor(struct stack* stack) { free(stack->base); } bool stack_empty(struct stack* stack) { return (stack->length == 0); } void* stack_end(struct stack* stack) { return (char*)stack->base + (stack->element_size * stack->length); } void* stack_peek(struct stack* stack) { return (char*)stack->base + (stack->element_size * (stack->length - 1)); } stack_status stack_push(struct stack* stack, void* element) { if (stack->length == stack->capacity) { void* base = realloc(stack->base, (stack->element_size * (stack->capacity + INCREMENT))); if (unlikely(base == NULL)) return ENOMEM; stack->base = base; stack->capacity += INCREMENT; } memcpy(stack_end(stack), element, stack->element_size); stack->length++; return EXIT_SUCCESS; } void* stack_pop(struct stack* stack) { if (stack_empty(stack)) return NULL; stack->length--; return stack_end(stack); } #if __cplusplus } #endif #endif Segue um caso real de uso para a solução de um problema, o rat in maze: #include <stdio.h> #include <stdbool.h> #include "stack.h" #define N 4 #define M 5 int maze[N][M] = { { 1, 0, 1, 1, 1 }, { 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1 }, { 1, 1, 1, 0, 1 } }; int fx = 3; int fy = 4; struct node { int x, y, dir; }; bool is_reachable() { // Initially starting at (0, 0). int i = 0, j = 0; bool visited[N][M]; memset(visited, true, sizeof(visited)); struct stack s; stack_ctor(&s, sizeof(struct node)); struct node temp = { i, j, 0 }; stack_push(&s, &temp); while (!stack_empty(&s)) { // Pop the top node and move to the left, right, top, down or // retract back according the value of node's dir variable. struct node* tmp = stack_peek(&s); int d = tmp->dir++; i = tmp->x, j = tmp->y; // If we reach the Food coordinates return true if (i == fx && j == fy) return true; // Checking the Up direction. if (d == 0) { if (i - 1 >= 0 && maze[i - 1][j] && visited[i - 1][j]) { struct node temp1 = { i - 1, j, 0 }; visited[i - 1][j] = false; stack_push(&s, &temp1); } } // Checking the left direction else if (d == 1) { if (j - 1 >= 0 && maze[i][j - 1] && visited[i][j - 1]) { struct node temp1 = { i, j - 1, 0 }; visited[i][j - 1] = false; stack_push(&s, &temp1); } } // Checking the down direction else if (d == 2) { if (i + 1 < N && maze[i + 1][j] && visited[i + 1][j]) { struct node temp1 = { i + 1, j, 0 }; visited[i + 1][j] = false; stack_push(&s, &temp1); } } // Checking the right direction else if (d == 3) { if (j + 1 < M && maze[i][j + 1] && visited[i][j + 1]) { struct node temp1 = { i, j + 1, 0 }; visited[i][j + 1] = false; stack_push(&s, &temp1); } } // If none of the direction can take the rat to the Food, // retract back to the path where the rat came from. else { visited[temp.x][temp.y] = true; stack_pop(&s); } } stack_dtor(&s); // If the stack is empty and no path is found return false. return false; } int main() { if (is_reachable()) printf("Path Found!\n"); else printf("No Path Found!\n"); return 0; } O algoritmo acima foi convertido do codigo em C++ de https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-using-stack/ A performance da Stack implementada deve ser a mais rápida possível.
  9. Não compilei seu código mas acredito que o faltou printar a nota do exame cin >> exame; cout << "Nota do exame: " << exame << endl;
  10. É mentira, a não ser que queira criar aplicações vazias e extremamente simples. Esse Outsystems usa o antigo WebForms da microsoft, que está descontinuado, e nunca deveria ter existido. Se você tem um sistema e precisa validar um CPF em algum campo? Vai ter que programar...
  11. São uteis, implementações do proprio framework, para uso do out string line = Console.ReadLine(); int n; if (int.TryParse(line, out n)) { // Conseguiu converter } else { // não conseguiu converter } Ou a implementação de um Swap utiliza-se ref void Swap(ref int x, ref int y) { int tmp = x; x = y; y = x; } Raramente você vai precisar usar goto, mas ele é util, normalmente para escapar de loop dentro de loop. Segue 2 codigos abaixo, porém em C, segue primeiro o sem goto: void quick_sort_int (int* array, int i_lo, int i_hi) { int lo = i_lo, hi = i_hi; int mid = array[((lo + hi) >> 1)]; do { while (array[lo] < mid) ++lo; while (array[hi] > mid) --hi; if (lo <= hi) { int temp = array[lo]; array[lo] = array[hi]; array[hi] = temp; lo++; hi--; } } while (lo < hi); if (hi > i_lo) quick_sort_int (array, i_lo, hi); if (lo < i_hi) quick_sort_int (array, lo, i_hi); } O codigo abaixo usa goto para evitar chamadas recursivas void quick_sort_int_goto (int* array, int i_lo, int i_hi) { skip_recursive_call:; int lo = i_lo, hi = i_hi; int mid = array[((lo + hi) >> 1)]; do { while (array[lo] < mid) ++lo; while (array[hi] > mid) --hi; if (lo <= hi) { int temp = array[lo]; array[lo] = array[hi]; array[hi] = temp; lo++; hi--; } } while (lo < hi); if (hi > i_lo) { if (lo < i_hi) { quick_sort_int_goto (array, i_lo, hi); i_lo = lo; goto skip_recursive_call; } else { i_hi = hi; goto skip_recursive_call; } } else if (lo < i_hi) { i_lo = lo; goto skip_recursive_call; } } Não se preocupe em entender a logica do código acima, é escrito para ser performatico e não legivel. O primeiro codigo ordena um array de 100.000.000 inteiros randomicos em 9,4 segundos e o segundo em 8.1 segundos. Apesar de usar raramente o goto, as vezes tem sua utilidade.
  12. Ao utilizar fibonacci, recomendo usar uma array para guardar todos os fibonaccis possiveis, a partir disso há um overflow para uint64. #define FIB_MAX (94) static unsigned long long fib[FIB_MAX] = { }; static void fill_fib() { fib[0] = 0; fib[1] = 1; for (unsigned long long i = 2; i < FIB_MAX; i++) fib[i] = fib[i - 1] + fib[i - 2]; } E para ver se um numero é primo use: bool eh_primo(unsigned long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } O resultado da combinação das 2 funções: $ time ./fib 2 3 5 13 89 233 1597 28657 514229 433494437 2971215073 806515533049393 99194853094755497 real 0m0,002s user 0m0,002s sys 0m0,000s
  13. Procure por "float are evil" Basicamente é por que com 0.1 + 0.2 = 0.30000000000000004 dependendo da implementação do ponto flutuante O problema no seu algoritmo é que em algum momento a variável que você ficou somando que deveria ter valor de 0.01 fica com o valor 0.009999998, que é bem próximo mas não igual.
  14. Isso trata-se de um problema chamado ponto-flutuante da morte. Não irei explicar aqui este problema. Mas não use double ou float, e sim long. minha solução ficou assim: #include <stdio.h> int main() { long notas_centavos[6] = { 10000, 5000, 2000, 1000, 500, 200 }; long moedas_centavos[6] = { 100, 50, 25, 10, 5, 1 }; double valor; scanf("%lf", &valor); long centavos = valor * 100; printf("NOTAS:\n"); for (int i = 0; i < 6; i++) { int no_notas = centavos / notas_centavos[i]; centavos %= notas_centavos[i]; printf("%d nota(s) de R$ %.2lf\n", no_notas, (notas_centavos[i] / 100.0)); } printf("MOEDAS:\n"); for (int i = 0; i < 6; i++) { int no_moeda = centavos / moedas_centavos[i]; centavos %= moedas_centavos[i]; printf("%d moeda(s) de R$ %.2lf\n", no_moeda, (moedas_centavos[i] / 100.0)); } return 0; }
  15. O problema deve estar na formatação, você está escrevendo: nota(s) de R$ 00.50 Em vez de: nota(s) de R$ 0.50
  16. Havia a pouco tempo editado para fazer isso
  17. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> typedef struct { union { int int_value; char char_value; }; bool is_int; } int_or_char; void make_int(int v, int_or_char* result) { result->is_int = true; result->char_value = 0; result->int_value = v; } void make_char(char v, int_or_char* result) { result->is_int = false; result->int_value = 0; result->char_value = v; } int main() { int_or_char matrix[2][2]; make_int(10, &matrix[0][0]); make_char('c', &matrix[0][1]); make_int(20, &matrix[1][0]); make_char('d', &matrix[1][1]); for (size_t x = 0; x < 2; x++) { for (size_t y = 0; y < 2; y++) { int_or_char* p = &matrix[x][y]; if (p->is_int) printf("matrix[%zu][%zu] = %d\n", x, y, p->int_value); else printf("matrix[%zu][%zu] = %c\n", x, y, p->char_value); } } return 0; } Veja se isso ajuda
  18. Inteiro não pode ser dividido por ZERO.
  19. Inicie uma variavel chamada level com valor 0; Faça um loop que percorra caracter por caracter: Se o caracter for igual '(' acrescente 1 ao level. Se o caracter for igual ')' : Verifique se o level: Se igual a zero é a expressão é incorreta. (fechando o parenteses antes de abrir). Senão decrescente 1 ao level. Ao fim do loop, verifique se o valor do level é 0;
  20. printf("DIGITE UM OPERADOR: "); scanf("%c", &operador); O scanf do operador não deveria ser um char?
  21. O desafio está completo #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char* turma; char nome[100]; int cod; } Pessoa; int pessoa_cod_cmp(const void* pessoa1, const void* pessoa2) { Pessoa* p1 = (Pessoa*)pessoa1; Pessoa* p2 = (Pessoa*)pessoa2; if (p1->cod > p2->cod) return 1; if (p1->cod < p2->cod) return -1; return strcmp(p1->nome, p2->nome); } int pessoa_nome_cmp(const void* pessoa1, const void* pessoa2) { Pessoa* p1 = (Pessoa*)pessoa1; Pessoa* p2 = (Pessoa*)pessoa2; int nome_cmp = strcmp(p1->nome, p2->nome); if (nome_cmp > 0) return 1; if (nome_cmp < 0) return -1; if (p1->cod > p2->cod) return 1; if (p1->cod < p2->cod) return -1; return 0; } static inline void swap (void* l, void* r, size_t size) { char *l_ptr = l, *r_ptr = r; do { char tmp = *l_ptr; *l_ptr++ = *r_ptr; *r_ptr++ = tmp; } while (--size > 0); } void bubble_sort (const void * pbase, size_t total_elems, size_t size_of_elem, int (*cmp)(const void*, const void*)) { if (total_elems < 1) return; void* last = (void*)pbase + (total_elems * size_of_elem) - size_of_elem; for (void* curr = (void*)pbase; curr < last; curr += size_of_elem) { void* next = curr + size_of_elem; if (cmp(curr, next) > 0) swap(curr, next, size_of_elem); } bubble_sort(pbase, total_elems - 1, size_of_elem, cmp); } int main() { char turma1_buffer[100]; scanf("%s", turma1_buffer); size_t turma1_length; scanf("%lu", &turma1_length); Pessoa* turma1 = (Pessoa*)calloc(turma1_length, sizeof(Pessoa)); for (size_t i = 0; i < turma1_length; i++) { turma1[i].turma = turma1_buffer; scanf("%s%d", turma1[i].nome, &(turma1[i].cod)); } char turma2_buffer[100]; scanf("%s", turma2_buffer); size_t turma2_length; scanf("%lu", &turma2_length); size_t turma1e2_length = turma1_length + turma2_length; Pessoa* turma1e2 = (Pessoa*)realloc(turma1, sizeof(Pessoa) * (turma1e2_length)); for (size_t i = turma1_length; i < turma1e2_length; i++) { turma1e2[i].turma = turma2_buffer; scanf("%s%d", turma1e2[i].nome, &(turma1e2[i].cod)); } char prioridade[20]; scanf("%s", prioridade); if (strcmp("COD", prioridade) == 0) bubble_sort(turma1e2, turma1e2_length, sizeof(Pessoa), pessoa_cod_cmp); else if (strcmp("NOME", prioridade) == 0) bubble_sort(turma1e2, turma1e2_length, sizeof(Pessoa), pessoa_nome_cmp); for (size_t i = 0; i < turma1e2_length; i++) printf("%s %d %s\n", turma1e2[i].turma, turma1e2[i].cod, turma1e2[i].nome); free(turma1e2); return 0; }
  22. Eu não havia lido o enunciado. Fiz exatamente isso no post anterior. Bom, acha de devo escrever um bubble sort genérico?
  23. É realmente, não havia lido enunciado. Agora sim: #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char* turma; char nome[100]; int cod; } Pessoa; int pessoa_cod_cmp(const void* pessoa1, const void* pessoa2) { Pessoa* p1 = (Pessoa*)pessoa1; Pessoa* p2 = (Pessoa*)pessoa2; if (p1->cod > p2->cod) return 1; if (p1->cod < p2->cod) return -1; return strcmp(p1->nome, p2->nome); } int pessoa_nome_cmp(const void* pessoa1, const void* pessoa2) { Pessoa* p1 = (Pessoa*)pessoa1; Pessoa* p2 = (Pessoa*)pessoa2; int nome_cmp = strcmp(p1->nome, p2->nome); if (nome_cmp > 0) return 1; if (nome_cmp < 0) return -1; if (p1->cod > p2->cod) return 1; if (p1->cod < p2->cod) return -1; return 0; } int main() { char turma1_buffer[100]; scanf("%s", turma1_buffer); size_t turma1_length; scanf("%lu", &turma1_length); Pessoa* turma1 = (Pessoa*)calloc(turma1_length, sizeof(Pessoa)); for (size_t i = 0; i < turma1_length; i++) { turma1[i].turma = turma1_buffer; scanf("%s%d", turma1[i].nome, &(turma1[i].cod)); } char turma2_buffer[100]; scanf("%s", turma2_buffer); size_t turma2_length; scanf("%lu", &turma2_length); size_t turma1e2_length = turma1_length + turma2_length; Pessoa* turma1e2 = (Pessoa*)realloc(turma1, sizeof(Pessoa) * (turma1e2_length)); for (size_t i = turma1_length; i < turma1e2_length; i++) { turma1e2[i].turma = turma2_buffer; scanf("%s%d", turma1e2[i].nome, &(turma1e2[i].cod)); } char prioridade[20]; scanf("%s", prioridade); if (strcmp("COD", prioridade) == 0) qsort(turma1e2, turma1e2_length, sizeof(Pessoa), pessoa_cod_cmp); else if (strcmp("NOME", prioridade) == 0) qsort(turma1e2, turma1e2_length, sizeof(Pessoa), pessoa_nome_cmp); for (size_t i = 0; i < turma1e2_length; i++) printf("%s %d %s\n", turma1e2[i].turma, turma1e2[i].cod, turma1e2[i].nome); free(turma1e2); return 0; }
  24. Use PascalCase na programação de C# public class Lampada { public bool Estado { get; private set; } public Lampada(bool estado) { Estado = estado; } public void Acender() { Estado = true; } public void Apagar() { Estado = false; } }

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...