Merge branch 'master' of https://github.com/icei-pucminas/AEDII
This commit is contained in:
commit
27fcd45e19
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
FILE *p = fopen ("teste.txt", "w");
|
||||
fputc('M', p);
|
||||
fclose(p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
FILE *p = fopen ("teste.txt", "w");
|
||||
fputs("Algoritmos e Estruturas de Dados II", p);
|
||||
fclose(p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
//=============================================================================
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
//=============================================================================
|
||||
// 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;
|
||||
}
|
||||
//=============================================================================
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void helloWorld(void){
|
||||
printf("Hello World!\n");
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef _H_TESTE
|
||||
#define _H_TESTE
|
||||
|
||||
void helloWorld(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "helloworld.h"
|
||||
|
||||
int main(){
|
||||
helloWorld();
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
printf ("Ola pessoal!!!\n\n");
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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");
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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");
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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");
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include <iostream>
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef CELULA_H
|
||||
#define CELULA_H
|
||||
//=============================================================================
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
//=============================================================================
|
||||
typedef struct Celula{
|
||||
int elemento;
|
||||
struct Celula *prox;
|
||||
}Celula;
|
||||
//=============================================================================
|
||||
Celula* new_celula(int elemento){
|
||||
Celula *temp = (Celula*)malloc(sizeof(Celula));
|
||||
temp->elemento = elemento;
|
||||
temp->prox = NULL;
|
||||
return temp;
|
||||
}
|
||||
//=============================================================================
|
||||
typedef struct CelulaDupla{
|
||||
int elemento;
|
||||
struct CelulaDupla *prox, *ant;
|
||||
}CelulaDupla;
|
||||
//=============================================================================
|
||||
CelulaDupla* new_celula_dupla(int elemento){
|
||||
CelulaDupla *temp = (CelulaDupla*)malloc(sizeof(CelulaDupla));
|
||||
temp->elemento = elemento;
|
||||
temp->ant = NULL;
|
||||
temp->prox = NULL;
|
||||
return temp;
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
#ifndef FILA_H
|
||||
#define FILA_H
|
||||
//=============================================================================
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include "celula.h"
|
||||
//=============================================================================
|
||||
typedef struct Fila{
|
||||
struct Celula *primeiro, *ultimo;
|
||||
int size;
|
||||
} Fila;
|
||||
//=============================================================================
|
||||
Fila new_fila(){
|
||||
Fila temp;
|
||||
temp.primeiro = temp.ultimo = new_celula(-1);
|
||||
temp.size = 0;
|
||||
return temp;
|
||||
}
|
||||
//=============================================================================
|
||||
int size_fila(Fila *f){
|
||||
return f->size;
|
||||
}
|
||||
//=============================================================================
|
||||
void enqueue_fila(Fila *f, int elemento){
|
||||
f->ultimo->prox = new_celula(elemento);
|
||||
f->ultimo = f->ultimo->prox;
|
||||
f->size++;
|
||||
}
|
||||
//=============================================================================
|
||||
int dequeue_fila(Fila *f){
|
||||
|
||||
if (f->primeiro == f->ultimo){
|
||||
printf("\nA fila esta vazia!\n");
|
||||
return INT_MIN;
|
||||
}
|
||||
|
||||
Celula *temp = f->primeiro;
|
||||
f->primeiro = f->primeiro->prox;
|
||||
f->size--;
|
||||
free(temp);
|
||||
return f->primeiro->elemento;
|
||||
}
|
||||
//=============================================================================
|
||||
void print_fila(Fila *f){
|
||||
Celula *i;
|
||||
printf("[");
|
||||
for (i = f->primeiro->prox; i != NULL; i = i->prox)
|
||||
{
|
||||
printf("%d, ", i->elemento);
|
||||
}
|
||||
printf("] \n");
|
||||
}
|
||||
//=============================================================================
|
||||
void delete_fila(Fila *f){
|
||||
while(f->size > 0)
|
||||
dequeue_fila(f);
|
||||
free(f->primeiro);
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#include "fila.h"
|
||||
//=============================================================================
|
||||
int main() {
|
||||
|
||||
Fila f;
|
||||
int i, x1, x2, x3;
|
||||
printf("==== FILA FLEXIVEL ====\n");
|
||||
|
||||
f = new_fila();
|
||||
|
||||
for(i=0; i < 10; i++)
|
||||
enqueue_fila(&f, i);
|
||||
|
||||
printf("Apos inserrir os dados: \n");
|
||||
print_fila(&f);
|
||||
|
||||
x1 = dequeue_fila(&f);
|
||||
x2 = dequeue_fila(&f);
|
||||
x3 = dequeue_fila(&f);
|
||||
|
||||
printf("Apos as remocoes (%d, %d, %d) \n", x1, x2, x3);
|
||||
print_fila(&f);
|
||||
|
||||
delete_fila(&f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
#ifndef LISTA_H
|
||||
#define LISTA_H
|
||||
//=============================================================================
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include "celula.h"
|
||||
//=============================================================================
|
||||
typedef struct Lista{
|
||||
struct Celula *primeiro, *ultimo;
|
||||
int size;
|
||||
} Lista;
|
||||
//=============================================================================
|
||||
Lista new_lista(){
|
||||
Lista temp;
|
||||
temp.primeiro = temp.ultimo = new_celula(-1);
|
||||
temp.size = 0;
|
||||
return temp;
|
||||
}
|
||||
//=============================================================================
|
||||
int size_lista(Lista *l){
|
||||
return l->size;
|
||||
}
|
||||
//=============================================================================
|
||||
void insert_begin(Lista *l, int elemento){
|
||||
|
||||
Celula *temp = new_celula(-1);
|
||||
temp->prox = l->primeiro;
|
||||
|
||||
l->primeiro->elemento = elemento;
|
||||
l->primeiro = temp;
|
||||
l->size++;
|
||||
}
|
||||
//=============================================================================
|
||||
void insert_end(Lista *l, int elemento){
|
||||
l->ultimo->prox = new_celula(elemento);
|
||||
l->ultimo = l->ultimo->prox;
|
||||
l->size++;
|
||||
}
|
||||
//=============================================================================
|
||||
void insert_at(Lista *l, int elemento, int pos){
|
||||
|
||||
if(pos < 0 || pos > l->size)
|
||||
printf("Erro ao tentar inserir na posicao (%d/ tamanho = %d) invalida!", pos, l->size);
|
||||
else if (pos == 0)
|
||||
insert_begin(l, elemento);
|
||||
else if (pos == l->size)
|
||||
insert_end(l, elemento);
|
||||
else{
|
||||
|
||||
Celula *ant = l->primeiro;
|
||||
for(int i=0; i<pos;i++)
|
||||
ant = ant->prox;
|
||||
|
||||
Celula *temp = new_celula(elemento);
|
||||
temp->prox = ant->prox;
|
||||
ant->prox = temp;
|
||||
l->size++;
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
int remove_at(Lista *l, int pos){
|
||||
|
||||
if(l->primeiro == l->ultimo){
|
||||
printf("\nA lista esta vazia!\n");
|
||||
return INT_MIN;
|
||||
}else if(pos < 0 || pos > l->size-1)
|
||||
printf("Erro ao tentar remover item da posicao (%d/ tamanho = %d) invalida!", pos, l->size);
|
||||
else{
|
||||
|
||||
Celula *ant = l->primeiro;
|
||||
for(int i=0; i<pos;i++)
|
||||
ant = ant->prox;
|
||||
|
||||
Celula *temp = ant->prox;
|
||||
int elemento = temp->elemento;
|
||||
|
||||
ant->prox = temp->prox;
|
||||
free(temp);
|
||||
|
||||
if(pos == l->size-1)
|
||||
l->ultimo = ant;
|
||||
|
||||
l->size--;
|
||||
|
||||
return elemento;
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
int remove_begin(Lista *l){
|
||||
return remove_at(l, 0);
|
||||
}
|
||||
//=============================================================================
|
||||
int remove_end(Lista *l){
|
||||
return remove_at(l, l->size-1);
|
||||
}
|
||||
//=============================================================================
|
||||
bool pesquisar_lista(Lista *l, int elemento){
|
||||
Celula *i;
|
||||
for (i = l->primeiro->prox; i != NULL; i = i->prox)
|
||||
if(i->elemento == elemento)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
//=============================================================================
|
||||
void print_lista(Lista *l){
|
||||
Celula *i;
|
||||
printf("[");
|
||||
for (i = l->primeiro->prox; i != NULL; i = i->prox)
|
||||
{
|
||||
printf("%d, ", i->elemento);
|
||||
}
|
||||
printf("] \n");
|
||||
}
|
||||
//=============================================================================
|
||||
void delete_lista(Lista *l){
|
||||
while(l->size > 0)
|
||||
remove_at(l,0);
|
||||
free(l->primeiro);
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
|
|
@ -0,0 +1,152 @@
|
|||
#ifndef LISTADUPLA_H
|
||||
#define LISTADUPLA_H
|
||||
//=============================================================================
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include "celula.h"
|
||||
//=============================================================================
|
||||
typedef struct ListaDupla{
|
||||
struct CelulaDupla *primeiro, *ultimo;
|
||||
int size;
|
||||
} ListaDupla;
|
||||
//=============================================================================
|
||||
ListaDupla new_lista_dupla(){
|
||||
ListaDupla temp;
|
||||
temp.primeiro = temp.ultimo = new_celula_dupla(-1);
|
||||
temp.size = 0;
|
||||
return temp;
|
||||
}
|
||||
//=============================================================================
|
||||
int size_lista_dupla(ListaDupla *l){
|
||||
return l->size;
|
||||
}
|
||||
//=============================================================================
|
||||
void insert_begin_dupla(ListaDupla *l, int elemento){
|
||||
|
||||
CelulaDupla *temp = new_celula_dupla(-1);
|
||||
temp->prox = l->primeiro;
|
||||
|
||||
l->primeiro->elemento = elemento;
|
||||
l->primeiro->ant = temp;
|
||||
l->primeiro = temp;
|
||||
l->size++;
|
||||
}
|
||||
//=============================================================================
|
||||
void insert_end_dupla(ListaDupla *l, int elemento){
|
||||
l->ultimo->prox = new_celula_dupla(elemento);
|
||||
l->ultimo->prox->ant = l->ultimo;
|
||||
l->ultimo = l->ultimo->prox;
|
||||
l->size++;
|
||||
}
|
||||
//=============================================================================
|
||||
void insert_at_dupla(ListaDupla *l, int elemento, int pos){
|
||||
|
||||
if(pos < 0 || pos > l->size)
|
||||
printf("Erro ao tentar inserir na posicao (%d/ tamanho = %d) invalida!", pos, l->size);
|
||||
else if (pos == 0)
|
||||
insert_begin_dupla(l, elemento);
|
||||
else if (pos == l->size)
|
||||
insert_end_dupla(l, elemento);
|
||||
else{
|
||||
|
||||
CelulaDupla *ant = l->primeiro;
|
||||
for(int i=0; i<pos;i++)
|
||||
ant = ant->prox;
|
||||
|
||||
CelulaDupla *temp = new_celula_dupla(elemento);
|
||||
temp->prox = ant->prox;
|
||||
temp->prox->ant = temp;
|
||||
temp->ant = ant;
|
||||
ant->prox = temp;
|
||||
l->size++;
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
int remove_end_dupla(ListaDupla *l){
|
||||
|
||||
if(l->primeiro == l->ultimo){
|
||||
printf("\nA lista esta vazia!\n");
|
||||
return INT_MIN;
|
||||
}
|
||||
|
||||
CelulaDupla *temp = l->ultimo;
|
||||
int elemento = temp->elemento;
|
||||
|
||||
l->ultimo = l->ultimo->ant;
|
||||
l->ultimo->prox = NULL;
|
||||
l->size--;
|
||||
|
||||
free(temp);
|
||||
|
||||
return elemento;
|
||||
}
|
||||
//=============================================================================
|
||||
int remove_at_dupla(ListaDupla *l, int pos){
|
||||
|
||||
if(l->primeiro == l->ultimo){
|
||||
printf("\nA lista esta vazia!\n");
|
||||
return INT_MIN;
|
||||
}else if(pos < 0 || pos > l->size-1)
|
||||
printf("Erro ao tentar remover item da posicao (%d/ tamanho = %d) invalida!", pos, l->size);
|
||||
else if(pos == l->size-1)
|
||||
remove_end_dupla(l);
|
||||
else{
|
||||
|
||||
CelulaDupla *ant = l->primeiro;
|
||||
for(int i=0; i<pos;i++)
|
||||
ant = ant->prox;
|
||||
|
||||
CelulaDupla *temp = ant->prox;
|
||||
int elemento = temp->elemento;
|
||||
|
||||
temp->prox->ant = ant;
|
||||
ant->prox = temp->prox;
|
||||
free(temp);
|
||||
|
||||
l->size--;
|
||||
|
||||
return elemento;
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
int remove_begin_dupla(ListaDupla *l){
|
||||
return remove_at_dupla(l, 0);
|
||||
}
|
||||
//=============================================================================
|
||||
bool pesquisar_lista_dupla(ListaDupla *l, int elemento){
|
||||
CelulaDupla *i;
|
||||
for (i = l->primeiro->prox; i != NULL; i = i->prox)
|
||||
if(i->elemento == elemento)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
//=============================================================================
|
||||
void print_lista_dupla(ListaDupla *l){
|
||||
CelulaDupla *i;
|
||||
printf("[");
|
||||
for (i = l->primeiro->prox; i != NULL; i = i->prox)
|
||||
{
|
||||
printf("%d, ", i->elemento);
|
||||
}
|
||||
printf("] \n");
|
||||
}
|
||||
//=============================================================================
|
||||
void print_lista_dupla_inverso(ListaDupla *l){
|
||||
CelulaDupla *i;
|
||||
printf("[");
|
||||
for (i = l->ultimo; i != l->primeiro; i = i->ant)
|
||||
{
|
||||
printf("%d, ", i->elemento);
|
||||
}
|
||||
printf("] \n");
|
||||
}
|
||||
//=============================================================================
|
||||
void delete_lista_dupla(ListaDupla *l){
|
||||
while(l->size > 0)
|
||||
remove_at_dupla(l,0);
|
||||
free(l->primeiro);
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
#include "lista_dupla.h"
|
||||
//=============================================================================
|
||||
int main() {
|
||||
|
||||
ListaDupla l;
|
||||
int i, x1, x2, x3;
|
||||
printf("==== LISTA FLEXIVEL ====\n");
|
||||
|
||||
l = new_lista_dupla();
|
||||
|
||||
for(i=0; i < 5; i++)
|
||||
insert_end_dupla(&l, i);
|
||||
|
||||
printf("Apos inserir os dados: \n");
|
||||
print_lista_dupla(&l);
|
||||
print_lista_dupla_inverso(&l);
|
||||
|
||||
printf("Apos inserir no inicio: \n");
|
||||
insert_begin_dupla(&l, i++);
|
||||
print_lista_dupla(&l);
|
||||
print_lista_dupla_inverso(&l);
|
||||
|
||||
printf("Apos inserir no final: \n");
|
||||
insert_end_dupla(&l, i++);
|
||||
print_lista_dupla(&l);
|
||||
print_lista_dupla_inverso(&l);
|
||||
|
||||
printf("Apos inserir na posicao 4: \n");
|
||||
insert_at_dupla(&l, i++, 4);
|
||||
print_lista_dupla(&l);
|
||||
print_lista_dupla_inverso(&l);
|
||||
|
||||
printf("Apos remover no inicio: \n");
|
||||
x1 = remove_begin_dupla(&l);
|
||||
print_lista_dupla(&l);
|
||||
print_lista_dupla_inverso(&l);
|
||||
|
||||
printf("Apos remover no final: \n");
|
||||
x1 = remove_end_dupla(&l);
|
||||
print_lista_dupla(&l);
|
||||
print_lista_dupla_inverso(&l);
|
||||
|
||||
printf("Apos remover na posicao 2: \n");
|
||||
x1 = remove_at_dupla(&l, 2);
|
||||
print_lista_dupla(&l);
|
||||
print_lista_dupla_inverso(&l);
|
||||
|
||||
delete_lista_dupla(&l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
#include "lista.h"
|
||||
//=============================================================================
|
||||
int main() {
|
||||
|
||||
Lista l;
|
||||
int i, x1, x2, x3;
|
||||
printf("==== LISTA FLEXIVEL ====\n");
|
||||
|
||||
l = new_lista();
|
||||
|
||||
for(i=0; i < 5; i++)
|
||||
insert_end(&l, i);
|
||||
|
||||
printf("Apos inserir os dados: \n");
|
||||
print_lista(&l);
|
||||
|
||||
printf("Apos inserir no inicio: \n");
|
||||
insert_begin(&l, i++);
|
||||
print_lista(&l);
|
||||
|
||||
printf("Apos inserir no final: \n");
|
||||
insert_end(&l, i++);
|
||||
print_lista(&l);
|
||||
|
||||
printf("Apos inserir na posicao 4: \n");
|
||||
insert_at(&l, i++, 4);
|
||||
print_lista(&l);
|
||||
|
||||
printf("Apos remover no inicio: \n");
|
||||
x1 = remove_begin(&l);
|
||||
print_lista(&l);
|
||||
|
||||
printf("Apos remover no final: \n");
|
||||
x1 = remove_end(&l);
|
||||
print_lista(&l);
|
||||
|
||||
printf("Apos remover na posicao 2: \n");
|
||||
x1 = remove_at(&l, 2);
|
||||
print_lista(&l);
|
||||
|
||||
delete_lista(&l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
#ifndef PILHA_H
|
||||
#define PILHA_H
|
||||
//=============================================================================
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
#include "celula.h"
|
||||
//=============================================================================
|
||||
typedef struct Pilha{
|
||||
struct Celula *topo;
|
||||
int size;
|
||||
} Pilha;
|
||||
//=============================================================================
|
||||
Pilha new_pilha(){
|
||||
Pilha temp;
|
||||
temp.topo = NULL;
|
||||
temp.size = 0;
|
||||
return temp;
|
||||
}
|
||||
//=============================================================================
|
||||
int size_pilha(Pilha *p){
|
||||
return p->size;
|
||||
}
|
||||
//=============================================================================
|
||||
void push_pilha(Pilha *p, int elemento){
|
||||
Celula *temp = new_celula(elemento);
|
||||
temp->prox = p->topo;
|
||||
p->topo = temp;
|
||||
p->size++;
|
||||
}
|
||||
//=============================================================================
|
||||
int pop_pilha(Pilha *p){
|
||||
if (p->topo == NULL){
|
||||
printf("\nA pilha esta vazia!\n");
|
||||
return INT_MIN;
|
||||
}
|
||||
int elemento = p->topo->elemento;
|
||||
Celula *temp = p->topo;
|
||||
p->topo = p->topo->prox;
|
||||
p->size--;
|
||||
free(temp);
|
||||
return elemento;
|
||||
}
|
||||
//=============================================================================
|
||||
void print_pilha(Pilha *p){
|
||||
Celula *i;
|
||||
printf("[");
|
||||
for (i = p->topo; i != NULL; i = i->prox)
|
||||
{
|
||||
printf("%d, ", i->elemento);
|
||||
}
|
||||
printf("] \n");
|
||||
}
|
||||
//=============================================================================
|
||||
void delete_pilha(Pilha *p){
|
||||
while(p->size > 0)
|
||||
pop_pilha(p);
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#include "pilha.h"
|
||||
//=============================================================================
|
||||
int main() {
|
||||
|
||||
Pilha p;
|
||||
int i, x1, x2, x3;
|
||||
printf("==== PILHA FLEXIVEL ====\n");
|
||||
|
||||
p = new_pilha();
|
||||
|
||||
for(i=0; i < 10; i++)
|
||||
push_pilha(&p, i);
|
||||
|
||||
printf("Apos inserrir os dados: \n");
|
||||
print_pilha(&p);
|
||||
|
||||
x1 = pop_pilha(&p);
|
||||
x2 = pop_pilha(&p);
|
||||
x3 = pop_pilha(&p);
|
||||
|
||||
printf("Apos as remocoes (%d, %d, %d) \n", x1, x2, x3);
|
||||
print_pilha(&p);
|
||||
|
||||
delete_pilha(&p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -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++) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef BUBBLESORT_H
|
||||
#define BUBBLESORT_H
|
||||
//=============================================================================
|
||||
#include "sort.h"
|
||||
//=============================================================================
|
||||
void bubbleSort(int *array, int n){
|
||||
int i, j;
|
||||
for (i = (n - 1); i > 0; i--) {
|
||||
for (j = 0; j < i; j++) {
|
||||
if (array[j] > array[j + 1]) {
|
||||
swap(&array[j], &array[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
|
|
@ -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
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
#ifndef ORDENACAO_H
|
||||
#define ORDENACAO_H
|
||||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
//=============================================================================
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
|
@ -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("[ ");
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
//=============================================================================
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
//=============================================================================
|
||||
// PROCEDIMENTO PARA TROCAR DOIS ELEMENTOS DO VETOR
|
||||
void swap(int *i, int *j) {
|
||||
int temp = *i;
|
||||
*i = *j;
|
||||
*j = temp;
|
||||
}
|
||||
//=============================================================================
|
||||
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM CRESCENTE
|
||||
void crescente(int *array, int n) {
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
array[i] = i;
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM DECRESCENTE
|
||||
void decrescente(int *array, int n) {
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
array[i] = n - 1 - i;
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
// PROCEDIMENTO PARA PREENCHER UM ARRANJO COM ELEMENTOS EM ORDEM ALEATORIA
|
||||
void aleatorio(int *array, int n) {
|
||||
int i, pos;
|
||||
crescente(array, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
pos = rand() % n;
|
||||
swap(&array[i], &array[pos]);
|
||||
}
|
||||
}
|
||||
//=============================================================================
|
||||
// PROCEDIMENTO PARA EXIBIR OS DADOS PRESENTES NO ARRANJO
|
||||
void print_array(int *array, int n) {
|
||||
int i;
|
||||
printf("[ ");
|
||||
for (i = 0; i < n; i++) {
|
||||
printf("%d ", array[i]);
|
||||
}
|
||||
printf("] \n");
|
||||
}
|
||||
//=============================================================================
|
||||
// PROCEDIMENTO PARA VERIFICAR SE O ARRANJO ESTA ORDENADO
|
||||
bool isOrdenado(int *array, int n){
|
||||
int i;
|
||||
for(int i = 1; i < n; i++){
|
||||
if(array[i-1] > array[i]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//=============================================================================
|
||||
#endif
|
||||
Loading…
Reference in New Issue