diff --git a/U0 - Nivelamento/c/aquivo/arq01.c b/U0 - Nivelamento/c/aquivo/arq01.c new file mode 100644 index 0000000..c2b110f --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq01.c @@ -0,0 +1,18 @@ +#include +#include + + + +int main(int argc, char *argv[]) { + + FILE *p = fopen ("teste.txt", "r"); + // testa se o arquivo foi aberto com sucesso + if (p != NULL) { + printf ("Arquivo foi aberto com sucesso.\n\n"); + fclose(p); + } else { + printf ("Nao foi possivel abrir o arquivo.\n\n"); + } + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq02.c b/U0 - Nivelamento/c/aquivo/arq02.c new file mode 100644 index 0000000..af77220 --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq02.c @@ -0,0 +1,19 @@ +#include +#include + + + +int main(int argc, char *argv[]) { + + FILE *p = fopen ("teste.txt", "r"); + + // testa se o arquivo foi aberto com sucesso + if (p != NULL) { + printf ("Arquivo foi aberto com sucesso.\n\n"); + fclose(p); + } else { + printf ("Nao foi possivel abrir o arquivo.\n\n"); + } + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq03.c b/U0 - Nivelamento/c/aquivo/arq03.c new file mode 100644 index 0000000..4a4fe7b --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq03.c @@ -0,0 +1,21 @@ +#include +#include + +int main(int argc, char *argv[]) { + + FILE *p = fopen (argv[1], "r"); + int ch; + + if (p != NULL) { + do { + ch = fgetc(p); + if(ch != EOF){ + printf( "%i %c\n", ch, (char)ch); + //printf( "%c", (char)ch); + } + } while (ch != EOF); + fclose(p); + } + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq04.c b/U0 - Nivelamento/c/aquivo/arq04.c new file mode 100644 index 0000000..8e7d1e8 --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq04.c @@ -0,0 +1,23 @@ +#include +#include + +int main(int argc, char *argv[]) { + + FILE *p = fopen ("teste.txt", "r"); + char str[100+1]; + char* resp; + int i = 0; + + if (p != NULL) { + do { + resp = fgets(str, 100, p); + if(resp != NULL){ + printf("(%d) %s", i, str); + i++; + } + } while (resp != NULL); + fclose(p); + } + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq05.c b/U0 - Nivelamento/c/aquivo/arq05.c new file mode 100644 index 0000000..7942f00 --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq05.c @@ -0,0 +1,12 @@ +#include +#include + +int main(int argc, char *argv[]) { + + FILE *p = fopen ("teste.txt", "w"); + fputc('M', p); + fclose(p); + + return 0; +} + diff --git a/U0 - Nivelamento/c/aquivo/arq06.c b/U0 - Nivelamento/c/aquivo/arq06.c new file mode 100644 index 0000000..59f2301 --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq06.c @@ -0,0 +1,11 @@ +#include +#include + +int main(int argc, char *argv[]) { + + FILE *p = fopen ("teste.txt", "w"); + fputs("Algoritmos e Estruturas de Dados II", p); + fclose(p); + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq07.c b/U0 - Nivelamento/c/aquivo/arq07.c new file mode 100644 index 0000000..58c76ac --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq07.c @@ -0,0 +1,19 @@ +#include +#include + +int main(int argc, char *argv[]) { + + FILE *in = fopen ("teste.txt", "r"), + *out = fopen ("copia.txt", "w"); + + while ( !feof(in) ) { + char ch = getc(in); + if ( !feof(in)) { + putc(ch, out); + } + } + + fclose(in); + fclose(out); + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq08.c b/U0 - Nivelamento/c/aquivo/arq08.c new file mode 100644 index 0000000..4272946 --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq08.c @@ -0,0 +1,24 @@ +#include +#include + +int main(int argc, char *argv[]) { + + FILE *p; + + double d = 12.23; + int i = 101; + long l = 123023L; + + if ((p = fopen("teste.txt", "wb")) == NULL) { + printf ("arquivo nao pode ser aberto\n"); + exit(1); + } + + fwrite(&d, sizeof(double), 1, p); + fwrite(&i, sizeof(int), 1, p); + fwrite(&l, sizeof(long), 1, p); + + fclose(p); + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq09.c b/U0 - Nivelamento/c/aquivo/arq09.c new file mode 100644 index 0000000..725fb36 --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq09.c @@ -0,0 +1,25 @@ +#include +#include + +int main(int argc, char *argv[]) { + + FILE *p; + + double d; + int i; + long l; + + if ((p = fopen("teste.txt", "rb")) == NULL) { + printf ("arquivo nao pode ser aberto\n"); + exit(1); + } + + fread(&d, sizeof(double), 1, p); + fread (&i, sizeof(int), 1, p); + fread (&l, sizeof(long), 1, p); + + printf("%f %d %ld", d, i, l); + fclose(p); + return 0; +} + diff --git a/U0 - Nivelamento/c/aquivo/arq10.c b/U0 - Nivelamento/c/aquivo/arq10.c new file mode 100644 index 0000000..b62fc8f --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq10.c @@ -0,0 +1,35 @@ +#include +#include + +typedef struct cliente { + char nome[100]; + int codigo; +} cliente; + +void escrever(char*); +void ler(char*); + +void main(int argc, char** argv){ + escrever("teste.txt"); + ler("teste.txt"); +} + +void escrever(char* nomeArq){ + cliente c1, c2; + strcat(c1.nome, "Ze da Silva"); c1.codigo = 1; + strcat(c2.nome, "Lele da Cuca"); c2.codigo = 11; + FILE *p = fopen(nomeArq, "ab"); + fwrite(&c1, sizeof(cliente), 1, p); + fwrite(&c2, sizeof(cliente), 1, p); + fclose(p); +} + +void ler(char* nomeArq){ + cliente c1, c2; + FILE *p = fopen(nomeArq, "rb"); + fread(&c1, sizeof(cliente), 1, p); + fread(&c2, sizeof(cliente), 1, p); + fclose(p); + printf("%s -- %d\n", c1.nome, c1.codigo); + printf("%s -- %d\n", c2.nome, c2.codigo); +} diff --git a/U0 - Nivelamento/c/aquivo/arq11.c b/U0 - Nivelamento/c/aquivo/arq11.c new file mode 100644 index 0000000..3b7cf8a --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq11.c @@ -0,0 +1,23 @@ +#include + +int main(int argc, char *argv[]) { + FILE *p = fopen("teste.txt", "wb+"); + + double d = 12.23; int i = 101; long l = 123023L; + + + fwrite(&d, sizeof(double), 1, p); + fwrite(&i, sizeof(int), 1, p); + fwrite(&l, sizeof(long), 1, p); + + rewind(p); + + fread(&d, sizeof(double), 1, p); + fread (&i, sizeof(int), 1, p); + fread (&l, sizeof(long), 1, p); + + printf("%f %d %ld", d, i, l); + fclose(p); + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arq12.c b/U0 - Nivelamento/c/aquivo/arq12.c new file mode 100644 index 0000000..81a2ff6 --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arq12.c @@ -0,0 +1,22 @@ +#include + +int main(int argc, char *argv[]) { + FILE *p = fopen("teste.txt", "wb+"); + int registro, valor, i; + + for (i = valor = 0; i < 10; i++, valor = i * 10) fwrite(&valor, sizeof(int), 1, p); + + int numRegistro = ftell(p) / sizeof (int); + + do { + printf ("\nEscolha um numero entre zero e %i: ", numRegistro-1); + scanf("%d", ®istro); + } while (registro < 0 || registro >= numRegistro); + + fseek(p, registro * sizeof(int), SEEK_SET); + fread(&valor, sizeof(int), 1, p); + fclose(p); + printf ("\nValor: %d\n\n", valor); + + return 0; +} diff --git a/U0 - Nivelamento/c/aquivo/arquivos.c b/U0 - Nivelamento/c/aquivo/arquivos.c new file mode 100644 index 0000000..d25865a --- /dev/null +++ b/U0 - Nivelamento/c/aquivo/arquivos.c @@ -0,0 +1,117 @@ +#include +#include +#include +//============================================================================= +void gravar_arquivo_texto(){ + + // ABRIR O ARQUIVO + FILE *arq = fopen("arquivo.txt", "w"); + + if(arq == NULL){ + printf("Erro ao tentar abrir o arquivo!\n"); + return; + } + + // ESCREVER NO ARQUIVO + fprintf(arq, "%s\t", "Algoritmos"); + fprintf(arq, "%d\t", 1); + fprintf(arq, "%f\t", 5.3); + fprintf(arq, "%c\t", 'X'); + + // FECHA O ARQUIVO + fclose(arq); +} +//============================================================================= +void ler_texto_texto(){ + + char texto[12], caracter; + int inteiro; + float real; + + // ABRIR O ARQUIVO + FILE *arq = fopen("arquivo.txt", "r"); + + if(arq == NULL){ + printf("Erro ao tentar abrir o arquivo!\n"); + return; + } + + // LE O ARQUIVO + fscanf(arq, "%99[^\t]\t", &texto); // LER STRING + fscanf(arq, "%d\t", &inteiro); + fscanf(arq, "%f\t", &real); + fscanf(arq, "%c\t", &caracter); + + // FECHA O ARQUIVO + fclose(arq); + + printf("texto: %s\n", texto); + printf("inteiro: %d\n", inteiro); + printf("real: %f\n", real); + printf("caracter: %c\n", caracter); +} +//============================================================================= +void gravar_arquivo_binario(){ + + char texto[12] = "Algoritmos", caracter = 'X'; + int inteiro = 1; + float real = 5.3; + + // ABRIR O ARQUIVO + FILE *arq = fopen("arquivo.bin", "wb"); + + if(arq == NULL){ + printf("Erro ao tentar abrir o arquivo!\n"); + return; + } + + // ESCREVER NO ARQUIVO + fwrite(&texto, sizeof(char), 12, arq); + fwrite(&inteiro, sizeof(int), 1, arq); + fwrite(&real, sizeof(float), 1, arq); + fwrite(&caracter,sizeof(char), 1, arq); + + // FECHA O ARQUIVO + fclose(arq); +} +//============================================================================= +void ler_texto_binario(){ + + char texto[12], caracter; + int inteiro; + float real; + + // ABRIR O ARQUIVO + FILE *arq = fopen("arquivo.bin", "rb"); + + if(arq == NULL){ + printf("Erro ao tentar abrir o arquivo!\n"); + return; + } + + // LE O ARQUIVO + fread(&texto, sizeof(char), 12, arq); + fread(&inteiro, sizeof(int), 1, arq); + fread(&real, sizeof(float), 1, arq); + fread(&caracter,sizeof(char), 1, arq); + + // FECHA O ARQUIVO + fclose(arq); + + printf("texto: %s\n", texto); + printf("inteiro: %d\n", inteiro); + printf("real: %f\n", real); + printf("caracter: %c\n", caracter); +} +//============================================================================= + +int main(){ + + gravar_arquivo_texto(); + ler_texto_texto(); + + gravar_arquivo_binario(); + ler_texto_binario(); + + return 1; +} \ No newline at end of file diff --git a/U0 - Nivelamento/c/argumentos_main.c b/U0 - Nivelamento/c/argumentos_main.c new file mode 100644 index 0000000..4e61c56 --- /dev/null +++ b/U0 - Nivelamento/c/argumentos_main.c @@ -0,0 +1,14 @@ +#include +#include +//============================================================================= +// EXEMPLO DE UTILIZAÇÃO DOS ARGUMENTOS PASSADOS PARA O MAIN +int main(int argc, char *argv[]){ + + int i; + + for (i = 0; i < argc; i++) + printf("%d Parametro: %s\n", i, argv[i]); + + return 0; +} +//============================================================================= \ No newline at end of file diff --git a/U0 - Nivelamento/c/leituraescrita.c b/U0 - Nivelamento/c/leituraescrita.c new file mode 100644 index 0000000..6b8cd70 --- /dev/null +++ b/U0 - Nivelamento/c/leituraescrita.c @@ -0,0 +1,22 @@ +#include +#include + +int main(int argc, char *argv[]){ + char c; + char s[100]; + int i; + double d; + + printf("\nEntre com um caractere: "); + scanf("%c", &c); + + printf("\nEntre com uma palavra: "); + scanf("%s", s); + + printf("\nEntre com um inteiro e um real: "); + scanf("%i%lf", &i, &d); + + printf("\ninteiro(%d) real(%f) char(%c) s(%s)", i, d, c, s); + + return 0; +} diff --git a/U0 - Nivelamento/c/makefile/helloworld.c b/U0 - Nivelamento/c/makefile/helloworld.c new file mode 100644 index 0000000..d069246 --- /dev/null +++ b/U0 - Nivelamento/c/makefile/helloworld.c @@ -0,0 +1,6 @@ +#include +#include + +void helloWorld(void){ + printf("Hello World!\n"); +} diff --git a/U0 - Nivelamento/c/makefile/helloworld.h b/U0 - Nivelamento/c/makefile/helloworld.h new file mode 100644 index 0000000..a4d992d --- /dev/null +++ b/U0 - Nivelamento/c/makefile/helloworld.h @@ -0,0 +1,6 @@ +#ifndef _H_TESTE +#define _H_TESTE + +void helloWorld(void); + +#endif diff --git a/U0 - Nivelamento/c/makefile/main.c b/U0 - Nivelamento/c/makefile/main.c new file mode 100644 index 0000000..96c33dc --- /dev/null +++ b/U0 - Nivelamento/c/makefile/main.c @@ -0,0 +1,8 @@ +#include +#include +#include "helloworld.h" + +int main(){ + helloWorld(); + return (0); +} diff --git a/U0 - Nivelamento/c/makefile/makefile b/U0 - Nivelamento/c/makefile/makefile new file mode 100644 index 0000000..d89dc49 --- /dev/null +++ b/U0 - Nivelamento/c/makefile/makefile @@ -0,0 +1,15 @@ +# My first makefile + +all: printy + +printy: main.o helloworld.o + gcc -o printy main.o helloworld.o + +main.o: main.c helloworld.h + gcc -o main.o main.c -c -W -Wall -ansi -pedantic + +helloworld.o: helloworld.c helloworld.h + gcc -o helloworld.o helloworld.c -c -W -Wall -ansi -pedantic + +clean: + rm -rf *.o *~ printy diff --git a/U0 - Nivelamento/c/meuprimeiroprograma.c b/U0 - Nivelamento/c/meuprimeiroprograma.c new file mode 100644 index 0000000..b362750 --- /dev/null +++ b/U0 - Nivelamento/c/meuprimeiroprograma.c @@ -0,0 +1,6 @@ +#include + +int main(int argc, char *argv[]) { + printf ("Ola pessoal!!!\n\n"); + return 0; +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro01.c b/U0 - Nivelamento/c/ponteiro/ponteiro01.c new file mode 100644 index 0000000..d6ebbb2 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro01.c @@ -0,0 +1,13 @@ +#include +#include + +int main (int argc, char** argv){ + int x = 10; + int * y = & x; + printf("\n%i", x); + printf("\n%p", &x); + printf("\n%p", y); + printf("\n%p", &y); + printf("\n%i", *y); + printf("\n"); +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro02.c b/U0 - Nivelamento/c/ponteiro/ponteiro02.c new file mode 100644 index 0000000..0271848 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro02.c @@ -0,0 +1,19 @@ +#include +#include + +int main (int argc, char** argv){ + int x1 = 11, + x2 = 22, + x3 = 33; + int *p; + + p = &x1; //p <- o endereço de x1 + x2 = *p; //x2 <- o cont. do addr apont. por p + *p = x3; //o cont. do addr apont. por p <- x3 + p = &x3; //p <- o endereço de x3 + *p = 0; //o cont. do addr apont. por p <- x3 + + printf("\ncont:%d %d %d %d", x1, x2, x3, *p); + printf("\naddr:%p %p %p %p", &x1, &x2, &x3, p); + printf("\n"); +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro03.c b/U0 - Nivelamento/c/ponteiro/ponteiro03.c new file mode 100644 index 0000000..8457201 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro03.c @@ -0,0 +1,27 @@ +#include +#include + +int main (int argc, char** argv){ + int *x1; + int x2; + int *x3; + + x1 = (int*) malloc (sizeof(int)); + printf("\nx1(%p)(%i)(%p) x2(%i)(%p) x3(%p)(%i)(%p)", x1, *x1, &x1, x2, &x2, x3, *x3, &x3); + + *x1 = 20; + printf("\nx1(%p)(%i)(%p) x2(%i)(%p) x3(%p)(%i)(%p)", x1, *x1, &x1, x2, &x2, x3, *x3, &x3); + + x2 = *x1; + printf("\nx1(%p)(%i)(%p) x2(%i)(%p) x3(%p)(%i)(%p)", x1, *x1, &x1, x2, &x2, x3, *x3, &x3); + + *x3 = x2 * *x1; + printf("\nx1(%p)(%i)(%p) x2(%i)(%p) x3(%p)(%i)(%p)", x1, *x1, &x1, x2, &x2, x3, *x3, &x3); + + x3 = &x2; + printf("\nx1(%p)(%i)(%p) x2(%i)(%p) x3(%p)(%i)(%p)", x1, *x1, &x1, x2, &x2, x3, *x3, &x3); + + x2 = 15; + printf("\nx1(%p)(%i)(%p) x2(%i)(%p) x3(%p)(%i)(%p)", x1, *x1, &x1, x2, &x2, x3, *x3, &x3); + printf("\n"); +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro04.c b/U0 - Nivelamento/c/ponteiro/ponteiro04.c new file mode 100644 index 0000000..3c1356c --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro04.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int main (int argc, char** argv){ + double M [3][3]; + double *p = M[0]; + + for (int i = 0; i < pow(3, 2); i++, p++){ + *p=0.0; + } + +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro05.c b/U0 - Nivelamento/c/ponteiro/ponteiro05.c new file mode 100644 index 0000000..1d33714 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro05.c @@ -0,0 +1,26 @@ +#include +#include + +#define MAXTAM 10 + +typedef struct Cliente { + int codigo; + char nome[MAXTAM]; +} Cliente; + + +int main (int argc, char** argv){ + char* p1 = (char*) malloc (sizeof(char)); + int* p2 = (int*) malloc (sizeof(int)); + float* p3 = (float*) malloc (sizeof(float)); + Cliente* p4 = (Cliente*) malloc (sizeof(Cliente)); + int* p5 = (int*) malloc (MAXTAM * sizeof (int)); + Cliente* p6 =(Cliente*) malloc (MAXTAM * sizeof (Cliente)); + + free(p1); + free(p2); + free(p3); + free(p4); + free(p5); + free(p6); +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro05emCMaisMais.cc b/U0 - Nivelamento/c/ponteiro/ponteiro05emCMaisMais.cc new file mode 100644 index 0000000..7a378e2 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro05emCMaisMais.cc @@ -0,0 +1,26 @@ +#include +using namespace std; + +#define MAXTAM 10 + +typedef struct Cliente { + int codigo; + char nome[MAXTAM]; +} Cliente; + + +int main (int argc, char** argv){ + char* p1 = new char; + int* p2 = new int; + float* p3 = new float; + Cliente* p4 = new Cliente; + int* p5 = new int [MAXTAM]; + Cliente* p6 = new Cliente[MAXTAM]; + + delete p1; + delete p2; + delete p3; + delete p4; + delete [] p5; + delete [] p6; +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro06.c b/U0 - Nivelamento/c/ponteiro/ponteiro06.c new file mode 100644 index 0000000..2c7719e --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro06.c @@ -0,0 +1,24 @@ +#include +#include +#include + +#define MAXTAM 100 + +typedef struct Cliente { + int codigo; + char nome[MAXTAM]; +} Cliente; + +int main (int argc, char** argv){ + Cliente registro; + Cliente* ponteiro = (Cliente*) malloc (sizeof(Cliente)); + + registro.codigo = 1; + strcpy(registro.nome, "AA"); + printf("\nFuncionario (%i): %s", registro.codigo, registro.nome); + + ponteiro->codigo = 2; + strcpy(ponteiro->nome, "BB"); + printf("\nFuncionario (%i): %s", ponteiro->codigo, ponteiro->nome); + printf("\n"); +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro07.c b/U0 - Nivelamento/c/ponteiro/ponteiro07.c new file mode 100644 index 0000000..7c91723 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro07.c @@ -0,0 +1,30 @@ +#include +#include + +int main (int argc, char** argv){ + double a; + double *p, *q; + + a = 3.14; + printf("%f\n", a); + + p = &a; + *p = 2.718; + printf("%f\n", a); + + a = 5; + printf("%f\n", *p); + + p = NULL; + p = (double*) malloc(sizeof(double)); + *p = 20; + q = p; + + printf("%f\n", *p); + printf("%f\n", a); + free(p); + printf("%f\n", *q); + printf("\n"); + + return 0; +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro08.c b/U0 - Nivelamento/c/ponteiro/ponteiro08.c new file mode 100644 index 0000000..3a1b2f2 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro08.c @@ -0,0 +1,19 @@ +#include +#include + +int main (int argc, char** argv){ + int a[10], *b; + + b = a; + b[5] = 100; + printf("\n%d -- %d", a[5], b[5]); + + b = (int*) malloc(10*sizeof(int)); + b[7] = 100; + printf("\n%d -- %d", a[7], b[7]); + printf("\n"); + + //O comando a = b gera um erro de compilação + + return 0; +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro09.c b/U0 - Nivelamento/c/ponteiro/ponteiro09.c new file mode 100644 index 0000000..ed4f931 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro09.c @@ -0,0 +1,40 @@ +#include +#include + +int main (int argc, char** argv){ + int *x1; + int x2; + int *x3; + + x1 = (int*) malloc(sizeof(int)); + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + *x1 = 20; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + x2 = *x1; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + *x3 = x2 * *x1; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + x3 = &x2; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + x2 = 15; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + x2 = 13 & 3; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + x2 = 13 | 3; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + x2 = 13 >> 1; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + x2 = 13 << 1; + printf("\nx1(%p)(%p)(%i) x2(%p)(%i) x3(%p)(%p)(%i)", &x1, x1, *x1, &x2, x2, &x3, x3, *x3); + + return 0; +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro10.c b/U0 - Nivelamento/c/ponteiro/ponteiro10.c new file mode 100644 index 0000000..b342c0f --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro10.c @@ -0,0 +1,21 @@ +#include +#include + +#define MAXTAM 10 + +typedef struct Cliente { + int codigo; + char nome[MAXTAM]; +} Cliente; + +int main (int argc, char** argv){ + Cliente c; + c.codigo = 5; + Cliente *p = NULL; + p = (Cliente*) malloc (sizeof(Cliente)); + p->codigo = 6; + Cliente *p2 = &c; + p2->codigo = 7; + + return 0; +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro11.c b/U0 - Nivelamento/c/ponteiro/ponteiro11.c new file mode 100644 index 0000000..963e646 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro11.c @@ -0,0 +1,20 @@ +#include +#include + +typedef struct Celula { + int elemento; + struct Celula *prox; +} Celula; + +Celula *novaCelula(int elemento) { + Celula *nova = (Celula*) malloc(sizeof(Celula)); + nova->elemento = elemento; + nova->prox = NULL; + return nova; +} + +int main (int argc, char** argv){ + Celula *tmp = novaCelula(3); + + return 0; +} diff --git a/U0 - Nivelamento/c/ponteiro/ponteiro12.c b/U0 - Nivelamento/c/ponteiro/ponteiro12.c new file mode 100644 index 0000000..b0318c3 --- /dev/null +++ b/U0 - Nivelamento/c/ponteiro/ponteiro12.c @@ -0,0 +1,20 @@ +#include +#include + +typedef struct Elemento { + int valor; +} Elemento; + +int main (int argc, char** argv){ + Elemento e1; + Elemento* e2 = (Elemento*) malloc (3 * sizeof (Elemento)); + Elemento e3[3]; + Elemento** e4 = (Elemento**) malloc(3 * sizeof(Elemento*)); + e4[0] = (Elemento*) malloc(sizeof(Elemento*)); + e4[2] = (Elemento*) malloc(sizeof(Elemento*)); + + e4[0]->valor = 7; + + printf("\n%i -- %i\n", e4[0]->valor, e4[2]->valor); + return 0; +} diff --git a/U0 - Nivelamento/c/recursividade/chamandometodo.c b/U0 - Nivelamento/c/recursividade/chamandometodo.c new file mode 100644 index 0000000..0e18ac4 --- /dev/null +++ b/U0 - Nivelamento/c/recursividade/chamandometodo.c @@ -0,0 +1,26 @@ +#include +#include + +void terceiro(){ + printf("3o - inicio e fim\n"); +} + +void segundo(){ + printf("2o - inicio\n"); + terceiro(); + printf("2o - fim\n"); +} + +void primeiro(){ + printf("1o - inicio\n"); + segundo(); + printf("1o - fim\n"); +} + +int main(int argc, char *argv[]) { + printf("main - inicio\n"); + primeiro(); + printf("main - fim\n"); + + return 0; +} diff --git a/U0 - Nivelamento/c/recursividade/contarmaiuscula.c b/U0 - Nivelamento/c/recursividade/contarmaiuscula.c new file mode 100644 index 0000000..b522aaa --- /dev/null +++ b/U0 - Nivelamento/c/recursividade/contarmaiuscula.c @@ -0,0 +1,45 @@ +#include +#include +#include + +#define bool int +#define true 1 +#define false 0 + +bool isUpper(char x){ + return (x >= 'A' && x <= 'Z'); +} + +int contMaiusculo(char* s){ + int cont = 0; + + for(int i = 0; i < strlen(s); i++){ + if(isUpper(s[i]) == true){ + cont++; + } + } + return cont; +} + +int contMaiusculoRec(char* s, int i){ + int cont = 0; + + if(i < strlen(s)){ + if(isUpper(s[i]) == true){ + cont++; + } + cont += contMaiusculoRec(s, i + 1); + } + return cont; +} + +int contMaiusculoRecursivo(char* s){ + return contMaiusculoRec(s, 0); +} + +int main(int argc, char *argv[]) { + printf("\nAlGoritmos e Estrutudas de Dados: %i", contMaiusculo("AlGoritmos e Estruturas de Dados")); + printf("\nAlGoritmos e Estruturas de Dados: %i", contMaiusculoRecursivo("AlGoritmos e Estruturas de Dados")); + + return 0; +} diff --git a/U0 - Nivelamento/c/recursividade/fatorialrecursivo.c b/U0 - Nivelamento/c/recursividade/fatorialrecursivo.c new file mode 100644 index 0000000..d41e3eb --- /dev/null +++ b/U0 - Nivelamento/c/recursividade/fatorialrecursivo.c @@ -0,0 +1,18 @@ +#include +#include + +int fatorial(int n){ + int resp; + printf("\nfat (%i)", n); + resp = (n == 1) ? 1 : n * fatorial(n-1); + printf("\nfat n(%i): %i", n, resp); + return resp; +} + + +int main(int argc, char *argv[]) { + int n = 5; + printf("\nFATORIAL RECURSIVO(%i): %i", n, fatorial(n)); + + return 0; +} diff --git a/U0 - Nivelamento/c/recursividade/fibonaccirecursivo.c b/U0 - Nivelamento/c/recursividade/fibonaccirecursivo.c new file mode 100644 index 0000000..b749871 --- /dev/null +++ b/U0 - Nivelamento/c/recursividade/fibonaccirecursivo.c @@ -0,0 +1,18 @@ +#include +#include + +int fibonacci(int n){ + int resp; + printf("\nfib (%i)", n); + resp = (n == 0 || n == 1) ? 1 : fibonacci(n-1) + fibonacci(n-2); + printf("\nfib n(%i): %i", n, resp); + return resp; +} + + +int main(int argc, char *argv[]) { + int n = 5; + printf("\nFIBONACCI RECURSIVO(%i): %i", n, fibonacci(n)); + + return 0; +} diff --git a/U0 - Nivelamento/c/recursividade/multiplicacao.c b/U0 - Nivelamento/c/recursividade/multiplicacao.c new file mode 100644 index 0000000..81e9b07 --- /dev/null +++ b/U0 - Nivelamento/c/recursividade/multiplicacao.c @@ -0,0 +1,34 @@ +#include +#include + +int multiplicacaoRec(int a, int b, int i){ + int resp = 0; + + if(i < b){ + resp = a + multiplicacaoRec(a, b, i+1); + } + + return resp; +} + +int multiplicacao(int a, int b){ + return multiplicacaoRec(a, b, 0); +} + +int multiplicacaoIt(int a, int b){ + int resp = 0; + for(int i = 0; i < b; i++){ + resp = a + resp; + } + return resp; +} + +int main(int argc, char *argv[]) { + int mult = multiplicacaoIt(4, 3); + printf("\nRESPOSTA IT: %i", mult); + + mult = multiplicacao(4, 3); + printf("\nRESPOSTA REC: %i", mult); + + return 0; +} diff --git a/U0 - Nivelamento/c/recursividade/printrecursivo.c b/U0 - Nivelamento/c/recursividade/printrecursivo.c new file mode 100644 index 0000000..e4b67a3 --- /dev/null +++ b/U0 - Nivelamento/c/recursividade/printrecursivo.c @@ -0,0 +1,17 @@ +#include +#include + +void printRecursivo(int i){ + printf("\nvalor de i: %i", i); + if(i > 0){ + printRecursivo(i-1); + } + printf("\nvalor de i: %i", i); +} + +int main(int argc, char *argv[]) { + int n = 3; + printRecursivo(n); + + return 0; +} diff --git a/U0 - Nivelamento/c/registro/registro01.c b/U0 - Nivelamento/c/registro/registro01.c new file mode 100644 index 0000000..6178a8d --- /dev/null +++ b/U0 - Nivelamento/c/registro/registro01.c @@ -0,0 +1,24 @@ + + +#include + +#define MAXTAM 100 + +struct Funcionario { + int matricula; + char nome[MAXTAM]; +}; + +void main (){ + struct Funcionario f; + + printf("\nEntre com a matricula: "); + scanf("%d", &f.matricula); + + printf("\nEntre com o nome: "); + scanf("%s", f.nome); + + printf("\nMatricula: %d", f.matricula); + printf("\nNome: %s", f.nome); + printf("\n\n"); +} diff --git a/U0 - Nivelamento/c/registro/registro02.c b/U0 - Nivelamento/c/registro/registro02.c new file mode 100644 index 0000000..1dc7faa --- /dev/null +++ b/U0 - Nivelamento/c/registro/registro02.c @@ -0,0 +1,24 @@ +#include + +#define MAXTAM 100 + +struct Funcionario { + int matricula; + char nome[MAXTAM]; +}; + +typedef struct Funcionario Funcionario; + +void main (){ + Funcionario f; + + printf("\nEntre com a matricula: "); + scanf("%d", &f.matricula); + + printf("\nEntre com o nome: "); + scanf("%s", f.nome); + + printf("\nMatricula: %d", f.matricula); + printf("\nNome: %s", f.nome); + printf("\n\n"); +} diff --git a/U0 - Nivelamento/c/registro/registro03.c b/U0 - Nivelamento/c/registro/registro03.c new file mode 100644 index 0000000..50dffe5 --- /dev/null +++ b/U0 - Nivelamento/c/registro/registro03.c @@ -0,0 +1,22 @@ +#include + +#define MAXTAM 100 + +typedef struct Funcionario { + int matricula; + char nome[MAXTAM]; +} Funcionario; + +void main (){ + Funcionario f; + + printf("\nEntre com a matricula: "); + scanf("%d", &f.matricula); + + printf("\nEntre com o nome: "); + scanf("%s", f.nome); + + printf("\nMatricula: %d", f.matricula); + printf("\nNome: %s", f.nome); + printf("\n\n"); +} diff --git a/U0 - Nivelamento/c/registro/registro04.c b/U0 - Nivelamento/c/registro/registro04.c new file mode 100644 index 0000000..3c387ec --- /dev/null +++ b/U0 - Nivelamento/c/registro/registro04.c @@ -0,0 +1,29 @@ +#include + +#define MAXTAM 100 + +struct Funcionario { + int matricula; + char nome[MAXTAM]; +}; + +typedef struct Funcionario Funcionario; + +void main (){ + Funcionario vet[MAXTAM]; + int n = 3; + + for(int i = 0; i < n; i++){ + printf("\nEntre com a matricula do funcionario %d: ", (i+1)); + scanf("%d", &vet[i].matricula); + + printf("\nEntre com o nome do funcionario %d: ", (i+1)); + scanf("%s", vet[i].nome); + } + + for(int i = 0; i < n; i++){ + printf("\nMatricula do funcionario %d: %d", (i+1), vet[i].matricula); + printf("\nNome do funcionario %d: %s\n", (i+1), vet[i].nome); + } + printf("\n\n"); +} diff --git a/U0 - Nivelamento/c/sizeof.c b/U0 - Nivelamento/c/sizeof.c new file mode 100644 index 0000000..cb24007 --- /dev/null +++ b/U0 - Nivelamento/c/sizeof.c @@ -0,0 +1,14 @@ +#include + +int main(int argc, char *argv[]) { + int a = sizeof(char), + b = sizeof(int), + c = sizeof(double), + d = sizeof(float); + + printf ("Tamanho do char: %i\n", a); + printf ("Tamanho do int: %i\n", b); + printf ("Tamanho do double: %i\n", c); + printf ("Tamanho do float: %i\n", d); + return 0; +} diff --git a/U0 - Nivelamento/c/string.c b/U0 - Nivelamento/c/string.c new file mode 100644 index 0000000..164b473 --- /dev/null +++ b/U0 - Nivelamento/c/string.c @@ -0,0 +1,33 @@ +#include +#include + +int main(int argc, char *argv[]){ + char s1[80], s2[80]; + + strcpy(s1, "Algoritmos"); + strcpy(s2, " e Estruturas de Dados II"); + + printf("\nTamanho s1(%i)", (int)strlen(s1)); + printf("\nTamanho s2(%i)", (int)strlen(s2)); + + if(!strcmp(s1, s2)) printf("\nIguais!!!"); + else printf("\nDiferentes!!!"); + + strcat(s1, s2); + printf("\nNova s1 (%s)", s1); + + strcpy(s2, s1); + printf("\nNova s2 (%s)", s2); + + if(!strcmp(s1, s2)) printf("\nIguais!!!"); + else printf("\nDiferentes!!!"); + + s1[10] = s2[10] = '\0'; s1[11] = 'a'; s2[11] = 'b'; + printf("\nNova s1 (%s)", s1); + printf("\nNova s2 (%s)", s2); + + if(!strcmp(s1, s2)) printf("\nIguais!!!"); + else printf("\nDiferentes!!!"); + + return 0; +} diff --git a/U4 - Ordenação em memória principal/c/bolha_teste.c b/U4 - Ordenação em memória principal/c/bolha_teste.c index f01940c..212fb02 100644 --- a/U4 - Ordenação em memória principal/c/bolha_teste.c +++ b/U4 - Ordenação em memória principal/c/bolha_teste.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) { crescente(array, n); print_array(array, n); comeco = clock(); - bolha(array, n); + bubbleSort(array, n); fim = clock(); total = (clock() - comeco) / (double)CLOCKS_PER_SEC / 1000.0; print_array(array, n); @@ -36,7 +36,7 @@ int main(int argc, char **argv) { decrescente(array, n); print_array(array, n); comeco = clock(); - bolha(array, n); + bubbleSort(array, n); fim = clock(); total = (clock() - comeco) / (double)CLOCKS_PER_SEC / 1000.0; print_array(array, n); @@ -46,7 +46,7 @@ int main(int argc, char **argv) { aleatorio(array, n); print_array(array, n); comeco = clock(); - bolha(array, n); + bubbleSort(array, n); fim = clock(); total = (clock() - comeco) / (double)CLOCKS_PER_SEC / 1000.0; print_array(array, n); diff --git a/U4 - Ordenação em memória principal/c/bolha.h b/U4 - Ordenação em memória principal/c/bubblesort.h similarity index 80% rename from U4 - Ordenação em memória principal/c/bolha.h rename to U4 - Ordenação em memória principal/c/bubblesort.h index c75c374..36ac7fb 100644 --- a/U4 - Ordenação em memória principal/c/bolha.h +++ b/U4 - Ordenação em memória principal/c/bubblesort.h @@ -1,9 +1,9 @@ - #ifndef BOLHA_H - #define BOLHA_H + #ifndef BUBBLESORT_H + #define BUBBLESORT_H //============================================================================= -#include "ordenacao.h" +#include "sort.h" //============================================================================= -void bolha(int *array, int n){ +void bubbleSort(int *array, int n){ int i, j; for (i = (n - 1); i > 0; i--) { for (j = 0; j < i; j++) { diff --git a/U4 - Ordenação em memória principal/c/countingsort.h b/U4 - Ordenação em memória principal/c/countingsort.h new file mode 100644 index 0000000..e69de29 diff --git a/U4 - Ordenação em memória principal/c/heapsort.h b/U4 - Ordenação em memória principal/c/heapsort.h new file mode 100644 index 0000000..e69de29 diff --git a/U4 - Ordenação em memória principal/c/insertionsort.h b/U4 - Ordenação em memória principal/c/insertionsort.h new file mode 100644 index 0000000..35496f3 --- /dev/null +++ b/U4 - Ordenação em memória principal/c/insertionsort.h @@ -0,0 +1,20 @@ + #ifndef INSERTSORT_H + #define INSERTSORT_H + //============================================================================= +#include "sort.h" +//============================================================================= +void insertSort(int *array, int n){ + int i, j, tmp; + for (int i = 1; i < n; i++) { + tmp = array[i]; + int j = i - 1; + + while ((j >= 0) && (array[j] > tmp)) { + array[j + 1] = array[j]; + j--; + } + array[j+1] = tmp; + } +} +//============================================================================= +#endif \ No newline at end of file diff --git a/U4 - Ordenação em memória principal/c/mergesort.h b/U4 - Ordenação em memória principal/c/mergesort.h new file mode 100644 index 0000000..e69de29 diff --git a/U4 - Ordenação em memória principal/c/quicksort.h b/U4 - Ordenação em memória principal/c/quicksort.h new file mode 100644 index 0000000..e69de29 diff --git a/U4 - Ordenação em memória principal/c/selectionsort.h b/U4 - Ordenação em memória principal/c/selectionsort.h new file mode 100644 index 0000000..7f1a393 --- /dev/null +++ b/U4 - Ordenação em memória principal/c/selectionsort.h @@ -0,0 +1,19 @@ + #ifndef SELECTSORT_H + #define SELECTSORT_H + //============================================================================= +#include "sort.h" +//============================================================================= +void selectSort(int *array, int n){ + int i, j, indice; + for (i = 0; i < (n - 1); i++) { + indice = i; + for (j = (i + 1); j < n; j++){ + if (array[indice] > array[j]){ + indice = j; + } + } + swap(&array[indice], &array[i]); + } +} +//============================================================================= +#endif \ No newline at end of file diff --git a/U4 - Ordenação em memória principal/c/shellsort.h b/U4 - Ordenação em memória principal/c/shellsort.h new file mode 100644 index 0000000..e69de29 diff --git a/U4 - Ordenação em memória principal/c/ordenacao.h b/U4 - Ordenação em memória principal/c/sort.h similarity index 94% rename from U4 - Ordenação em memória principal/c/ordenacao.h rename to U4 - Ordenação em memória principal/c/sort.h index 2cbbc13..2add4f2 100644 --- a/U4 - Ordenação em memória principal/c/ordenacao.h +++ b/U4 - Ordenação em memória principal/c/sort.h @@ -1,5 +1,5 @@ - #ifndef ORDENACAO_H - #define ORDENACAO_H + #ifndef SORT_H + #define SORT_H //============================================================================= #include #include @@ -40,7 +40,7 @@ void aleatorio(int *array, int n) { } } //============================================================================= -// PROCEDIMENTO PARA EXIBIR OS DADOS PRESENTES NO arranjo +// PROCEDIMENTO PARA EXIBIR OS DADOS PRESENTES NO ARRANJO void print_array(int *array, int n) { int i; printf("[ ");