Added pieces of code
This commit is contained in:
commit
b93d5547d5
|
@ -0,0 +1,71 @@
|
|||
# Cronograma
|
||||
|
||||
- [ ] Leiaute
|
||||
|
||||
- [ ] Documentação
|
||||
|
||||
- [ ] Vetores
|
||||
|
||||
- [ ] Recursão e algoritmos recursivos
|
||||
|
||||
- [ ] Listas encadeadas
|
||||
|
||||
- [ ] Filas
|
||||
|
||||
- [ ] Pilhas
|
||||
|
||||
- [ ] Busca em vetor ordenado
|
||||
|
||||
- [ ] Ordenação: algoritmos elementares
|
||||
|
||||
- [ ] Mergesort
|
||||
|
||||
- [ ] Quicksort
|
||||
|
||||
- [ ] Heapsort
|
||||
|
||||
- [ ] Ordenação digital
|
||||
|
||||
- [ ] Hashing
|
||||
|
||||
- [ ] Busca de palavras em um texto
|
||||
|
||||
- [ ] Árvores binárias
|
||||
|
||||
- [ ] Árvores binárias de busca
|
||||
|
||||
- [ ] Algoritmos de enumeração
|
||||
|
||||
## Recursos da linguagem C
|
||||
|
||||
- [ ] Bytes, números e caracteres
|
||||
|
||||
- [ ] Os tipos `int` e `char`
|
||||
|
||||
- [ ] Manipulação de `strings`
|
||||
|
||||
- [ ] Entrada e saída
|
||||
|
||||
- [ ] Endereços e ponteiros
|
||||
|
||||
- [ ] Alocação dinâmica d ememória
|
||||
|
||||
- [ ] Leitura de linhas de texto
|
||||
|
||||
- [ ] Números aleatórios
|
||||
|
||||
- [ ] Precedência de funções
|
||||
|
||||
- [ ] Bibliotecas de funções
|
||||
|
||||
- [ ] Tabela ASCII
|
||||
|
||||
- [ ] Unicode e UTF-8
|
||||
|
||||
- [ ] Compilação e depuração
|
||||
|
||||
- [ ] Pré-processamento
|
||||
|
||||
- [ ] Ferramentas e utilitários
|
||||
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char * toChar(int n) {
|
||||
char * str;
|
||||
str = malloc(sizeof(str));
|
||||
str[0] = (char) (n < 10) ? '0' + n : 'A' + n - 10;
|
||||
str[1] = '\0';
|
||||
return str;
|
||||
}
|
||||
|
||||
char * toHex(long long int n) {
|
||||
int r = n % 16;
|
||||
if (n - r == 0)
|
||||
return toChar(n);
|
||||
return strcat(toHex((n - r) / 16), toChar(r));
|
||||
}
|
||||
|
||||
int main () {
|
||||
long long int n;
|
||||
char div;
|
||||
|
||||
printf("Este programa lê inteiros positivos em notação decimal e os converte à notação hexacedimal. Digite quaisquer números naturais separados entre si por espaço e pressione ENTER para tê-los convertidos:\n");
|
||||
|
||||
do {
|
||||
if (!(scanf("%lld%c", &n, &div)) || n < 0) {
|
||||
printf("Valor inválido lido.\n");
|
||||
return 1;
|
||||
}
|
||||
printf("%lld → 0x%s\n", n, toHex(n));
|
||||
} while(div != '\n');
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int isBin (long long int n) {
|
||||
while (n != 0) {
|
||||
if (n % 10 > 1)
|
||||
return 0;
|
||||
n /= 10;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
long long int toDec (int n) {
|
||||
int decimal = 0, pot = 1;
|
||||
while (n > 0) {
|
||||
decimal += n % 10 * pot;
|
||||
n /= 10;
|
||||
pot *= 2;
|
||||
}
|
||||
return decimal;
|
||||
}
|
||||
|
||||
char * toHex(long long int n) {
|
||||
long long int dec;
|
||||
char * hex, * string;
|
||||
string = malloc (19 * sizeof(string));
|
||||
hex = malloc (19 * sizeof(hex));
|
||||
string[0] = '\0';
|
||||
|
||||
do {
|
||||
dec = toDec(n % 10000);
|
||||
hex[0] = (char) (dec < 10) ? '0' + dec : 'A' + dec - 10;
|
||||
hex[1] = '\0';
|
||||
hex = strcat(hex, string);
|
||||
string = strcpy (string, hex);
|
||||
n /= 10000;
|
||||
} while (n > 0);
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
int main () {
|
||||
long long int n;
|
||||
char div;
|
||||
|
||||
printf("Este programa lê inteiros positivos em notação binária e os converte à notação hexacedimal. Digite zeros e uns, separados ou não entre si por espaço, e pressione ENTER para tê-los covertidos em uma notação hexadecimal:\n");
|
||||
|
||||
do {
|
||||
if (!(scanf("%lld%c", &n, &div)) || n < 0 || !(isBin(n))) {
|
||||
printf("Valor inválido lido.\n");
|
||||
return 1;
|
||||
}
|
||||
printf("%lld → 0x%s\n", n, toHex(n));
|
||||
} while(div != '\n');
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct array {
|
||||
int i, size;
|
||||
char * index;
|
||||
} number;
|
||||
|
||||
int main () {
|
||||
short int value, add;
|
||||
char c;
|
||||
number bin, hex;
|
||||
|
||||
printf("Este programa lê um número natural em notação binária e o converte em notação hexadecimal. Digite uma série de uns e zeros, separados ou não entre si por espaços, e pressione ENTER para realizar a conversão:\n");
|
||||
|
||||
/* Read binary number */
|
||||
bin.i = 0;
|
||||
bin.size = 4;
|
||||
bin.index = malloc(4 * sizeof(bin.index));
|
||||
while ((c = getchar()) != EOF && c != '\n') {
|
||||
if (c == ' ')
|
||||
continue;
|
||||
if (c != '0' && c != '1') {
|
||||
printf("Valor inválido detectado\n");
|
||||
return 1;
|
||||
}
|
||||
if ((bin.i + 1) == bin.size) {
|
||||
bin.size += 2;
|
||||
bin.index = realloc(bin.index, bin.size * sizeof(bin.index));
|
||||
}
|
||||
bin.index[bin.i] = c;
|
||||
bin.i++;
|
||||
}
|
||||
if (bin.i < 1)
|
||||
return 1;
|
||||
|
||||
/* Conversion algorithm */
|
||||
hex.i = bin.i / 4;
|
||||
hex.i += (bin.i % 4 > 0) ? 1 : 0;
|
||||
hex.index = malloc(hex.i * sizeof(hex.index));
|
||||
hex.index[hex.i] = '\0';
|
||||
for (hex.i--; hex.i >= 0; hex.i--) {
|
||||
value = 0;
|
||||
for (add = 1; add < 9; add *= 2) {
|
||||
bin.i--;
|
||||
if (bin.index[bin.i] == '1')
|
||||
value += add;
|
||||
if (bin.i == 0)
|
||||
break;
|
||||
}
|
||||
hex.index[hex.i] = (char) (value < 10) ? '0' + value : 'A' + value - 10;
|
||||
}
|
||||
|
||||
printf("\nNotação hexadecimal: 0x%s\n", hex.index);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int main () {
|
||||
wchar_t c;
|
||||
|
||||
setlocale(LC_CTYPE, "pt_BR.UTF-8");
|
||||
wprintf(L"Este programa lê um texto terminado com ENTER e mostra o valor de cada caractere neste na tabela ASCII. Escreva algo:\n");
|
||||
|
||||
while ((c = getwchar()) != WEOF && c != '\n') {
|
||||
wprintf(L"%03d | %c\n", (int) c, c);
|
||||
}
|
||||
wprintf(L"%-03d | %s\n", 10, "ENTER");
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int main () {
|
||||
wchar_t c, div;
|
||||
|
||||
setlocale(LC_CTYPE, "pt_BR.UTF-8");
|
||||
wprintf(L"Este programa lê uma sequência de números inteiros positivos, maiores que 31 e menores que 256, separados por espaço, e os corresponde a caracteres na tabela ASCII. Liste-os e pressione ENTER:\n");
|
||||
|
||||
do {
|
||||
scanf("%d%lc", &c, &div);
|
||||
wprintf(L"%03d | %c\n", (int) c, c );
|
||||
} while(div != '\n');
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int main () {
|
||||
wchar_t a, b;
|
||||
|
||||
setlocale(LC_CTYPE, "pt_BR.UTF-8");
|
||||
wprintf(L"Este programa lê dois caracteres e compara o valor destes na Tabela ASCII. Digite dois caracteres separados entre si por um espaço: ");
|
||||
wscanf(L"%lc %lc", &a, &b);
|
||||
if (a > b)
|
||||
wprintf(L"%lc (%d) > %lc (%d)\n", a, a, b, b);
|
||||
else if (a < b)
|
||||
wprintf(L"%lc (%d) > %lc (%d)\n", b, b, a, a);
|
||||
else
|
||||
wprintf(L"%lc (%d) = %lc (%d)\n", a, a, b, b);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
int main (void) {
|
||||
printf ("sizeof (unsigned): %lu\n",
|
||||
sizeof (unsigned));
|
||||
printf ("UINT_MAX: %u, ou %x\n",
|
||||
UINT_MAX, UINT_MAX);
|
||||
printf ("sizeof (int) = %lu, ou %x\n",
|
||||
sizeof (int), (int) sizeof (int));
|
||||
printf ("INT_MIN: %d, ou %x\nINT_MAX: %d, ou %x\n",
|
||||
INT_MIN, INT_MIN, INT_MAX, INT_MAX);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#include<stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Size of int = %ld\n", sizeof(int));
|
||||
printf("Size of long = %ld\n", sizeof(long));
|
||||
printf("Size of long long = %ld\n", sizeof(long long));
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main () {
|
||||
unsigned char u, v, w;
|
||||
|
||||
u = 255; v = 2;
|
||||
printf ("%d\n", u + v);
|
||||
w = u + v;
|
||||
printf ("%d\n", w);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <locale.h>
|
||||
#include <wchar.h>
|
||||
|
||||
void substr (wchar_t * str, int a, int b) {
|
||||
unsigned int i;
|
||||
for (i = 0; i + a <= b; i++)
|
||||
str[i] = str[i + a];
|
||||
str[i] = '\0';
|
||||
}
|
||||
|
||||
int main () {
|
||||
int i, a, b, size = 1;
|
||||
wchar_t c, * str;
|
||||
str = malloc(sizeof(str));
|
||||
|
||||
setlocale(LC_CTYPE, "pt_BR.UTF-8");
|
||||
wprintf(L"Este programa lê uma sequência de texto terminada com ENTER e depois permite ao usuário selecionar um trecho entre dois caracteres para ser exibido.\n");
|
||||
|
||||
wprintf(L"\nEscreva algo: ");
|
||||
for (i = 0; (c = getwchar()) != WEOF && c != '\n'; i++) {
|
||||
if (i == size - 1) {
|
||||
size *= 2;
|
||||
str = realloc(str, size * sizeof(str));
|
||||
}
|
||||
str[i] = c;
|
||||
}
|
||||
str[i] = '\0';
|
||||
|
||||
wprintf(L"\nDigite dois valores inteiros e positivos a e b separados por espaço, onde 0 <= a <= b <= %d, para demarcar a seleção da sequência de texto: ", i - 1);
|
||||
wscanf(L"%d %d", &a, &b);
|
||||
if (!(a >= 0 && b >= a && b < i)) {
|
||||
wprintf(L"Valores inválidos passados.\n");
|
||||
return 1;
|
||||
}
|
||||
substr(str, a, b);
|
||||
wprintf(L"\nTexto selecionado:\n%ls\n", str);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <locale.h>
|
||||
#include <wchar.h>
|
||||
|
||||
wchar_t * substr (wchar_t str[], int a, int b) {
|
||||
unsigned int i;
|
||||
wchar_t * sub;
|
||||
sub = malloc((b - a + 1) * sizeof(sub));
|
||||
for (i = 0; i + a <= b; i++)
|
||||
sub[i] = str[i + a];
|
||||
sub[i] = '\0';
|
||||
return sub;
|
||||
}
|
||||
|
||||
int main () {
|
||||
int i, a, b, size = 1;
|
||||
wchar_t c, * str;
|
||||
str = malloc(sizeof(str));
|
||||
|
||||
setlocale(LC_CTYPE, "pt_BR.UTF-8");
|
||||
wprintf(L"Este programa lê uma sequência de texto terminada com ENTER e depois permite ao usuário selecionar um trecho entre dois caracteres para ser exibido.\n");
|
||||
|
||||
wprintf(L"\nEscreva algo: ");
|
||||
for (i = 0; (c = getwchar()) != WEOF && c != '\n'; i++) {
|
||||
if (i == size - 1) {
|
||||
size *= 2;
|
||||
str = realloc(str, size * sizeof(str));
|
||||
}
|
||||
str[i] = c;
|
||||
}
|
||||
str[i] = '\0';
|
||||
|
||||
wprintf(L"\nDigite dois valores inteiros e positivos a e b separados por espaço, onde 0 <= a <= b <= %d, para demarcar a seleção da sequência de texto: ", i - 1);
|
||||
wscanf(L"%d %d", &a, &b);
|
||||
if (!(a >= 0 && b >= a && b < i)) {
|
||||
wprintf(L"Valores inválidos passados.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
wprintf(L"\nTexto selecionado:\n%ls\n", substr(str, a, b));
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool byte_diff (char * a, char * b) {
|
||||
unsigned int i, count = 0;
|
||||
|
||||
for (i = 0; a[i] != '\0' || b[i] != '\0'; i++){
|
||||
if (a[i] != b[i])
|
||||
count++;
|
||||
if (count > 1)
|
||||
return false;
|
||||
}
|
||||
if (b[i] != '\0' || a[i] != '\0')
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
char * toString (char c) {
|
||||
char * string;
|
||||
string = malloc(2 * sizeof(string));
|
||||
string[0] = c;
|
||||
string[1] = '\0';
|
||||
return string;
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <locale.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int main () {
|
||||
int i, a, b, size = 1;
|
||||
wchar_t c, * str;
|
||||
str = malloc(sizeof(str));
|
||||
|
||||
setlocale(LC_CTYPE, "pt_BR.UTF-8");
|
||||
wprintf(L"Este programa lê duas sequências de texto terminada com ENTER e conta quantas vezes a segunda sequência de texto ocorre na primeira.\n");
|
||||
|
||||
wprintf(L"\nEscreva algo: ");
|
||||
for (i = 0; (c = getwchar()) != WEOF && c != '\n'; i++) {
|
||||
if (i == size - 1) {
|
||||
size *= 2;
|
||||
str = realloc(str, size * sizeof(str));
|
||||
}
|
||||
str[i] = c;
|
||||
}
|
||||
str[i] = '\0';
|
||||
|
||||
wprintf(L"\nDigite dois valores inteiros e positivos a e b separados por espaço, onde 0 <= a <= b <= %d, para demarcar a seleção da sequência de texto: ", i - 1);
|
||||
wscanf(L"%d %d", &a, &b);
|
||||
if (!(a >= 0 && b >= a && b < i)) {
|
||||
wprintf(L"Valores inválidos passados.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
wprintf(L"\nTexto selecionado:\n%ls\n", substr(str, a, b));
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
# Algoritmos em C
|
||||
|
||||
Repositório de [resumos](Resumos) e [exercícios](Exerc%C3%ADcios) feitos à partir da leitura da referida [apostila](http://www.ime.usp.br/~pf/algoritmos/).
|
||||
|
||||
## Para compilar
|
||||
|
||||
Algumas das resoluções feitas requerem o uso da flag `-lm` para serem compiladas; outras requerem `-std=c99`. Para qualquer eventualidade, utilize ambas:
|
||||
|
||||
```c
|
||||
gcc <input>.c -o <output> -lm -std=c99
|
||||
```
|
Loading…
Reference in New Issue