|
|
View previous topic :: View next topic |
Author |
Message |
Orcino
Joined: 07 Sep 2003 Posts: 56
|
Struct and bubble sort |
Posted: Fri Nov 02, 2007 9:12 am |
|
|
Hi, i am with a problem in struct and bubble sort, with 10 elements this work right but more not right. I write a small code for test, sees below.
Compiler version is 4.058
forgives for my bad English
Code: |
/******************************************************************************
//
//
//******************************************************************************
#include <18F4525.h>
// #DEVICE ICD=TRUE
#FUSES NOWDT //No Watch Dog Timer
#FUSES H4 //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT //Code not protected from reading
#FUSES MCLR //Master Clear pin enabled
#use delay(clock = 40 Mhz)
#use FAST_IO(A)
#use FAST_IO(B)
#use FAST_IO(E)
#byte PORT_A=0XF80
#byte PORT_B=0XF81
#byte PORT_E=0XF84
#BIT STD = PORT_B.0
#BIT B_SALVA = PORT_B.4
#BIT B_PROXIMO = PORT_B.6
#BIT B_SAIR = PORT_B.5
#BIT B_APAGA = PORT_B.7
#BIT BUZZER = PORT_B.3
#include <giga_lcd.c>
int8 posicao=0;
//******************************************************************************
// Estrutura
//******************************************************************************
struct SGPSCoord
{
int32 buffer_latitude;
int32 buffer_longetude;
int distancia;
int16 ponto;
int8 status;
int1 flag_lido;
};
struct SGPSCoord GPSCOORD[100];
//******************************************************************************
// Funções
//******************************************************************************
void InverteElementos(struct SGPSCoord *pCoordenadas, int nE1, int nE2 );
void BubbleSort( int nQtdElementos, struct SGPSCoord *pCoordenadas );
void preenche_estrutura(void);
void mostra(void);
void calcula_distancias_ordena(void);
//******************************************************************************
// Função principal
//******************************************************************************
void main()
{
lcd_init();
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128); // overflow a cada 840ms
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
disable_interrupts(INT_RTCC);
disable_interrupts(GLOBAL);
setup_oscillator(False);
set_tris_B(0B11110111); // STD, SALVA , RS232, BOTOES
printf(LCD_PUTC," TESTE ");
preenche_estrutura();
BubbleSort(50,GPSCOORD);
while(1)
{
if(!b_proximo)
{
printf(LCD_PUTC,"\f");
printf(LCD_PUTC,"Posicao %u\r",posicao );
printf(LCD_PUTC,"\nDistancia %u\r",GPSCOORD[posicao].distancia);
posicao++;
delay_ms(500);
}
}
}
//******************************************************************************
// Ordenação
//******************************************************************************
void BubbleSort( int nQtdElementos, struct SGPSCoord *pCoordenadas )
{
int nBase, nCursor;
for( nBase = 0; nBase < nQtdElementos - 1; ++nBase )
{
for( nCursor = nBase + 1; nCursor < nQtdElementos; ++nCursor )
{
if( pCoordenadas[ nBase ].distancia > pCoordenadas[nCursor ].distancia )
InverteElementos( pCoordenadas, nBase, nCursor );
}
}
}
//******************************************************************************
// swap
//******************************************************************************
void InverteElementos(struct SGPSCoord *pCoordenadas, int nE1, int nE2 )
{
struct SGPSCoord sTemp;
memcpy( &sTemp, &pCoordenadas[ nE1 ], sizeof( sTemp ));
memcpy( &pCoordenadas[ nE1 ], &pCoordenadas[ nE2 ], sizeof( sTemp ));
memcpy( &pCoordenadas[ nE2 ], &sTemp, sizeof( sTemp ));
}
//******************************************************************************
// for test
//******************************************************************************
void preenche_estrutura(void)
{
int8 i=0, cont2=0;
for(i=0; i < 50 ; i++)
{
GPSCOORD[i].buffer_latitude = cont2;
GPSCOORD[i].buffer_longetude = cont2;
GPSCOORD[i].ponto = cont2;
GPSCOORD[i].distancia = cont2;
cont2++;
}
} |
|
|
|
Guest
|
|
Posted: Fri Nov 02, 2007 1:34 pm |
|
|
Hi, in other small code for test, in 18F452 work right, but in the 18F4525 not work right, see below
18F4525 -------------------------------- 18F452
Valor 0 posicao 0 --------------- Valor 0 posicao 0
Valor 0 posicao 1 --------------- Valor 1 posicao 1
Valor 0 posicao 2 --------------- Valor 3 posicao 2
Valor 0 posicao 3 --------------- Valor 4 posicao 3
Valor 0 posicao 4 --------------- Valor 8 posicao 4
Valor 0 posicao 5 --------------- Valor 9 posicao 5
Valor 1 posicao 6 --------------- Valor 11 posicao 6
Valor 4 posicao 7 --------------- Valor 12 posicao 7
Valor 8 posicao 8 --------------- Valor 13 posicao 8
Valor 9 posicao 9 ---------------- Valor 15 posicao 9
Valor 12 posicao 10 ------------- Valor 19 posicao 10
Valor 19 posicao 11 ------------- Valor 32 posicao 11
Valor 32 posicao 12 ------------- Valor 33 posicao 12
Valor 33 posicao 13 ------------- Valor 34 posicao 13
Valor 34 posicao 14 ------------- Valor 45 posicao 14
Valor 45 posicao 15 ------------- Valor 56 posicao 15
Valor 56 posicao 16 ------------- Valor 65 posicao 16
Valor 65 posicao 17 ------------- Valor 67 posicao 17
Valor 67 posicao 18 ------------- Valor 73 posicao 18
Valor 73 posicao 19 ------------- Valor 100 posicao 19
Code: |
// #include <18F452.h> // Work Right
#include <18F4525.h> // Nor work
// ***#DEVICE ICD=TRUE
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed osc with HW enabled 4X PLL
#use delay(clock = 20 Mhz)
#use FAST_IO(A)
#use FAST_IO(B)
#use FAST_IO(E)
//#byte PORT_A=0XF80
//#byte PORT_B=0XF81
//#byte PORT_E=0XF84
//#byte DTMF_IN = PORT_A
//#BIT CLK = PORT_E.0 //|
//#BIT DADO = PORT_E.1 //| HT 9200A
//#BIT CE = PORT_E.2 //|
//#BIT STD = PORT_B.0
//#BIT B_SALVA = PORT_B.4 // |
//#BIT B_PROXIMO = PORT_B.6 // | Botoes p/ navegação nas coordenadas salvas
//#BIT B_SAIR = PORT_B.5 // |
//#BIT B_APAGA = PORT_B.7 // |
//#BIT BUZZER = PORT_B.3
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//******************************************************************************
#include <giga_lcd.c> // Drive de LCD modificado
#define scl pin_C3 // Define pinos de comunicação com EEPROM
#define sda pin_C4
struct SGPSCoord
{
int buffer_latitude; // Longetude
int buffer_longetude; // Latitude
int distancia; // Dist. do ponto atual até a coordenada
int ponto; // N. do ponto
int status; // status do eqpt monitorado
int flag_lido; // Indica se coordenada já foi lida
};
struct SGPSCoord GPSCOORD[100];
//******************************************************************************
// Funções
//******************************************************************************
void InverteElementos(struct SGPSCoord *pCoordenadas, int nE1, int nE2 );
void BubbleSort( int nQtdElementos, struct SGPSCoord *pCoordenadas );
void preenche_estrutura(void);
void mostra(void);
void calcula_distancias_ordena(void);
//******************************************************************************
// Função principal
//******************************************************************************
void main()
{
int cont1;
preenche_estrutura();
BubbleSort(20,GPSCOORD); // Ordena pela distancia
for(cont1=0;cont1 < 20 ; cont1++)
{
printf("\nValor %u posicao %u\r",GPSCOORD[cont1].distancia, cont1 );
}
while(1)
{
}
}
//******************************************************************************
// Ordenação
//******************************************************************************
void BubbleSort( int nQtdElementos, struct SGPSCoord *pCoordenadas )
{
int nBase, nCursor;
for( nBase = 0; nBase < nQtdElementos - 1; ++nBase )
{
for( nCursor = nBase + 1; nCursor < nQtdElementos; ++nCursor )
{
if( pCoordenadas[ nBase ].distancia > pCoordenadas[nCursor ].distancia )
InverteElementos( pCoordenadas, nBase, nCursor );
}
}
}
//******************************************************************************
// inverte os elementos
//******************************************************************************
void InverteElementos(struct SGPSCoord *pCoordenadas, int nE1, int nE2 )
{
struct SGPSCoord sTemp;
memcpy( &sTemp, &pCoordenadas[ nE1 ], sizeof( sTemp ));
memcpy( &pCoordenadas[ nE1 ], &pCoordenadas[ nE2 ], sizeof( sTemp ));
memcpy( &pCoordenadas[ nE2 ], &sTemp, sizeof( sTemp ));
}
//******************************************************************************
// For test
//******************************************************************************
void preenche_estrutura(void)
{
GPSCOORD[0].distancia = 12;
GPSCOORD[1].distancia = 33;
GPSCOORD[2].distancia = 32;
GPSCOORD[3].distancia = 45;
GPSCOORD[4].distancia = 67;
GPSCOORD[5].distancia = 34;
GPSCOORD[6].distancia = 65;
GPSCOORD[7].distancia = 73;
GPSCOORD[8].distancia = 56;
GPSCOORD[9].distancia = 19;
GPSCOORD[10].distancia = 0;
GPSCOORD[11].distancia = 1;
GPSCOORD[12].distancia = 4;
GPSCOORD[13].distancia = 8;
GPSCOORD[14].distancia = 9;
GPSCOORD[15].distancia = 3;
GPSCOORD[16].distancia = 11;
GPSCOORD[17].distancia = 13;
GPSCOORD[18].distancia = 15;
GPSCOORD[19].distancia = 100;
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|