Skip to content

Instantly share code, notes, and snippets.

@matheusopedro
Last active September 1, 2016 21:18
Show Gist options
  • Save matheusopedro/19371a795ca5bb72309047d94ce2b582 to your computer and use it in GitHub Desktop.
Save matheusopedro/19371a795ca5bb72309047d94ce2b582 to your computer and use it in GitHub Desktop.
Trabalho de Compiladores e Montadores (Em andamento...)
/*
Trabalho de Compiladores e Montadores
Montar um algoritmo que identifica o numero de linhas comentadas em um arquivo C
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *arquivo;
char filename[255] = "HashTable.c";
// Para ler o arquivo, sera usada a funcao fgetc
int linhas;
int i = 0;
arquivo = fopen(filename, "r");
if(arquivo==NULL){
printf("O arquivo nao existe. Verifique!");
return;
}
else {
while(!feof(arquivo)){
// Pega os caracteres das linhas. Um por vez
linhas = fgetc(arquivo);
if(linhas == '/'){// Verifica se a linha comeca com '//'
if((linhas = fgetc(arquivo)) == '/'){
i++;
}
}
}
// Imprimindo o resultado
printf("Numero de linhas comentadas: %d", i);
}
//Fecha o arquivo (Boa pratica).
fclose(arquivo);
return;
}
/*
* Arquivo usado para teste do programa.
* Arquivo original:
* https://github.com/jelathro/C/blob/master/HashTable/HashTable.c
*
*
* @author: jelathro
* @date: 11/2/13
*
* Implementation file for HashTable.
*
* After ten collisions (ie. ten items in
* a particular hash items list) the hash
* table will expand to the maxCollision
* plus the growthFactor.
*/
#include <stdlib.h>
#include "HashTable.h"
HashTable * hashtable_initialize(size_t size, size_t mc, size_t gf, hash_function fn, compare_equal efn){
size_t i;
HashTable * ht = (HashTable *) malloc( sizeof(HashTable) );
ht->hf = fn;
ht->eq = efn;
ht->size = size;
ht->maxCollisions = mc;
ht->growthFactor = gf;
ht->table = (Item **) malloc(size * sizeof( Item * ));
for(i=0; i<size; i++){
ht->table[i] = 0;
}
return(ht);
}
void * hashtable_get(HashTable * ht, void * key){
size_t hash = ht->hf(key);
Item * next = ht->table[ hash % ht->size ];
while(next){
if(ht->eq( next->key, key )){
return( next->value );
}else{
next = next->next;
}
}
return( (void *)0 );
}
int hashtable_destroy(HashTable * ht){
size_t i;
for(i=0; i<ht->size; i++){
free(ht->table[i]);
}
free(ht->table);
free(ht);
return(1);
}
int hashtable_resize(HashTable * ht, size_t size){
HashTable * newht = hashtable_initialize(size, ht->maxCollisions, ht->growthFactor, ht->hf, ht->eq);
int i;
Item * next;
// Re-enter all the items again into the new hashtable
// with the new size.
for(i=0; i<ht->size; i++){
if(ht->table[i]){
for(next=ht->table[i]; next; next=next->next){
hashtable_add(newht, next->key, next->value);
}
}
hashtable_remove(ht, ht->table[i]->key);
}
free(ht->table);
ht->size = newht->size;
ht->table = newht->table;
return(1);
}
int hashtable_add(HashTable * ht, void * key, void * value){
size_t hash = ht->hf(key);
Item * next = ht->table[ hash % ht->size ];
size_t i = 0;
while(next){
// Replace data if key is same
if(ht->eq( next->key, key )){
next->value = value;
return(1);
}
next = next->next;
i++;
}
next = (Item *)malloc( sizeof(Item) );
next->key = key;
next->value = value;
next->next = ht->table[ hash % ht->size ];
ht->table[ hash % ht->size ] = next;
if(i >= ht->maxCollisions){
hashtable_resize(ht, ht->size + ht->growthFactor);
}
return(1);
}
int hashtable_remove(HashTable * ht, void * key){
size_t hash = ht->hf(key);
Item * next = ht->table[ hash % ht->size ];
Item * prev = 0;
while(next){
if(ht->eq( next->key, key )){
if(prev){
prev->next = next->next;
}else{
ht->table[ hash % ht->size ] = next->next;
}
free(next);
return(1);
}
prev = next;
next = next->next;
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment