• 0

I am gonna explode -> easy C program


Question

Here is the input http://pastebin.com/BBHxd0iY. I have to evaluate in each line what is the county with the most people and from all the tests to that line the parish with the least people from that county. I am using a linked list. The county with the most people should be the root. I really need a fast reply xD.

District, county, parish, population.

pessoa = people, concelho = county, numero = number, nome = name, freguesia = parish, insere = insert, pesquisa = search, proximo = next.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lista.h"

concelho* criarConcelho( char *nomeConcelho, int pessoas ) {

concelho *ptr;

if ( ( ptr = ( concelho *) malloc( sizeof(concelho) * 1 ) ) == NULL ) return NULL;

ptr->concelho = nomeConcelho;
ptr->numeroPessoas = pessoas;
ptr->proximo = NULL;

return ptr;
}

freguesia* criarFreguesia ( char *nomeFreg, int populacao ) {

freguesia *ptr;

if ( ( ptr = ( freguesia *) malloc( sizeof(freguesia) * 1 ) ) == NULL ) return NULL;

ptr->freguesia = nomeFreg;
ptr->populacao = populacao;

return ptr;
}

FILE* readlnVeriPlace( FILE *input, baseConcelhos *bConcelhos) {

int i;
int numeroPessoas;
concelho *ptr, *intelliScan;
freguesia *freg;
char *concelho;
char *freguesia;
char ch;
int pos;


if ( input == NULL || bConcelhos == NULL ) return NULL;

freguesia = (char *) malloc ( sizeof(char) * 50 );
concelho = (char *) malloc ( sizeof(char) * 50 );
if ( freguesia == NULL || concelho == NULL ) return input;

// READLINE

while ( ( ch = fgetc(input) ) != ',' );

i = 0;
while ( ( ch = fgetc(input) ) != ',' ) {
concelho[i] = ch;
i++;
}
concelho[i] = '\0';

i = 0;
while ( ( ch = fgetc(input) ) != ',' ) {
freguesia[i] = ch;
i++;
}
freguesia[i] = '\0';

fscanf(input, "%d", &numeroPessoas);

while ( fgetc(input) != '\n' && !feof(input)) ;

//ENDREADLINE

intelliScan = lista_pesquisa( bConcelhos, concelho, &pos);

if ( intelliScan != NULL ) {
lista_atribui( bConcelhos, pos, &freguesia, numeroPessoas);
free(concelho);
}
else {
freg = criarFreguesia ( freguesia, numeroPessoas );
ptr = criarConcelho ( concelho, numeroPessoas );
ptr->freg = freg;
intelliScan = ptr;
if ( bConcelhos->numero == 0 ) lista_insere( bConcelhos, -1, ptr);
}

lista_ordena( bConcelhos, pos, intelliScan);

return input;
}


int main( int argc, const char *argv[] ) {

FILE *input;
FILE *output;
baseConcelhos *base;
concelho *umDoLiTa;

if ( argc != 2 ) {
fprintf( stdout, "%s shuffle.csv\n", argv[0] );
exit(1);
}

if ( ( input = fopen( argv[1], "r") ) == NULL ) {
fprintf( stdout, "Not possible to open the file.\n");
exit(2);
}

if ( ( output = fopen ( "output.csv", "w") ) == NULL ) exit(3);

if ( ( base = (baseConcelhos *) malloc( sizeof(baseConcelhos) * 1) ) == NULL ) exit (4);

base->numero = 0;
base->proximo = NULL;

while ( !feof(input) ) {
input = readlnVeriPlace( input, base );
fprintf( stdout, "%s, %s, %d\n", base->proximo->freg->freguesia, base->proximo->concelho, base->proximo->freg->populacao);
}

return 0;
}[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1078461-i-am-gonna-explode-easy-c-program/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

typedef struct {
int populacao;
char *freguesia;
}freguesia;

typedef struct concelhos {
char *concelho;
int numeroPessoas;
freguesia *freg;
struct concelhos *proximo;
} concelho;

typedef struct baseConcelhos {
int numero;
concelho *proximo;
} baseConcelhos;

int lista_atribui( baseConcelhos *l, int pos, char **str, int numeroP);

int lista_insere( baseConcelhos *l, int pos, concelho *curr);

concelho* lista_trocaAUX(baseConcelhos *l, int pos);

concelho * lista_pesquisa( baseConcelhos *l, char* str, int *pos );

int lista_ordena( baseConcelhos *l, int posAlt, concelho *intelliScan );
[/CODE]

  • 0

#include "lista.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int lista_atribui( baseConcelhos *l, int pos, char **str, int numeroP)
{
int i = 0;
concelho *aux;

if (l == NULL || pos < 0)
return -1;

aux = l->proximo;

for (i = 0; i < pos && aux != NULL; i++)
aux = aux->proximo;

if (aux == NULL)
return -1;

aux->numeroPessoas += numeroP;

if ( numeroP < aux->freg->populacao ) {
aux->freg->populacao = numeroP;
free(aux->freg->freguesia);
aux->freg->freguesia = *str;
} else free(*str);

return pos;
}


int lista_insere( baseConcelhos *l, int pos, concelho *curr)
{
int i = 0;
concelho *temp;

if (l == NULL || pos < -1 || pos >= l->numero) return -1;

temp = l->proximo;

if (curr == NULL) return -1;

l->numero++;

if(pos == -1)
{
if (temp == NULL) l->proximo = curr;
else
{
while (temp->proximo != NULL) temp = temp->proximo;
temp->proximo = curr;
}

return l->numero-1;
}

if (pos == 0)
{
curr->proximo = temp;
l->proximo = curr;
return pos;
}

for (i = 0; i < pos-1 && temp != NULL; i++) temp = temp->proximo;

curr->proximo = temp->proximo;
temp->proximo = curr;

return pos;
}


concelho* lista_trocaAUX(baseConcelhos *l, int pos)
{
int i = 0;
concelho *prev, *curr;

if (l == NULL || pos < 0 || pos >= l->numero) return NULL;

curr = l->proximo;

l->numero--;

if(pos == 0)
{
l->proximo = curr->proximo;
curr->proximo = NULL;
return curr;
}

for( i = 0; i < pos && curr->proximo; i++)
{
prev = curr;
curr = curr->proximo;
}

prev->proximo = curr->proximo;

curr->proximo = NULL;
return curr;
}

concelho * lista_pesquisa( baseConcelhos *l, char* str, int *pos)
{

int i = 0;
concelho *aux;

if( l == NULL ) return NULL;

for (aux = l->proximo; aux != NULL; aux = aux->proximo, i++)
{
if ( strcmp( aux->concelho, str) == 0) {
*pos = i;
return aux;
}
}

*pos = -1;
return NULL;
}

int lista_ordena( baseConcelhos *l, int posAlt, concelho *intelliScan)
{
concelho *ptr;
int i = 0;

if ( l == NULL ) return -1;
if ( l->proximo == NULL || l->numero == 1) return 0;

if ( posAlt != -1 ) intelliScan = lista_trocaAUX( l, posAlt);

for ( ptr = l->proximo; ptr != NULL; ptr = ptr->proximo, i++ ) {
if ( ptr->numeroPessoas < intelliScan->numeroPessoas ) {
lista_insere( l, i, intelliScan);
}
}
lista_insere( l, -1, intelliScan);

return 0;
}[/CODE]

  • 0

Where exactly are you stuck? Its quite difficult to understand code not written in your language (now I know how hard it must be for non-english speakers).

If you want the county with the largest number of people at the root, then use a sorting algorithm to get the list in order.

To begin with use something simple like bubble sort, then once that works you can try implementing faster and more efficient (but more complicated) sorting algorithms.

  • 0

Please delete this topic I will create a new one with the proper documentation. And in English, since tiagosilva29 which is Portuguese did not even give it a go. And just ++ to his massive posts. I am not worried with the exams which are next door, it is all under control. I just cant spot the glitch... Some extra pair of eyes would be appreciated.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.